From cf0025c38aeee4502d5c50d8350045830abb7706 Mon Sep 17 00:00:00 2001 From: OBATA Akio Date: Mon, 8 Jul 2024 16:41:40 +0900 Subject: [PATCH] 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 --- mcop_mt/threads_posix.cpp | 41 +++++++++++++++++++++++---------------- 1 file 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 }