tools: fix to use `pthread_t` for Thread ID

Thread ID is opaque type pthread_t, it may not be compatible with integer,
and may integer with valid id `0`.
Change to store mutex owner thread ID as `pthread_t` type with valid flag
and compare with `pthread_equal()`,

and don't try to print it.

Signed-off-by: OBATA Akio <obache@wizdas.com>
pull/59/head
OBATA Akio 2 years ago
parent 1c1b280b1f
commit 7e4fc64d69

@ -72,7 +72,6 @@ typedef pthread_mutex_t Q_MUTEX_T;
#include "qmutex_p.h" #include "qmutex_p.h"
#include <errno.h> #include <errno.h>
#include <stdint.h>
#include <string.h> #include <string.h>
// Private class declarations // Private class declarations
@ -106,7 +105,8 @@ public:
int level(); int level();
int count; int count;
unsigned long owner; pthread_t owner;
bool is_owned;
pthread_mutex_t handle2; pthread_mutex_t handle2;
}; };
#endif // !Q_RECURSIVE_MUTEX_TYPE #endif // !Q_RECURSIVE_MUTEX_TYPE
@ -207,7 +207,7 @@ int TQRealMutexPrivate::level()
#ifndef Q_RECURSIVE_MUTEX_TYPE #ifndef Q_RECURSIVE_MUTEX_TYPE
TQRecursiveMutexPrivate::TQRecursiveMutexPrivate() TQRecursiveMutexPrivate::TQRecursiveMutexPrivate()
: count(0), owner(0) : count(0), is_owned(false)
{ {
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr); pthread_mutexattr_init(&attr);
@ -244,14 +244,15 @@ void TQRecursiveMutexPrivate::lock()
{ {
pthread_mutex_lock(&handle2); pthread_mutex_lock(&handle2);
if (count > 0 && owner == (unsigned long) pthread_self()) { if (count > 0 && pthread_equal(owner, pthread_self()) ) {
count++; count++;
} else { } else {
pthread_mutex_unlock(&handle2); pthread_mutex_unlock(&handle2);
pthread_mutex_lock(&handle); pthread_mutex_lock(&handle);
pthread_mutex_lock(&handle2); pthread_mutex_lock(&handle2);
count = 1; count = 1;
owner = (unsigned long) pthread_self(); owner = pthread_self();
is_owned = true;
} }
pthread_mutex_unlock(&handle2); pthread_mutex_unlock(&handle2);
@ -261,7 +262,7 @@ void TQRecursiveMutexPrivate::unlock()
{ {
pthread_mutex_lock(&handle2); pthread_mutex_lock(&handle2);
if (owner == (unsigned long) pthread_self()) { if ( is_owned && pthread_equal(owner, pthread_self()) ) {
// do nothing if the count is already 0... to reflect the behaviour described // do nothing if the count is already 0... to reflect the behaviour described
// in the docs // in the docs
if (count && (--count) < 1) { if (count && (--count) < 1) {
@ -271,8 +272,6 @@ void TQRecursiveMutexPrivate::unlock()
} else { } else {
#ifdef QT_CHECK_RANGE #ifdef QT_CHECK_RANGE
tqWarning("TQMutex::unlock: unlock from different thread than locker"); tqWarning("TQMutex::unlock: unlock from different thread than locker");
tqWarning(" was locked by %d, unlock attempt from %lu",
(int)owner, (uintptr_t)pthread_self());
#endif #endif
} }
@ -309,7 +308,7 @@ bool TQRecursiveMutexPrivate::trylock()
pthread_mutex_lock(&handle2); pthread_mutex_lock(&handle2);
if ( count > 0 && owner == (unsigned long) pthread_self() ) { if ( count > 0 && pthread_equal(owner, pthread_self()) ) {
count++; count++;
} else { } else {
int code = pthread_mutex_trylock(&handle); int code = pthread_mutex_trylock(&handle);
@ -323,7 +322,8 @@ bool TQRecursiveMutexPrivate::trylock()
ret = FALSE; ret = FALSE;
} else { } else {
count = 1; count = 1;
owner = (unsigned long) pthread_self(); owner = pthread_self();
is_owned = true;
} }
} }

Loading…
Cancel
Save