From fd27857e31dc6bd7b78ddddbb6ef3f1162ee0b88 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Fri, 27 Oct 2006 14:00:20 +0000 Subject: 2006-10-27 Havoc Pennington * dbus/dbus-sysdeps-pthread.c: make the "count" and "holder" variables volatile, suggested by Thiago. Document struct fields. (PTHREAD_CHECK): remove pthread error checking if assertions are disabled, should reduce the no-assertions case to the bare minimum code. --- dbus/dbus-sysdeps-pthread.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'dbus') diff --git a/dbus/dbus-sysdeps-pthread.c b/dbus/dbus-sysdeps-pthread.c index 9979283b..fc2acd40 100644 --- a/dbus/dbus-sysdeps-pthread.c +++ b/dbus/dbus-sysdeps-pthread.c @@ -30,13 +30,16 @@ #include typedef struct { - pthread_mutex_t lock; - int count; - pthread_t holder; + pthread_mutex_t lock; /**< lock protecting count field */ + volatile int count; /**< count of how many times lock holder has recursively locked */ + volatile pthread_t holder; /**< holder of the lock if count >0, + valid but arbitrary thread if count + has ever been >0, uninitialized memory + if count has never been >0 */ } DBusMutexPThread; typedef struct { - pthread_cond_t cond; + pthread_cond_t cond; /**< the condition */ } DBusCondVarPThread; #define DBUS_MUTEX(m) ((DBusMutex*) m) @@ -46,6 +49,10 @@ typedef struct { #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c) +#ifdef DBUS_DISABLE_ASSERT +#define PTHREAD_CHECK(func_name, result_or_call) do { \ + do { (result_or_call) } while (0) +#else #define PTHREAD_CHECK(func_name, result_or_call) do { \ int tmp = (result_or_call); \ if (tmp != 0) { \ @@ -53,7 +60,8 @@ typedef struct { func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \ } \ } while (0) - +#endif /* !DBUS_DISABLE_ASSERT */ + static DBusMutex* _dbus_pthread_mutex_new (void) { @@ -118,8 +126,9 @@ _dbus_pthread_mutex_lock (DBusMutex *mutex) PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock)); - /* We now have the lock. Count must be 0 since it was before when we - * did not have the lock, and we have not changed it in this thread. + /* We now have the lock. Count must be 0 since it must be 0 when + * the lock is released by another thread, and we just now got + * the lock. */ _dbus_assert (pmutex->count == 0); @@ -130,13 +139,14 @@ _dbus_pthread_mutex_lock (DBusMutex *mutex) { /* We know someone had the lock, possibly us. Thus * pmutex->holder is not pointing to junk, though it may not be - * the lock holder anymore if the lock holder is not us. - * If the lock holder is us, then we definitely have the lock. + * the lock holder anymore if the lock holder is not us. If the + * lock holder is us, then we definitely have the lock. */ if (pthread_equal (pmutex->holder, self)) { /* We already have the lock. */ + _dbus_assert (pmutex->count > 0); } else { -- cgit