Fix usage of `pthread_t`

`pthread_t` is opaque type.
Change to compare with `pthread_equal()` and introduce validate flag.

Signed-off-by: OBATA Akio <obache@wizdas.com>
pull/21/head
OBATA Akio 5 months ago
parent 343be2cf87
commit cf0025c38a

@ -59,6 +59,7 @@ protected:
pthread_mutex_t mutex; pthread_mutex_t mutex;
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
bool have_owner;
pthread_t owner; pthread_t owner;
#endif #endif
@ -67,20 +68,21 @@ public:
{ {
pthread_mutex_init(&mutex, 0); pthread_mutex_init(&mutex, 0);
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
owner = 0; have_owner = false;
#endif #endif
} }
void lock() void lock()
{ {
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
pthread_t self = pthread_self(); pthread_t self = pthread_self();
arts_assert(owner != self); arts_assert(!have_owner || !pthread_equal(owner, self));
#endif #endif
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
arts_assert(!owner); arts_assert(!have_owner);
have_owner = true;
owner = self; owner = self;
#endif #endif
} }
@ -88,7 +90,7 @@ public:
{ {
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
pthread_t self = pthread_self(); pthread_t self = pthread_self();
arts_assert(owner != self); arts_assert(!have_owner || !pthread_equal(owner, self));
#endif #endif
int result = pthread_mutex_trylock(&mutex); int result = pthread_mutex_trylock(&mutex);
@ -96,7 +98,8 @@ public:
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
if(result == 0) if(result == 0)
{ {
arts_assert(!owner); arts_assert(!have_owner);
have_owner = true;
owner = self; owner = self;
} }
#endif #endif
@ -105,8 +108,8 @@ public:
void unlock() void unlock()
{ {
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
arts_assert(owner == pthread_self()); arts_assert(have_owner && pthread_equal(owner, pthread_self()));
owner = 0; have_owner = false;
#endif #endif
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
@ -117,6 +120,7 @@ class RecMutex_impl : public Arts::Mutex_impl {
protected: protected:
friend class ThreadCondition_impl; friend class ThreadCondition_impl;
pthread_mutex_t mutex; pthread_mutex_t mutex;
bool have_owner;
pthread_t owner; pthread_t owner;
int count; int count;
@ -124,19 +128,20 @@ public:
RecMutex_impl() RecMutex_impl()
{ {
pthread_mutex_init(&mutex, 0); pthread_mutex_init(&mutex, 0);
owner = 0; have_owner = false;
count = 0; count = 0;
} }
void lock() void lock()
{ {
pthread_t self = pthread_self(); pthread_t self = pthread_self();
if(owner != self) if(!have_owner || !pthread_equal(owner, self))
{ {
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
arts_assert(count == 0); arts_assert(count == 0);
arts_assert(!owner); arts_assert(!have_owner);
#endif #endif
have_owner = true;
owner = self; owner = self;
} }
count++; count++;
@ -144,7 +149,7 @@ public:
bool tryLock() bool tryLock()
{ {
pthread_t self = pthread_self(); pthread_t self = pthread_self();
if(owner != self) if(!have_owner || !pthread_equal(owner, self))
{ {
int result = pthread_mutex_trylock(&mutex); int result = pthread_mutex_trylock(&mutex);
if(result != 0) if(result != 0)
@ -152,8 +157,9 @@ public:
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
arts_assert(count == 0); arts_assert(count == 0);
arts_assert(!owner); arts_assert(!have_owner);
#endif #endif
have_owner = true;
owner = self; owner = self;
} }
count++; count++;
@ -162,14 +168,14 @@ public:
void unlock() void unlock()
{ {
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
arts_assert(owner == pthread_self()); arts_assert(have_owner && pthread_equal(owner, pthread_self()));
arts_assert(count > 0); arts_assert(count > 0);
#endif #endif
count--; count--;
if(count == 0) if(count == 0)
{ {
owner = 0; have_owner = false;
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }
} }
@ -230,14 +236,15 @@ public:
void wait(Arts::Mutex_impl *mutex) { void wait(Arts::Mutex_impl *mutex) {
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
pthread_t self = pthread_self(); pthread_t self = pthread_self();
arts_assert(((Mutex_impl *)mutex)->owner == self); arts_assert(((Mutex_impl *)mutex)->have_owner && pthread_equal(((Mutex_impl *)mutex)->owner, self));
((Mutex_impl *)mutex)->owner = 0; ((Mutex_impl *)mutex)->have_owner = false;
#endif #endif
pthread_cond_wait(&cond, &((Mutex_impl*)mutex)->mutex); pthread_cond_wait(&cond, &((Mutex_impl*)mutex)->mutex);
#ifdef PTHREAD_DEBUG #ifdef PTHREAD_DEBUG
arts_assert(((Mutex_impl *)mutex)->owner == 0); arts_assert(!((Mutex_impl *)mutex)->have_owner);
((Mutex_impl *)mutex)->have_owner = true;
((Mutex_impl *)mutex)->owner = self; ((Mutex_impl *)mutex)->owner = self;
#endif #endif
} }

Loading…
Cancel
Save