dbus-sysdeps-pthread.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-sysdeps-pthread.c Implements threads using pthreads (internal to libdbus)
00003  * 
00004  * Copyright (C) 2002, 2003, 2006  Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include "dbus-internals.h"
00025 #include "dbus-sysdeps.h"
00026 #include "dbus-threads.h"
00027 
00028 #include <sys/time.h>
00029 #include <pthread.h>
00030 #include <string.h>
00031 
00032 typedef struct {
00033   pthread_mutex_t lock; 
00034   volatile int count;   
00035   volatile pthread_t holder; 
00039 } DBusMutexPThread;
00040 
00041 typedef struct {
00042   pthread_cond_t cond; 
00043 } DBusCondVarPThread;
00044 
00045 #define DBUS_MUTEX(m)         ((DBusMutex*) m)
00046 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
00047 
00048 #define DBUS_COND_VAR(c)         ((DBusCondVar*) c)
00049 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
00050 
00051 
00052 #ifdef DBUS_DISABLE_ASSERT
00053 /* (tmp != 0) is a no-op usage to silence compiler */
00054 #define PTHREAD_CHECK(func_name, result_or_call)    \
00055     do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
00056 #else
00057 #define PTHREAD_CHECK(func_name, result_or_call) do {                                  \
00058     int tmp = (result_or_call);                                                        \
00059     if (tmp != 0) {                                                                    \
00060       _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n",        \
00061                                func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME);    \
00062     }                                                                                  \
00063 } while (0)
00064 #endif /* !DBUS_DISABLE_ASSERT */
00065 
00066 static DBusMutex*
00067 _dbus_pthread_mutex_new (void)
00068 {
00069   DBusMutexPThread *pmutex;
00070   int result;
00071   
00072   pmutex = dbus_new (DBusMutexPThread, 1);
00073   if (pmutex == NULL)
00074     return NULL;
00075 
00076   result = pthread_mutex_init (&pmutex->lock, NULL);
00077 
00078   if (result == ENOMEM || result == EAGAIN)
00079     {
00080       dbus_free (pmutex);
00081       return NULL;
00082     }
00083   else
00084     {
00085       PTHREAD_CHECK ("pthread_mutex_init", result);
00086     }
00087 
00088   /* Only written */
00089   pmutex->count = 0;
00090 
00091   /* There's no portable way to have a "null" pthread afaik so we
00092    * can't set pmutex->holder to anything sensible.  We only access it
00093    * once the lock is held (which means we've set it).
00094    */
00095   
00096   return DBUS_MUTEX (pmutex);
00097 }
00098 
00099 static void
00100 _dbus_pthread_mutex_free (DBusMutex *mutex)
00101 {
00102   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
00103 
00104   _dbus_assert (pmutex->count == 0);
00105   
00106   PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->lock));
00107 
00108   dbus_free (pmutex);
00109 }
00110 
00111 static void
00112 _dbus_pthread_mutex_lock (DBusMutex *mutex)
00113 {
00114   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
00115   pthread_t self = pthread_self ();
00116 
00117   /* If the count is > 0 then someone had the lock, maybe us. If it is
00118    * 0, then it might immediately change right after we read it,
00119    * but it will be changed by another thread; i.e. if we read 0,
00120    * we assume that this thread doesn't have the lock.
00121    *
00122    * Not 100% sure this is safe, but ... seems like it should be.
00123    */
00124   if (pmutex->count == 0)
00125     {
00126       /* We know we don't have the lock; someone may have the lock. */
00127       
00128       PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
00129 
00130       /* We now have the lock. Count must be 0 since it must be 0 when
00131        * the lock is released by another thread, and we just now got
00132        * the lock.
00133        */
00134       _dbus_assert (pmutex->count == 0);
00135       
00136       pmutex->holder = self;
00137       pmutex->count = 1;
00138     }
00139   else
00140     {
00141       /* We know someone had the lock, possibly us. Thus
00142        * pmutex->holder is not pointing to junk, though it may not be
00143        * the lock holder anymore if the lock holder is not us.  If the
00144        * lock holder is us, then we definitely have the lock.
00145        */
00146 
00147       if (pthread_equal (pmutex->holder, self))
00148         {
00149           /* We already have the lock. */
00150           _dbus_assert (pmutex->count > 0);
00151         }
00152       else
00153         {
00154           /* Wait for the lock */
00155           PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
00156           _dbus_assert (pmutex->count == 0);
00157         }
00158 
00159       pmutex->count += 1;
00160     }
00161 }
00162 
00163 static void
00164 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
00165 {
00166   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
00167 
00168   _dbus_assert (pmutex->count > 0);
00169   
00170   pmutex->count -= 1;
00171 
00172   if (pmutex->count == 0)
00173     PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
00174   
00175   /* We leave pmutex->holder set to ourselves, its content is undefined if count is 0 */
00176 }
00177 
00178 static DBusCondVar *
00179 _dbus_pthread_condvar_new (void)
00180 {
00181   DBusCondVarPThread *pcond;
00182   int result;
00183   
00184   pcond = dbus_new (DBusCondVarPThread, 1);
00185   if (pcond == NULL)
00186     return NULL;
00187 
00188   result = pthread_cond_init (&pcond->cond, NULL);
00189 
00190   if (result == EAGAIN || result == ENOMEM)
00191     {
00192       dbus_free (pcond);
00193       return NULL;
00194     }
00195   else
00196     {
00197       PTHREAD_CHECK ("pthread_cond_init", result);
00198     }
00199   
00200   return DBUS_COND_VAR (pcond);
00201 }
00202 
00203 static void
00204 _dbus_pthread_condvar_free (DBusCondVar *cond)
00205 {  
00206   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00207   
00208   PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
00209 
00210   dbus_free (pcond);
00211 }
00212 
00213 static void
00214 _dbus_pthread_condvar_wait (DBusCondVar *cond,
00215                             DBusMutex   *mutex)
00216 {
00217   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
00218   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00219   int old_count;
00220   
00221   _dbus_assert (pmutex->count > 0);
00222   _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));
00223 
00224   old_count = pmutex->count;
00225   pmutex->count = 0;
00226   PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
00227   _dbus_assert (pmutex->count == 0);
00228   pmutex->count = old_count;
00229 }
00230 
00231 static dbus_bool_t
00232 _dbus_pthread_condvar_wait_timeout (DBusCondVar               *cond,
00233                                     DBusMutex                 *mutex,
00234                                     int                        timeout_milliseconds)
00235 {
00236   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
00237   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00238   struct timeval time_now;
00239   struct timespec end_time;
00240   int result;
00241   int old_count;
00242   
00243   _dbus_assert (pmutex->count > 0);
00244   _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));  
00245   
00246   gettimeofday (&time_now, NULL);
00247   
00248   end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
00249   end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
00250   if (end_time.tv_nsec > 1000*1000*1000)
00251     {
00252       end_time.tv_sec += 1;
00253       end_time.tv_nsec -= 1000*1000*1000;
00254     }
00255 
00256   old_count = pmutex->count;
00257   pmutex->count = 0;
00258   result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
00259   
00260   if (result != ETIMEDOUT)
00261     {
00262       PTHREAD_CHECK ("pthread_cond_timedwait", result);
00263     }
00264 
00265   _dbus_assert (pmutex->count == 0);
00266   pmutex->count = old_count;
00267   
00268   /* return true if we did not time out */
00269   return result != ETIMEDOUT;
00270 }
00271 
00272 static void
00273 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
00274 {
00275   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00276 
00277   PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
00278 }
00279 
00280 static void
00281 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
00282 {
00283   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00284   
00285   PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
00286 }
00287 
00288 static const DBusThreadFunctions pthread_functions =
00289 {
00290   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK |
00291   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK |
00292   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK |
00293   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK |
00294   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
00295   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
00296   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
00297   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
00298   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
00299   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
00300   NULL, NULL, NULL, NULL,
00301   _dbus_pthread_condvar_new,
00302   _dbus_pthread_condvar_free,
00303   _dbus_pthread_condvar_wait,
00304   _dbus_pthread_condvar_wait_timeout,
00305   _dbus_pthread_condvar_wake_one,
00306   _dbus_pthread_condvar_wake_all,
00307   _dbus_pthread_mutex_new,
00308   _dbus_pthread_mutex_free,
00309   _dbus_pthread_mutex_lock,
00310   _dbus_pthread_mutex_unlock
00311 };
00312 
00313 dbus_bool_t
00314 _dbus_threads_init_platform_specific (void)
00315 {
00316   return dbus_threads_init (&pthread_functions);
00317 }

Generated on Sun Nov 26 13:16:28 2006 for D-Bus by  doxygen 1.4.6