OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" // NOLINT | 5 #include "platform/globals.h" // NOLINT |
6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
7 | 7 |
8 #include "vm/os_thread.h" | 8 #include "vm/os_thread.h" |
9 | 9 |
10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 VALIDATE_PTHREAD_RESULT(result); | 223 VALIDATE_PTHREAD_RESULT(result); |
224 #endif // defined(DEBUG) | 224 #endif // defined(DEBUG) |
225 | 225 |
226 result = pthread_mutex_init(data_.mutex(), &attr); | 226 result = pthread_mutex_init(data_.mutex(), &attr); |
227 // Verify that creating a pthread_mutex succeeded. | 227 // Verify that creating a pthread_mutex succeeded. |
228 VALIDATE_PTHREAD_RESULT(result); | 228 VALIDATE_PTHREAD_RESULT(result); |
229 | 229 |
230 result = pthread_mutexattr_destroy(&attr); | 230 result = pthread_mutexattr_destroy(&attr); |
231 VALIDATE_PTHREAD_RESULT(result); | 231 VALIDATE_PTHREAD_RESULT(result); |
232 | 232 |
233 // When running with assertions enabled we do track the owner. | |
234 #if defined(DEBUG) | 233 #if defined(DEBUG) |
| 234 // When running with assertions enabled we track the owner. |
235 owner_ = OSThread::kInvalidThreadId; | 235 owner_ = OSThread::kInvalidThreadId; |
236 #endif // defined(DEBUG) | 236 #endif // defined(DEBUG) |
237 } | 237 } |
238 | 238 |
239 | 239 |
240 Mutex::~Mutex() { | 240 Mutex::~Mutex() { |
241 int result = pthread_mutex_destroy(data_.mutex()); | 241 int result = pthread_mutex_destroy(data_.mutex()); |
242 // Verify that the pthread_mutex was destroyed. | 242 // Verify that the pthread_mutex was destroyed. |
243 VALIDATE_PTHREAD_RESULT(result); | 243 VALIDATE_PTHREAD_RESULT(result); |
244 | 244 |
245 // When running with assertions enabled we do track the owner. | |
246 #if defined(DEBUG) | 245 #if defined(DEBUG) |
| 246 // When running with assertions enabled we track the owner. |
247 ASSERT(owner_ == OSThread::kInvalidThreadId); | 247 ASSERT(owner_ == OSThread::kInvalidThreadId); |
248 #endif // defined(DEBUG) | 248 #endif // defined(DEBUG) |
249 } | 249 } |
250 | 250 |
251 | 251 |
252 void Mutex::Lock() { | 252 void Mutex::Lock() { |
253 int result = pthread_mutex_lock(data_.mutex()); | 253 int result = pthread_mutex_lock(data_.mutex()); |
254 // Specifically check for dead lock to help debugging. | 254 // Specifically check for dead lock to help debugging. |
255 ASSERT(result != EDEADLK); | 255 ASSERT(result != EDEADLK); |
256 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 256 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
257 // When running with assertions enabled we do track the owner. | |
258 #if defined(DEBUG) | 257 #if defined(DEBUG) |
| 258 // When running with assertions enabled we track the owner. |
259 owner_ = OSThread::GetCurrentThreadId(); | 259 owner_ = OSThread::GetCurrentThreadId(); |
260 #endif // defined(DEBUG) | 260 #endif // defined(DEBUG) |
261 } | 261 } |
262 | 262 |
263 | 263 |
264 bool Mutex::TryLock() { | 264 bool Mutex::TryLock() { |
265 int result = pthread_mutex_trylock(data_.mutex()); | 265 int result = pthread_mutex_trylock(data_.mutex()); |
266 // Return false if the lock is busy and locking failed. | 266 // Return false if the lock is busy and locking failed. |
267 if (result == EBUSY) { | 267 if (result == EBUSY) { |
268 return false; | 268 return false; |
269 } | 269 } |
270 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 270 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
271 // When running with assertions enabled we do track the owner. | |
272 #if defined(DEBUG) | 271 #if defined(DEBUG) |
| 272 // When running with assertions enabled we track the owner. |
273 owner_ = OSThread::GetCurrentThreadId(); | 273 owner_ = OSThread::GetCurrentThreadId(); |
274 #endif // defined(DEBUG) | 274 #endif // defined(DEBUG) |
275 return true; | 275 return true; |
276 } | 276 } |
277 | 277 |
278 | 278 |
279 void Mutex::Unlock() { | 279 void Mutex::Unlock() { |
280 // When running with assertions enabled we do track the owner. | |
281 #if defined(DEBUG) | 280 #if defined(DEBUG) |
| 281 // When running with assertions enabled we track the owner. |
282 ASSERT(IsOwnedByCurrentThread()); | 282 ASSERT(IsOwnedByCurrentThread()); |
283 owner_ = OSThread::kInvalidThreadId; | 283 owner_ = OSThread::kInvalidThreadId; |
284 #endif // defined(DEBUG) | 284 #endif // defined(DEBUG) |
285 int result = pthread_mutex_unlock(data_.mutex()); | 285 int result = pthread_mutex_unlock(data_.mutex()); |
286 // Specifically check for wrong thread unlocking to aid debugging. | 286 // Specifically check for wrong thread unlocking to aid debugging. |
287 ASSERT(result != EPERM); | 287 ASSERT(result != EPERM); |
288 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 288 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
289 } | 289 } |
290 | 290 |
291 | 291 |
(...skipping 18 matching lines...) Expand all Loading... |
310 VALIDATE_PTHREAD_RESULT(result); | 310 VALIDATE_PTHREAD_RESULT(result); |
311 | 311 |
312 result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); | 312 result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); |
313 VALIDATE_PTHREAD_RESULT(result); | 313 VALIDATE_PTHREAD_RESULT(result); |
314 | 314 |
315 result = pthread_cond_init(data_.cond(), &cond_attr); | 315 result = pthread_cond_init(data_.cond(), &cond_attr); |
316 VALIDATE_PTHREAD_RESULT(result); | 316 VALIDATE_PTHREAD_RESULT(result); |
317 | 317 |
318 result = pthread_condattr_destroy(&cond_attr); | 318 result = pthread_condattr_destroy(&cond_attr); |
319 VALIDATE_PTHREAD_RESULT(result); | 319 VALIDATE_PTHREAD_RESULT(result); |
| 320 |
| 321 #if defined(DEBUG) |
| 322 // When running with assertions enabled we track the owner. |
| 323 owner_ = OSThread::kInvalidThreadId; |
| 324 #endif // defined(DEBUG) |
320 } | 325 } |
321 | 326 |
322 | 327 |
323 Monitor::~Monitor() { | 328 Monitor::~Monitor() { |
| 329 #if defined(DEBUG) |
| 330 // When running with assertions enabled we track the owner. |
| 331 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 332 #endif // defined(DEBUG) |
| 333 |
324 int result = pthread_mutex_destroy(data_.mutex()); | 334 int result = pthread_mutex_destroy(data_.mutex()); |
325 VALIDATE_PTHREAD_RESULT(result); | 335 VALIDATE_PTHREAD_RESULT(result); |
326 | 336 |
327 result = pthread_cond_destroy(data_.cond()); | 337 result = pthread_cond_destroy(data_.cond()); |
328 VALIDATE_PTHREAD_RESULT(result); | 338 VALIDATE_PTHREAD_RESULT(result); |
329 } | 339 } |
330 | 340 |
331 | 341 |
332 void Monitor::Enter() { | 342 void Monitor::Enter() { |
333 int result = pthread_mutex_lock(data_.mutex()); | 343 int result = pthread_mutex_lock(data_.mutex()); |
334 VALIDATE_PTHREAD_RESULT(result); | 344 VALIDATE_PTHREAD_RESULT(result); |
335 // TODO(iposva): Do we need to track lock owners? | 345 |
| 346 #if defined(DEBUG) |
| 347 // When running with assertions enabled we track the owner. |
| 348 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 349 owner_ = OSThread::GetCurrentThreadId(); |
| 350 #endif // defined(DEBUG) |
336 } | 351 } |
337 | 352 |
338 | 353 |
339 void Monitor::Exit() { | 354 void Monitor::Exit() { |
340 // TODO(iposva): Do we need to track lock owners? | 355 #if defined(DEBUG) |
| 356 // When running with assertions enabled we track the owner. |
| 357 ASSERT(IsOwnedByCurrentThread()); |
| 358 owner_ = OSThread::kInvalidThreadId; |
| 359 #endif // defined(DEBUG) |
| 360 |
341 int result = pthread_mutex_unlock(data_.mutex()); | 361 int result = pthread_mutex_unlock(data_.mutex()); |
342 VALIDATE_PTHREAD_RESULT(result); | 362 VALIDATE_PTHREAD_RESULT(result); |
343 } | 363 } |
344 | 364 |
345 | 365 |
346 Monitor::WaitResult Monitor::Wait(int64_t millis) { | 366 Monitor::WaitResult Monitor::Wait(int64_t millis) { |
347 return WaitMicros(millis * kMicrosecondsPerMillisecond); | 367 Monitor::WaitResult retval = WaitMicros(millis * kMicrosecondsPerMillisecond); |
| 368 return retval; |
348 } | 369 } |
349 | 370 |
350 | 371 |
351 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { | 372 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { |
352 // TODO(iposva): Do we need to track lock owners? | 373 #if defined(DEBUG) |
| 374 // When running with assertions enabled we track the owner. |
| 375 ASSERT(IsOwnedByCurrentThread()); |
| 376 ThreadId saved_owner = owner_; |
| 377 owner_ = OSThread::kInvalidThreadId; |
| 378 #endif // defined(DEBUG) |
| 379 |
353 Monitor::WaitResult retval = kNotified; | 380 Monitor::WaitResult retval = kNotified; |
354 if (micros == kNoTimeout) { | 381 if (micros == kNoTimeout) { |
355 // Wait forever. | 382 // Wait forever. |
356 int result = pthread_cond_wait(data_.cond(), data_.mutex()); | 383 int result = pthread_cond_wait(data_.cond(), data_.mutex()); |
357 VALIDATE_PTHREAD_RESULT(result); | 384 VALIDATE_PTHREAD_RESULT(result); |
358 } else { | 385 } else { |
359 struct timespec ts; | 386 struct timespec ts; |
360 ComputeTimeSpecMicros(&ts, micros); | 387 ComputeTimeSpecMicros(&ts, micros); |
361 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); | 388 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); |
362 ASSERT((result == 0) || (result == ETIMEDOUT)); | 389 ASSERT((result == 0) || (result == ETIMEDOUT)); |
363 if (result == ETIMEDOUT) { | 390 if (result == ETIMEDOUT) { |
364 retval = kTimedOut; | 391 retval = kTimedOut; |
365 } | 392 } |
366 } | 393 } |
| 394 |
| 395 #if defined(DEBUG) |
| 396 // When running with assertions enabled we track the owner. |
| 397 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 398 owner_ = OSThread::GetCurrentThreadId(); |
| 399 ASSERT(owner_ == saved_owner); |
| 400 #endif // defined(DEBUG) |
367 return retval; | 401 return retval; |
368 } | 402 } |
369 | 403 |
370 | 404 |
371 void Monitor::Notify() { | 405 void Monitor::Notify() { |
372 // TODO(iposva): Do we need to track lock owners? | 406 // When running with assertions enabled we track the owner. |
| 407 ASSERT(IsOwnedByCurrentThread()); |
373 int result = pthread_cond_signal(data_.cond()); | 408 int result = pthread_cond_signal(data_.cond()); |
374 VALIDATE_PTHREAD_RESULT(result); | 409 VALIDATE_PTHREAD_RESULT(result); |
375 } | 410 } |
376 | 411 |
377 | 412 |
378 void Monitor::NotifyAll() { | 413 void Monitor::NotifyAll() { |
379 // TODO(iposva): Do we need to track lock owners? | 414 // When running with assertions enabled we track the owner. |
| 415 ASSERT(IsOwnedByCurrentThread()); |
380 int result = pthread_cond_broadcast(data_.cond()); | 416 int result = pthread_cond_broadcast(data_.cond()); |
381 VALIDATE_PTHREAD_RESULT(result); | 417 VALIDATE_PTHREAD_RESULT(result); |
382 } | 418 } |
383 | 419 |
384 } // namespace dart | 420 } // namespace dart |
385 | 421 |
386 #endif // defined(TARGET_OS_LINUX) | 422 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |