From 999cebcb5df0269d24d65db579a7c78a94053c2e Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Mon, 27 Mar 2023 22:07:16 -0500 Subject: Fix level when using pthreads recursive mutex When using the pthreads recursive mutex, the level method was only able to return either zero or one, but a recursive mutex can be locked more than once. Additionally, the way this detected if the mutex was already locked was to test whether it can be locked again. If testing from the thread that is currently holding the lock, this locking attempt always succeeds, and therefore determines that the lock is _not_ held at all. This is especially bad, as this operation only makes sense to perform from the thread that holds the lock -- if any thread not holding the lock queries the number of times the lock is held, that answer can change before the thread can used it. Signed-off-by: Bobby Bingham --- src/tools/qmutex_unix.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/tools/qmutex_unix.cpp b/src/tools/qmutex_unix.cpp index dae1533c4..5cb037a18 100644 --- a/src/tools/qmutex_unix.cpp +++ b/src/tools/qmutex_unix.cpp @@ -88,6 +88,7 @@ public: int level(); bool recursive; + int count; }; #ifndef Q_RECURSIVE_MUTEX_TYPE @@ -127,7 +128,7 @@ TQMutexPrivate::~TQMutexPrivate() // real mutex class TQRealMutexPrivate::TQRealMutexPrivate(bool recurs) - : recursive(recurs) + : count(0), recursive(recurs) { pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); @@ -146,36 +147,31 @@ void TQRealMutexPrivate::lock() { int ret = pthread_mutex_lock(&handle); + if (!ret) { + count++; + } else { #ifdef QT_CHECK_RANGE - if (ret) tqWarning("Mutex lock failure: %s", strerror(ret)); #endif + } } void TQRealMutexPrivate::unlock() { + count--; int ret = pthread_mutex_unlock(&handle); + if (ret) { + count++; #ifdef QT_CHECK_RANGE - if (ret) tqWarning("Mutex unlock failure: %s", strerror(ret)); #endif + } } bool TQRealMutexPrivate::locked() { - int ret = pthread_mutex_trylock(&handle); - - if (ret == EBUSY) { - return TRUE; - } else if (ret) { -#ifdef QT_CHECK_RANGE - tqWarning("Mutex locktest failure: %s", strerror(ret)); -#endif - } else - pthread_mutex_unlock(&handle); - - return FALSE; + return count > 0; } bool TQRealMutexPrivate::trylock() @@ -201,7 +197,7 @@ int TQRealMutexPrivate::type() const int TQRealMutexPrivate::level() { - return locked(); + return count; } -- cgit v1.2.1