dbus/dbus dbus-sysdeps-pthread.c,1.1,1.2 dbus-threads.h,1.14,1.15

Havoc Pennington hp at kemper.freedesktop.org
Thu Oct 26 19:17:44 PDT 2006


Update of /cvs/dbus/dbus/dbus
In directory kemper:/tmp/cvs-serv28036/dbus

Modified Files:
	dbus-sysdeps-pthread.c dbus-threads.h 
Log Message:
2006-10-26  Havoc Pennington  <hp at redhat.com>

	* doc/dbus-specification.xml: clarify the UUID text slightly

	* dbus/dbus-sysdeps-pthread.c: check for and mostly abort on
	pthread errors. Add DBusMutexPThread and DBusCondVarPThread 
	in preparation for being able to extend them for e.g. recursive
	mutexes.



Index: dbus-sysdeps-pthread.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-sysdeps-pthread.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- dbus-sysdeps-pthread.c	27 Oct 2006 01:09:24 -0000	1.1
+++ dbus-sysdeps-pthread.c	27 Oct 2006 02:17:42 -0000	1.2
@@ -27,81 +27,138 @@
 
 #include <sys/time.h>
 #include <pthread.h>
+#include <string.h>
+
+typedef struct {
+  pthread_mutex_t lock;
+} DBusMutexPThread;
+
+typedef struct {
+  pthread_cond_t cond;
+} DBusCondVarPThread;
+
+#define DBUS_MUTEX(m)         ((DBusMutex*) m)
+#define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
+
+#define DBUS_COND_VAR(c)         ((DBusCondVar*) c)
+#define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
+
 
+#define PTHREAD_CHECK(func_name, result_or_call) do {                                  \
+    int tmp = (result_or_call);                                                        \
+    if (tmp != 0) {                                                                    \
+      _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n",        \
+                               func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME);    \
+    }                                                                                  \
+} while (0)
+            
 static DBusMutex*
 _dbus_pthread_mutex_new (void)
 {
-  pthread_mutex_t *retval;
+  DBusMutexPThread *pmutex;
+  int result;
   
-  retval = dbus_new (pthread_mutex_t, 1);
-  if (retval == NULL)
+  pmutex = dbus_new (DBusMutexPThread, 1);
+  if (pmutex == NULL)
     return NULL;
-  
-  if (pthread_mutex_init (retval, NULL))
+
+  result = pthread_mutex_init (&pmutex->lock, NULL);
+
+  if (result == ENOMEM || result == EAGAIN)
     {
-      dbus_free (retval);
+      dbus_free (pmutex);
       return NULL;
     }
+  else
+    {
+      PTHREAD_CHECK ("pthread_mutex_init", result);
+    }
 
-  return (DBusMutex *) retval;
+  return DBUS_MUTEX (pmutex);
 }
 
 static void
 _dbus_pthread_mutex_free (DBusMutex *mutex)
 {
-  pthread_mutex_destroy ((pthread_mutex_t *) mutex);
-  dbus_free (mutex);
+  DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
+  
+  PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->lock));
+
+  dbus_free (pmutex);
 }
 
 static dbus_bool_t
 _dbus_pthread_mutex_lock (DBusMutex *mutex)
 {
-  return pthread_mutex_lock ((pthread_mutex_t *) mutex) == 0;
+  DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
+
+  PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
+
+  return TRUE;
 }
 
 static dbus_bool_t
 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
 {
-  return pthread_mutex_unlock ((pthread_mutex_t *) mutex) == 0;
+  DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
+
+  PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
+
+  return TRUE;
 }
 
 static DBusCondVar *
 _dbus_pthread_condvar_new (void)
 {
-  pthread_cond_t *retval;
+  DBusCondVarPThread *pcond;
+  int result;
   
-  retval = dbus_new (pthread_cond_t, 1);
-  if (retval == NULL)
+  pcond = dbus_new (DBusCondVarPThread, 1);
+  if (pcond == NULL)
     return NULL;
-  
-  if (pthread_cond_init (retval, NULL))
+
+  result = pthread_cond_init (&pcond->cond, NULL);
+
+  if (result == EAGAIN || result == ENOMEM)
     {
-      dbus_free (retval);
+      dbus_free (pcond);
       return NULL;
     }
-  return (DBusCondVar *) retval;
+  else
+    {
+      PTHREAD_CHECK ("pthread_cond_init", result);
+    }
+  
+  return DBUS_COND_VAR (pcond);
 }
 
 static void
 _dbus_pthread_condvar_free (DBusCondVar *cond)
-{
-  pthread_cond_destroy ((pthread_cond_t *) cond);
-  dbus_free (cond);
+{  
+  DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
+  
+  PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
+
+  dbus_free (pcond);
 }
 
 static void
 _dbus_pthread_condvar_wait (DBusCondVar *cond,
-                    DBusMutex   *mutex)
+                            DBusMutex   *mutex)
 {
-  pthread_cond_wait ((pthread_cond_t *)cond,
-		     (pthread_mutex_t *) mutex);
+  DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
+  DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
+  
+  PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
 }
 
 static dbus_bool_t
 _dbus_pthread_condvar_wait_timeout (DBusCondVar               *cond,
-				     DBusMutex                 *mutex,
-				     int                        timeout_milliseconds)
+                                    DBusMutex                 *mutex,
+                                    int                        timeout_milliseconds)
 {
+  DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
+  DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
   struct timeval time_now;
   struct timespec end_time;
   int result;
@@ -115,10 +172,13 @@
       end_time.tv_sec += 1;
       end_time.tv_nsec -= 1000*1000*1000;
     }
