| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) | 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) |
| 4 * Copyright (C) 2011 Research In Motion Limited. All rights reserved. | 4 * Copyright (C) 2011 Research In Motion Limited. All rights reserved. |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * | 9 * |
| 10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 ThreadIdentifier id = ThreadIdentifierData::identifier(); | 282 ThreadIdentifier id = ThreadIdentifierData::identifier(); |
| 283 if (id) | 283 if (id) |
| 284 return id; | 284 return id; |
| 285 | 285 |
| 286 // Not a WTF-created thread, ThreadIdentifier is not established yet. | 286 // Not a WTF-created thread, ThreadIdentifier is not established yet. |
| 287 id = establishIdentifierForPthreadHandle(pthread_self()); | 287 id = establishIdentifierForPthreadHandle(pthread_self()); |
| 288 ThreadIdentifierData::initialize(id); | 288 ThreadIdentifierData::initialize(id); |
| 289 return id; | 289 return id; |
| 290 } | 290 } |
| 291 | 291 |
| 292 Mutex::Mutex() | 292 MutexBase::MutexBase(bool recursive) |
| 293 { | 293 { |
| 294 pthread_mutexattr_t attr; | 294 pthread_mutexattr_t attr; |
| 295 pthread_mutexattr_init(&attr); | 295 pthread_mutexattr_init(&attr); |
| 296 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); | 296 pthread_mutexattr_settype(&attr, recursive ? PTHREAD_MUTEX_RECURSIVE : PTHRE
AD_MUTEX_NORMAL); |
| 297 | 297 |
| 298 int result = pthread_mutex_init(&m_mutex, &attr); | 298 int result = pthread_mutex_init(&m_mutex.m_internalMutex, &attr); |
| 299 ASSERT_UNUSED(result, !result); | 299 ASSERT_UNUSED(result, !result); |
| 300 #if ENABLE(ASSERT) |
| 301 m_mutex.m_recursionCount = 0; |
| 302 #endif |
| 300 | 303 |
| 301 pthread_mutexattr_destroy(&attr); | 304 pthread_mutexattr_destroy(&attr); |
| 302 } | 305 } |
| 303 | 306 |
| 304 Mutex::~Mutex() | 307 MutexBase::~MutexBase() |
| 305 { | 308 { |
| 306 int result = pthread_mutex_destroy(&m_mutex); | 309 int result = pthread_mutex_destroy(&m_mutex.m_internalMutex); |
| 307 ASSERT_UNUSED(result, !result); | 310 ASSERT_UNUSED(result, !result); |
| 308 } | 311 } |
| 309 | 312 |
| 310 void Mutex::lock() | 313 void MutexBase::lock() |
| 311 { | 314 { |
| 312 int result = pthread_mutex_lock(&m_mutex); | 315 int result = pthread_mutex_lock(&m_mutex.m_internalMutex); |
| 316 ASSERT_UNUSED(result, !result); |
| 317 #if ENABLE(ASSERT) |
| 318 ++m_mutex.m_recursionCount; |
| 319 #endif |
| 320 } |
| 321 |
| 322 void MutexBase::unlock() |
| 323 { |
| 324 #if ENABLE(ASSERT) |
| 325 ASSERT(m_mutex.m_recursionCount); |
| 326 --m_mutex.m_recursionCount; |
| 327 #endif |
| 328 int result = pthread_mutex_unlock(&m_mutex.m_internalMutex); |
| 313 ASSERT_UNUSED(result, !result); | 329 ASSERT_UNUSED(result, !result); |
| 314 } | 330 } |
| 315 | 331 |
| 332 // There is a separate tryLock implementation for the Mutex and the |
| 333 // RecursiveMutex since on Windows we need to manually check if tryLock should |
| 334 // succeed or not for the non-recursive mutex. On Linux the two implementations |
| 335 // are equal except we can assert the recursion count is always zero for the |
| 336 // non-recursive mutex. |
| 316 bool Mutex::tryLock() | 337 bool Mutex::tryLock() |
| 317 { | 338 { |
| 318 int result = pthread_mutex_trylock(&m_mutex); | 339 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex); |
| 319 | 340 if (result == 0) { |
| 320 if (result == 0) | 341 #if ENABLE(ASSERT) |
| 342 // The Mutex class is not recursive, so the recursionCount should be |
| 343 // zero after getting the lock. |
| 344 ASSERT(!m_mutex.m_recursionCount); |
| 345 ++m_mutex.m_recursionCount; |
| 346 #endif |
| 321 return true; | 347 return true; |
| 348 } |
| 322 if (result == EBUSY) | 349 if (result == EBUSY) |
| 323 return false; | 350 return false; |
| 324 | 351 |
| 325 ASSERT_NOT_REACHED(); | 352 ASSERT_NOT_REACHED(); |
| 326 return false; | 353 return false; |
| 327 } | 354 } |
| 328 | 355 |
| 329 void Mutex::unlock() | 356 bool RecursiveMutex::tryLock() |
| 330 { | 357 { |
| 331 int result = pthread_mutex_unlock(&m_mutex); | 358 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex); |
| 332 ASSERT_UNUSED(result, !result); | 359 if (result == 0) { |
| 360 #if ENABLE(ASSERT) |
| 361 ++m_mutex.m_recursionCount; |
| 362 #endif |
| 363 return true; |
| 364 } |
| 365 if (result == EBUSY) |
| 366 return false; |
| 367 |
| 368 ASSERT_NOT_REACHED(); |
| 369 return false; |
| 333 } | 370 } |
| 334 | 371 |
| 335 ThreadCondition::ThreadCondition() | 372 ThreadCondition::ThreadCondition() |
| 336 { | 373 { |
| 337 pthread_cond_init(&m_condition, NULL); | 374 pthread_cond_init(&m_condition, NULL); |
| 338 } | 375 } |
| 339 | 376 |
| 340 ThreadCondition::~ThreadCondition() | 377 ThreadCondition::~ThreadCondition() |
| 341 { | 378 { |
| 342 pthread_cond_destroy(&m_condition); | 379 pthread_cond_destroy(&m_condition); |
| 343 } | 380 } |
| 344 | 381 |
| 345 void ThreadCondition::wait(Mutex& mutex) | 382 void ThreadCondition::wait(MutexBase& mutex) |
| 346 { | 383 { |
| 347 int result = pthread_cond_wait(&m_condition, &mutex.impl()); | 384 PlatformMutex& platformMutex = mutex.impl(); |
| 385 int result = pthread_cond_wait(&m_condition, &platformMutex.m_internalMutex)
; |
| 348 ASSERT_UNUSED(result, !result); | 386 ASSERT_UNUSED(result, !result); |
| 387 #if ENABLE(ASSERT) |
| 388 ++platformMutex.m_recursionCount; |
| 389 #endif |
| 349 } | 390 } |
| 350 | 391 |
| 351 bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime) | 392 bool ThreadCondition::timedWait(MutexBase& mutex, double absoluteTime) |
| 352 { | 393 { |
| 353 if (absoluteTime < currentTime()) | 394 if (absoluteTime < currentTime()) |
| 354 return false; | 395 return false; |
| 355 | 396 |
| 356 if (absoluteTime > INT_MAX) { | 397 if (absoluteTime > INT_MAX) { |
| 357 wait(mutex); | 398 wait(mutex); |
| 358 return true; | 399 return true; |
| 359 } | 400 } |
| 360 | 401 |
| 361 int timeSeconds = static_cast<int>(absoluteTime); | 402 int timeSeconds = static_cast<int>(absoluteTime); |
| 362 int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9); | 403 int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9); |
| 363 | 404 |
| 364 timespec targetTime; | 405 timespec targetTime; |
| 365 targetTime.tv_sec = timeSeconds; | 406 targetTime.tv_sec = timeSeconds; |
| 366 targetTime.tv_nsec = timeNanoseconds; | 407 targetTime.tv_nsec = timeNanoseconds; |
| 367 | 408 |
| 368 return pthread_cond_timedwait(&m_condition, &mutex.impl(), &targetTime) == 0
; | 409 PlatformMutex& platformMutex = mutex.impl(); |
| 410 int result = pthread_cond_timedwait(&m_condition, &platformMutex.m_internalM
utex, &targetTime); |
| 411 #if ENABLE(ASSERT) |
| 412 ++platformMutex.m_recursionCount; |
| 413 #endif |
| 414 return result == 0; |
| 369 } | 415 } |
| 370 | 416 |
| 371 void ThreadCondition::signal() | 417 void ThreadCondition::signal() |
| 372 { | 418 { |
| 373 int result = pthread_cond_signal(&m_condition); | 419 int result = pthread_cond_signal(&m_condition); |
| 374 ASSERT_UNUSED(result, !result); | 420 ASSERT_UNUSED(result, !result); |
| 375 } | 421 } |
| 376 | 422 |
| 377 void ThreadCondition::broadcast() | 423 void ThreadCondition::broadcast() |
| 378 { | 424 { |
| 379 int result = pthread_cond_broadcast(&m_condition); | 425 int result = pthread_cond_broadcast(&m_condition); |
| 380 ASSERT_UNUSED(result, !result); | 426 ASSERT_UNUSED(result, !result); |
| 381 } | 427 } |
| 382 | 428 |
| 383 } // namespace WTF | 429 } // namespace WTF |
| 384 | 430 |
| 385 #endif // USE(PTHREADS) | 431 #endif // USE(PTHREADS) |
| OLD | NEW |