2010-06-09 00:52:24 +08:00
|
|
|
//===-- Mutex.cpp -----------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Host/Mutex.h"
|
2012-01-28 02:29:47 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
#if 0
|
2010-06-14 12:30:13 +08:00
|
|
|
// This logging is way too verbose to enable even for a log channel.
|
|
|
|
// This logging can be enabled by changing the "#if 0", but should be
|
|
|
|
// reverted prior to checking in.
|
|
|
|
#include <cstdio>
|
2010-06-09 00:52:24 +08:00
|
|
|
#define DEBUG_LOG(fmt, ...) printf(fmt, ## __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DEBUG_LOG(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
2012-01-28 02:29:47 +08:00
|
|
|
// Enable extra mutex error checking
|
|
|
|
#ifdef LLDB_CONFIGURATION_DEBUG
|
|
|
|
#define ENABLE_MUTEX_ERROR_CHECKING 1
|
|
|
|
#endif
|
|
|
|
|
2012-04-13 08:21:53 +08:00
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
enum MutexAction
|
|
|
|
{
|
|
|
|
eMutexActionInitialized,
|
|
|
|
eMutexActionDestroyed,
|
|
|
|
eMutexActionAssertInitialized
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
error_check_mutex (pthread_mutex_t *m, MutexAction action)
|
|
|
|
{
|
|
|
|
typedef std::set<pthread_mutex_t *> mutex_set;
|
|
|
|
static pthread_mutex_t g_mutex_set_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
mutex_set g_initialized_mutex_set;
|
|
|
|
mutex_set g_destroyed_mutex_set;
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
int err;
|
|
|
|
// Manually call lock so we don't to any of this error checking
|
|
|
|
err = ::pthread_mutex_lock (&g_mutex_set_mutex);
|
|
|
|
assert(err == 0);
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case eMutexActionInitialized:
|
|
|
|
// Make sure this isn't already in our initialized mutex set...
|
|
|
|
assert (g_initialized_mutex_set.find(m) == g_initialized_mutex_set.end());
|
|
|
|
// Remove this from the destroyed set in case it was ever in there
|
|
|
|
g_destroyed_mutex_set.erase(m);
|
|
|
|
// Add the mutex to the initialized set
|
|
|
|
g_initialized_mutex_set.insert(m);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eMutexActionDestroyed:
|
|
|
|
// Make sure this isn't already in our destroyed mutex set...
|
|
|
|
assert (g_destroyed_mutex_set.find(m) == g_destroyed_mutex_set.end());
|
|
|
|
// Remove this from the initialized so we can put it into the destroyed set
|
|
|
|
g_initialized_mutex_set.erase(m);
|
|
|
|
// Add the mutex to the destroyed set
|
|
|
|
g_destroyed_mutex_set.insert(m);
|
|
|
|
break;
|
|
|
|
case eMutexActionAssertInitialized:
|
|
|
|
// This function will return true if "m" is in the initialized mutex set
|
|
|
|
success = g_initialized_mutex_set.find(m) != g_initialized_mutex_set.end();
|
2012-04-14 07:28:33 +08:00
|
|
|
assert (success);
|
2012-04-13 08:21:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Manually call unlock so we don't to any of this error checking
|
|
|
|
err = ::pthread_mutex_unlock (&g_mutex_set_mutex);
|
|
|
|
assert(err == 0);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Default constructor.
|
|
|
|
//
|
|
|
|
// This will create a scoped mutex locking object that doesn't have
|
|
|
|
// a mutex to lock. One will need to be provided using the Reset()
|
|
|
|
// method.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Locker::Locker () :
|
|
|
|
m_mutex_ptr(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Constructor with a Mutex object.
|
|
|
|
//
|
|
|
|
// This will create a scoped mutex locking object that extracts the
|
|
|
|
// mutex owned by "m" and locks it.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Locker::Locker (Mutex& m) :
|
2012-04-11 08:24:49 +08:00
|
|
|
m_mutex_ptr(NULL)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2012-04-11 08:24:49 +08:00
|
|
|
Lock (m.GetMutex());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Constructor with a Mutex object pointer.
|
|
|
|
//
|
|
|
|
// This will create a scoped mutex locking object that extracts the
|
|
|
|
// mutex owned by "m" and locks it.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Locker::Locker (Mutex* m) :
|
2012-04-11 08:24:49 +08:00
|
|
|
m_mutex_ptr(NULL)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2012-04-11 08:24:49 +08:00
|
|
|
if (m)
|
|
|
|
Lock (m->GetMutex());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Constructor with a raw pthread mutex object pointer.
|
|
|
|
//
|
|
|
|
// This will create a scoped mutex locking object that locks "mutex"
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Locker::Locker (pthread_mutex_t *mutex_ptr) :
|
2012-04-11 08:24:49 +08:00
|
|
|
m_mutex_ptr (NULL)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
if (m_mutex_ptr)
|
2012-04-11 08:24:49 +08:00
|
|
|
Lock (m_mutex_ptr);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2011-01-09 04:28:42 +08:00
|
|
|
// Destructor
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
// Unlocks any owned mutex object (if it is valid).
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Locker::~Locker ()
|
|
|
|
{
|
2012-04-11 08:24:49 +08:00
|
|
|
Unlock();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Unlock the current mutex in this object (if this owns a valid
|
|
|
|
// mutex) and lock the new "mutex" object if it is non-NULL.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2012-04-11 08:24:49 +08:00
|
|
|
Mutex::Locker::Lock (pthread_mutex_t *mutex_ptr)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-09-04 03:15:43 +08:00
|
|
|
// We already have this mutex locked or both are NULL...
|
|
|
|
if (m_mutex_ptr == mutex_ptr)
|
|
|
|
return;
|
|
|
|
|
2012-04-11 08:24:49 +08:00
|
|
|
Unlock ();
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2012-04-11 08:24:49 +08:00
|
|
|
if (mutex_ptr)
|
|
|
|
{
|
|
|
|
m_mutex_ptr = mutex_ptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
Mutex::Lock (m_mutex_ptr);
|
2012-04-11 08:24:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Mutex::Locker::Unlock ()
|
|
|
|
{
|
|
|
|
if (m_mutex_ptr)
|
|
|
|
{
|
|
|
|
Mutex::Unlock (m_mutex_ptr);
|
|
|
|
m_mutex_ptr = NULL;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Mutex::Locker::TryLock (pthread_mutex_t *mutex_ptr)
|
|
|
|
{
|
2010-09-04 03:15:43 +08:00
|
|
|
// We already have this mutex locked!
|
|
|
|
if (m_mutex_ptr == mutex_ptr)
|
2012-04-11 08:24:49 +08:00
|
|
|
return m_mutex_ptr != NULL;
|
2010-09-04 03:15:43 +08:00
|
|
|
|
2012-04-11 08:24:49 +08:00
|
|
|
Unlock ();
|
2010-09-04 03:15:43 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (mutex_ptr)
|
|
|
|
{
|
|
|
|
if (Mutex::TryLock (mutex_ptr) == 0)
|
|
|
|
m_mutex_ptr = mutex_ptr;
|
|
|
|
}
|
|
|
|
return m_mutex_ptr != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Default constructor.
|
|
|
|
//
|
|
|
|
// Creates a pthread mutex with no attributes.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Mutex () :
|
|
|
|
m_mutex()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
err = ::pthread_mutex_init (&m_mutex, NULL);
|
2012-04-13 08:21:53 +08:00
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
|
|
|
if (err == 0)
|
|
|
|
error_check_mutex (&m_mutex, eMutexActionInitialized);
|
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
assert(err == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Default constructor.
|
|
|
|
//
|
|
|
|
// Creates a pthread mutex with "type" as the mutex type.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Mutex (Mutex::Type type) :
|
|
|
|
m_mutex()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
::pthread_mutexattr_t attr;
|
|
|
|
err = ::pthread_mutexattr_init (&attr);
|
|
|
|
assert(err == 0);
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case eMutexTypeNormal:
|
2012-01-28 02:29:47 +08:00
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
|
|
|
err = ::pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
|
|
|
|
#else
|
2010-06-09 00:52:24 +08:00
|
|
|
err = ::pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL);
|
2012-01-28 02:29:47 +08:00
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eMutexTypeRecursive:
|
|
|
|
err = ::pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(err == 0);
|
|
|
|
err = ::pthread_mutex_init (&m_mutex, &attr);
|
2012-04-13 08:21:53 +08:00
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
|
|
|
if (err == 0)
|
|
|
|
error_check_mutex (&m_mutex, eMutexActionInitialized);
|
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
assert(err == 0);
|
|
|
|
err = ::pthread_mutexattr_destroy (&attr);
|
|
|
|
assert(err == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Destructor.
|
|
|
|
//
|
|
|
|
// Destroys the mutex owned by this object.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::~Mutex()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
err = ::pthread_mutex_destroy (&m_mutex);
|
2012-01-28 02:29:47 +08:00
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
2012-04-13 08:21:53 +08:00
|
|
|
if (err == 0)
|
|
|
|
error_check_mutex (&m_mutex, eMutexActionDestroyed);
|
|
|
|
else
|
2012-01-28 02:29:47 +08:00
|
|
|
{
|
|
|
|
Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_destroy() => err = %i (%s)", __PRETTY_FUNCTION__, err, strerror(err));
|
|
|
|
assert(err == 0);
|
|
|
|
}
|
|
|
|
memset (&m_mutex, '\xba', sizeof(m_mutex));
|
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Mutex get accessor.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
pthread_mutex_t *
|
|
|
|
Mutex::GetMutex()
|
|
|
|
{
|
|
|
|
return &m_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Mutex::Lock (pthread_mutex_t *mutex_ptr)
|
|
|
|
{
|
|
|
|
DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p)...\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr);
|
2012-04-13 08:21:53 +08:00
|
|
|
|
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
|
|
|
error_check_mutex (mutex_ptr, eMutexActionAssertInitialized);
|
|
|
|
#endif
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
int err = ::pthread_mutex_lock (mutex_ptr);
|
2012-04-13 08:21:53 +08:00
|
|
|
|
|
|
|
|
2012-01-28 02:29:47 +08:00
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
|
|
|
if (err)
|
|
|
|
{
|
2012-01-28 02:53:22 +08:00
|
|
|
Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_lock(%p) => err = %i (%s)", __PRETTY_FUNCTION__, mutex_ptr, err, strerror(err));
|
2012-01-28 02:29:47 +08:00
|
|
|
assert(err == 0);
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Mutex::TryLock (pthread_mutex_t *mutex_ptr)
|
|
|
|
{
|
2012-04-13 08:21:53 +08:00
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
|
|
|
error_check_mutex (mutex_ptr, eMutexActionAssertInitialized);
|
|
|
|
#endif
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
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);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Mutex::Unlock (pthread_mutex_t *mutex_ptr)
|
|
|
|
{
|
2012-04-13 08:21:53 +08:00
|
|
|
#if ENABLE_MUTEX_ERROR_CHECKING
|
|
|
|
error_check_mutex (mutex_ptr, eMutexActionAssertInitialized);
|
|
|
|
#endif
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
int err = ::pthread_mutex_unlock (mutex_ptr);
|
2012-04-13 08:21:53 +08:00
|
|
|
|
2012-01-28 02:29:47 +08:00
|
|
|
#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));
|
|
|
|
assert(err == 0);
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_unlock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, 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.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
Mutex::TryLock()
|
|
|
|
{
|
|
|
|
return Mutex::TryLock (&m_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// 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);
|
|
|
|
}
|