diff options
Diffstat (limited to 'lldb/source/Host/common/Mutex.cpp')
| -rw-r--r-- | lldb/source/Host/common/Mutex.cpp | 131 |
1 files changed, 60 insertions, 71 deletions
diff --git a/lldb/source/Host/common/Mutex.cpp b/lldb/source/Host/common/Mutex.cpp index d519baf45b6..e8091bd3e0c 100644 --- a/lldb/source/Host/common/Mutex.cpp +++ b/lldb/source/Host/common/Mutex.cpp @@ -141,19 +141,14 @@ Mutex::Locker::~Locker () void Mutex::Locker::Lock (Mutex &mutex) { - pthread_mutex_t *mutex_ptr = mutex.GetMutex(); - // We already have this mutex locked or both are NULL... - if (m_mutex_ptr == mutex_ptr) + if (m_mutex_ptr == &mutex) return; Unlock (); - if (mutex_ptr) - { - m_mutex_ptr = mutex_ptr; - Mutex::Lock (m_mutex_ptr); - } + m_mutex_ptr = &mutex; + m_mutex_ptr->Lock(); } void @@ -161,27 +156,23 @@ Mutex::Locker::Unlock () { if (m_mutex_ptr) { - Mutex::Unlock (m_mutex_ptr); + m_mutex_ptr->Unlock (); m_mutex_ptr = NULL; } } bool -Mutex::Locker::TryLock (Mutex &mutex) +Mutex::Locker::TryLock (Mutex &mutex, const char *failure_message) { - pthread_mutex_t *mutex_ptr = mutex.GetMutex(); - // We already have this mutex locked! - if (m_mutex_ptr == mutex_ptr) - return m_mutex_ptr != NULL; + if (m_mutex_ptr == &mutex) + return true; Unlock (); - if (mutex_ptr) - { - if (Mutex::TryLock (mutex_ptr) == 0) - m_mutex_ptr = mutex_ptr; - } + if (mutex.TryLock(failure_message) == 0) + m_mutex_ptr = &mutex; + return m_mutex_ptr != NULL; } @@ -273,100 +264,98 @@ Mutex::GetMutex() return &m_mutex; } +//---------------------------------------------------------------------- +// Locks the mutex owned by this object, if the mutex is already +// locked, the calling thread will block until the mutex becomes +// available. +// +// RETURNS +// The error code from the pthread_mutex_lock() function call. +//---------------------------------------------------------------------- int -Mutex::Lock (pthread_mutex_t *mutex_ptr) +Mutex::Lock() { DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p)...\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr); #if ENABLE_MUTEX_ERROR_CHECKING - error_check_mutex (mutex_ptr, eMutexActionAssertInitialized); + error_check_mutex (&m_mutex, eMutexActionAssertInitialized); #endif - int err = ::pthread_mutex_lock (mutex_ptr); + int err = ::pthread_mutex_lock (&m_mutex); #if ENABLE_MUTEX_ERROR_CHECKING if (err) { - Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_lock(%p) => err = %i (%s)", __PRETTY_FUNCTION__, mutex_ptr, err, strerror(err)); + Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_lock(%p) => err = %i (%s)", __PRETTY_FUNCTION__, &m_mutex, err, strerror(err)); assert(err == 0); } #endif - DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err); + DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), &m_mutex, err); return err; } +//---------------------------------------------------------------------- +// Attempts to lock the mutex owned by this object without blocking. +// If the mutex is already locked, TryLock() will not block waiting +// for the mutex, but will return an error condition. +// +// RETURNS +// The error code from the pthread_mutex_trylock() function call. +//---------------------------------------------------------------------- int -Mutex::TryLock (pthread_mutex_t *mutex_ptr) +Mutex::TryLock(const char *failure_message) { #if ENABLE_MUTEX_ERROR_CHECKING - error_check_mutex (mutex_ptr, eMutexActionAssertInitialized); + error_check_mutex (&m_mutex, eMutexActionAssertInitialized); #endif - int err = ::pthread_mutex_trylock (mutex_ptr); - DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_trylock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err); + int err = ::pthread_mutex_trylock (&m_mutex); + DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_trylock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), &m_mutex, err); return err; } +//---------------------------------------------------------------------- +// If the current thread holds the lock on the owned mutex, then +// Unlock() will unlock the mutex. Calling Unlock() on this object +// that the calling thread does not hold will result in undefined +// behavior. +// +// RETURNS +// The error code from the pthread_mutex_unlock() function call. +//---------------------------------------------------------------------- int -Mutex::Unlock (pthread_mutex_t *mutex_ptr) +Mutex::Unlock() { #if ENABLE_MUTEX_ERROR_CHECKING - error_check_mutex (mutex_ptr, eMutexActionAssertInitialized); + error_check_mutex (&m_mutex, eMutexActionAssertInitialized); #endif - int err = ::pthread_mutex_unlock (mutex_ptr); + int err = ::pthread_mutex_unlock (&m_mutex); #if ENABLE_MUTEX_ERROR_CHECKING if (err) { - Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_unlock(%p) => err = %i (%s)", __PRETTY_FUNCTION__, mutex_ptr, err, strerror(err)); + Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_unlock(%p) => err = %i (%s)", __PRETTY_FUNCTION__, &m_mutex, err, strerror(err)); assert(err == 0); } #endif - DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_unlock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err); + DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_unlock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), &m_mutex, err); return err; } -//---------------------------------------------------------------------- -// Locks the mutex owned by this object, if the mutex is already -// locked, the calling thread will block until the mutex becomes -// available. -// -// RETURNS -// The error code from the pthread_mutex_lock() function call. -//---------------------------------------------------------------------- -int -Mutex::Lock() -{ - return Mutex::Lock (&m_mutex); -} - -//---------------------------------------------------------------------- -// Attempts to lock the mutex owned by this object without blocking. -// If the mutex is already locked, TryLock() will not block waiting -// for the mutex, but will return an error condition. -// -// RETURNS -// The error code from the pthread_mutex_trylock() function call. -//---------------------------------------------------------------------- +#ifdef LLDB_CONFIGURATION_DEBUG int -Mutex::TryLock() +TrackingMutex::Unlock () { - return Mutex::TryLock (&m_mutex); + if (!m_failure_message.empty()) + Host::SetCrashDescriptionWithFormat ("Unlocking lock (on thread %p) that thread: %p failed to get: %s", + pthread_self(), + m_thread_that_tried, + m_failure_message.c_str()); + assert (m_failure_message.empty()); + return Mutex::Unlock(); } +#endif + -//---------------------------------------------------------------------- -// If the current thread holds the lock on the owned mutex, then -// Unlock() will unlock the mutex. Calling Unlock() on this object -// that the calling thread does not hold will result in undefined -// behavior. -// -// RETURNS -// The error code from the pthread_mutex_unlock() function call. -//---------------------------------------------------------------------- -int -Mutex::Unlock() -{ - return Mutex::Unlock (&m_mutex); -} |