-  
-  result = pthread_cond_timedwait ((pthread_cond_t *) cond,
-				   (pthread_mutex_t *) mutex,
-				   &end_time);
+
+  result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
+
+  if (result != ETIMEDOUT)
+    {
+      PTHREAD_CHECK ("pthread_cond_timedwait", result);
+    }
   
   /* return true if we did not time out */
   return result != ETIMEDOUT;
@@ -127,13 +187,17 @@
 static void
 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
 {
-  pthread_cond_signal ((pthread_cond_t *)cond);
+  DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
+
+  PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
 }
 
 static void
 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
 {
-  pthread_cond_broadcast ((pthread_cond_t *)cond);
+  DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
+  
+  PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
 }
 
 static const DBusThreadFunctions pthread_functions =

Index: dbus-threads.h
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-threads.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- dbus-threads.h	27 Oct 2006 01:09:24 -0000	1.14
+++ dbus-threads.h	27 Oct 2006 02:17:42 -0000	1.15
@@ -51,24 +51,28 @@
 /** Deprecated, provide DBusRecursiveMutexUnlockFunction instead. Return value is unlock success, but gets ignored in practice. */
 typedef dbus_bool_t (* DBusMutexUnlockFunction) (DBusMutex *mutex);
 
-/** Creates a new recursively-lockable mutex, or returns #NULL if not enough memory.
- * Found in #DBusThreadFunctions. Do not just use PTHREAD_MUTEX_RECURSIVE for this, because
- * it does not save/restore the recursion count when waiting on a condition. libdbus
- * requires the Java-style behavior where the mutex is fully unlocked to wait on
- * a condition.
+/** Creates a new recursively-lockable mutex, or returns #NULL if not
+ * enough memory.  Can only fail due to lack of memory.  Found in
+ * #DBusThreadFunctions. Do not just use PTHREAD_MUTEX_RECURSIVE for
+ * this, because it does not save/restore the recursion count when
+ * waiting on a condition. libdbus requires the Java-style behavior
+ * where the mutex is fully unlocked to wait on a condition.
  */
 typedef DBusMutex*  (* DBusRecursiveMutexNewFunction)    (void);
 /** Frees a recursively-lockable mutex.  Found in #DBusThreadFunctions.
  */
 typedef void        (* DBusRecursiveMutexFreeFunction)   (DBusMutex *mutex);
 /** Locks a recursively-lockable mutex.  Found in #DBusThreadFunctions.
+ * Can only fail due to lack of memory.
  */
 typedef void        (* DBusRecursiveMutexLockFunction)   (DBusMutex *mutex);
 /** Unlocks a recursively-lockable mutex.  Found in #DBusThreadFunctions.
+ * Can only fail due to lack of memory.
  */
 typedef void        (* DBusRecursiveMutexUnlockFunction) (DBusMutex *mutex);
 
 /** Creates a new condition variable.  Found in #DBusThreadFunctions.
+ * Can only fail (returning #NULL) due to lack of memory.
  */
 typedef DBusCondVar*  (* DBusCondVarNewFunction)         (void);
 /** Frees a condition variable.  Found in #DBusThreadFunctions.
@@ -82,6 +86,8 @@
  * condition variables (does not save/restore the recursion count) so
  * don't try using simply pthread_cond_wait() and a
  * PTHREAD_MUTEX_RECURSIVE to implement this, it won't work right.
+ *
+ * Has no error conditions. Must succeed if it returns.
  */
 typedef void          (* DBusCondVarWaitFunction)        (DBusCondVar *cond,
 							  DBusMutex   *mutex);
@@ -89,14 +95,21 @@
 /** Waits on a condition variable with a timeout.  Found in
  *  #DBusThreadFunctions. Returns #TRUE if the wait did not
  *  time out, and #FALSE if it did.
+ *
+ * Has no error conditions. Must succeed if it returns. 
  */
 typedef dbus_bool_t   (* DBusCondVarWaitTimeoutFunction) (DBusCondVar *cond,
 							  DBusMutex   *mutex,
 							  int          timeout_milliseconds);
 /** Wakes one waiting thread on a condition variable.  Found in #DBusThreadFunctions.
+ *
+ * Has no error conditions. Must succeed if it returns.
  */
 typedef void          (* DBusCondVarWakeOneFunction) (DBusCondVar *cond);
+
 /** Wakes all waiting threads on a condition variable.  Found in #DBusThreadFunctions.
+ *
+ * Has no error conditions. Must succeed if it returns.
  */
 typedef void          (* DBusCondVarWakeAllFunction) (DBusCondVar *cond);
 



More information about the dbus-commit mailing list