diff options
author | OBATA Akio <obache@wizdas.com> | 2024-07-08 16:41:40 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2024-07-09 11:43:05 +0900 |
commit | 75cb448c029096cabeecc0b259e1b8a3bd978e32 (patch) | |
tree | 03b40bb1e8c3d7dab5ee8dad6514a22ddb1460f6 | |
parent | 939fcae97093ce7a1c37645eb73721adcd01d454 (diff) | |
download | arts-75cb448c029096cabeecc0b259e1b8a3bd978e32.tar.gz arts-75cb448c029096cabeecc0b259e1b8a3bd978e32.zip |
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>
(cherry picked from commit cf0025c38aeee4502d5c50d8350045830abb7706)
-rw-r--r-- | mcop_mt/threads_posix.cpp | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/mcop_mt/threads_posix.cpp b/mcop_mt/threads_posix.cpp index 6fa953c..06920b1 100644 --- a/mcop_mt/threads_posix.cpp +++ b/mcop_mt/threads_posix.cpp @@ -59,6 +59,7 @@ protected: pthread_mutex_t mutex; #ifdef PTHREAD_DEBUG + bool have_owner; pthread_t owner; #endif @@ -67,20 +68,21 @@ public: { pthread_mutex_init(&mutex, 0); #ifdef PTHREAD_DEBUG - owner = 0; + have_owner = false; #endif } void lock() { #ifdef PTHREAD_DEBUG pthread_t self = pthread_self(); - arts_assert(owner != self); + arts_assert(!have_owner || !pthread_equal(owner, self)); #endif pthread_mutex_lock(&mutex); #ifdef PTHREAD_DEBUG - arts_assert(!owner); + arts_assert(!have_owner); + have_owner = true; owner = self; #endif } @@ -88,7 +90,7 @@ public: { #ifdef PTHREAD_DEBUG pthread_t self = pthread_self(); - arts_assert(owner != self); + arts_assert(!have_owner || !pthread_equal(owner, self)); #endif int result = pthread_mutex_trylock(&mutex); @@ -96,7 +98,8 @@ public: #ifdef PTHREAD_DEBUG if(result == 0) { - arts_assert(!owner); + arts_assert(!have_owner); + have_owner = true; owner = self; } #endif @@ -105,8 +108,8 @@ public: void unlock() { #ifdef PTHREAD_DEBUG - arts_assert(owner == pthread_self()); - owner = 0; + arts_assert(have_owner && pthread_equal(owner, pthread_self())); + have_owner = false; #endif pthread_mutex_unlock(&mutex); @@ -117,6 +120,7 @@ class RecMutex_impl : public Arts::Mutex_impl { protected: friend class ThreadCondition_impl; pthread_mutex_t mutex; + bool have_owner; pthread_t owner; int count; @@ -124,19 +128,20 @@ public: RecMutex_impl() { pthread_mutex_init(&mutex, 0); - owner = 0; + have_owner = false; count = 0; } void lock() { pthread_t self = pthread_self(); - if(owner != self) + if(!have_owner || !pthread_equal(owner, self)) { pthread_mutex_lock(&mutex); #ifdef PTHREAD_DEBUG arts_assert(count == 0); - arts_assert(!owner); + arts_assert(!have_owner); #endif + have_owner = true; owner = self; } count++; @@ -144,7 +149,7 @@ public: bool tryLock() { pthread_t self = pthread_self(); - if(owner != self) + if(!have_owner || !pthread_equal(owner, self)) { int result = pthread_mutex_trylock(&mutex); if(result != 0) @@ -152,8 +157,9 @@ public: #ifdef PTHREAD_DEBUG arts_assert(count == 0); - arts_assert(!owner); + arts_assert(!have_owner); #endif + have_owner = true; owner = self; } count++; @@ -162,14 +168,14 @@ public: void unlock() { #ifdef PTHREAD_DEBUG - arts_assert(owner == pthread_self()); + arts_assert(have_owner && pthread_equal(owner, pthread_self())); arts_assert(count > 0); #endif count--; if(count == 0) { - owner = 0; + have_owner = false; pthread_mutex_unlock(&mutex); } } @@ -230,14 +236,15 @@ public: void wait(Arts::Mutex_impl *mutex) { #ifdef PTHREAD_DEBUG pthread_t self = pthread_self(); - arts_assert(((Mutex_impl *)mutex)->owner == self); - ((Mutex_impl *)mutex)->owner = 0; + arts_assert(((Mutex_impl *)mutex)->have_owner && pthread_equal(((Mutex_impl *)mutex)->owner, self)); + ((Mutex_impl *)mutex)->have_owner = false; #endif pthread_cond_wait(&cond, &((Mutex_impl*)mutex)->mutex); #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; #endif } |