|
|
|
@ -43,7 +43,7 @@
|
|
|
|
|
* define this if you want to protect mutexes against being locked twice by
|
|
|
|
|
* the same thread
|
|
|
|
|
*/
|
|
|
|
|
#undef PTHREAD_DEBUG
|
|
|
|
|
#undef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
|
|
|
|
|
namespace Arts {
|
|
|
|
|
|
|
|
|
@ -58,7 +58,7 @@ protected:
|
|
|
|
|
friend class ThreadCondition_impl;
|
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
|
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
bool have_owner;
|
|
|
|
|
pthread_t owner;
|
|
|
|
|
#endif
|
|
|
|
@ -67,20 +67,20 @@ public:
|
|
|
|
|
Mutex_impl()
|
|
|
|
|
{
|
|
|
|
|
pthread_mutex_init(&mutex, 0);
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
have_owner = false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
void lock()
|
|
|
|
|
{
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
|
arts_assert(!have_owner || !pthread_equal(owner, self));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
arts_assert(!have_owner);
|
|
|
|
|
have_owner = true;
|
|
|
|
|
owner = self;
|
|
|
|
@ -88,14 +88,14 @@ public:
|
|
|
|
|
}
|
|
|
|
|
bool tryLock()
|
|
|
|
|
{
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
|
arts_assert(!have_owner || !pthread_equal(owner, self));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int result = pthread_mutex_trylock(&mutex);
|
|
|
|
|
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
if(result == 0)
|
|
|
|
|
{
|
|
|
|
|
arts_assert(!have_owner);
|
|
|
|
@ -107,7 +107,7 @@ public:
|
|
|
|
|
}
|
|
|
|
|
void unlock()
|
|
|
|
|
{
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
arts_assert(have_owner && pthread_equal(owner, pthread_self()));
|
|
|
|
|
have_owner = false;
|
|
|
|
|
#endif
|
|
|
|
@ -137,7 +137,7 @@ public:
|
|
|
|
|
if(!have_owner || !pthread_equal(owner, self))
|
|
|
|
|
{
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
arts_assert(count == 0);
|
|
|
|
|
arts_assert(!have_owner);
|
|
|
|
|
#endif
|
|
|
|
@ -155,7 +155,7 @@ public:
|
|
|
|
|
if(result != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
arts_assert(count == 0);
|
|
|
|
|
arts_assert(!have_owner);
|
|
|
|
|
#endif
|
|
|
|
@ -167,7 +167,7 @@ public:
|
|
|
|
|
}
|
|
|
|
|
void unlock()
|
|
|
|
|
{
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
arts_assert(have_owner && pthread_equal(owner, pthread_self()));
|
|
|
|
|
arts_assert(count > 0);
|
|
|
|
|
#endif
|
|
|
|
@ -234,7 +234,7 @@ public:
|
|
|
|
|
pthread_cond_broadcast(&cond);
|
|
|
|
|
}
|
|
|
|
|
void wait(Arts::Mutex_impl *mutex) {
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
|
arts_assert(((Mutex_impl *)mutex)->have_owner && pthread_equal(((Mutex_impl *)mutex)->owner, self));
|
|
|
|
|
((Mutex_impl *)mutex)->have_owner = false;
|
|
|
|
@ -242,7 +242,7 @@ public:
|
|
|
|
|
|
|
|
|
|
pthread_cond_wait(&cond, &((Mutex_impl*)mutex)->mutex);
|
|
|
|
|
|
|
|
|
|
#ifdef PTHREAD_DEBUG
|
|
|
|
|
#ifdef PTHREAD_PREVENT_MULTI_LOCK
|
|
|
|
|
arts_assert(!((Mutex_impl *)mutex)->have_owner);
|
|
|
|
|
((Mutex_impl *)mutex)->have_owner = true;
|
|
|
|
|
((Mutex_impl *)mutex)->owner = self;
|
|
|
|
|