00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
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 
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 
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   
00089   pmutex->count = 0;
00090 
00091   
00092 
00093 
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   
00118 
00119 
00120 
00121 
00122 
00123 
00124   if (pmutex->count == 0)
00125     {
00126       
00127       
00128       PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
00129 
00130       
00131 
00132 
00133 
00134       _dbus_assert (pmutex->count == 0);
00135       
00136       pmutex->holder = self;
00137       pmutex->count = 1;
00138     }
00139   else
00140     {
00141       
00142 
00143 
00144 
00145 
00146 
00147       if (pthread_equal (pmutex->holder, self))
00148         {
00149           
00150           _dbus_assert (pmutex->count > 0);
00151         }
00152       else
00153         {
00154           
00155           PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
00156           pmutex->holder = self;
00157           _dbus_assert (pmutex->count == 0);
00158         }
00159 
00160       pmutex->count += 1;
00161     }
00162 }
00163 
00164 static void
00165 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
00166 {
00167   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
00168 
00169   _dbus_assert (pmutex->count > 0);
00170   
00171   pmutex->count -= 1;
00172 
00173   if (pmutex->count == 0)
00174     PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
00175   
00176   
00177 }
00178 
00179 static DBusCondVar *
00180 _dbus_pthread_condvar_new (void)
00181 {
00182   DBusCondVarPThread *pcond;
00183   int result;
00184   
00185   pcond = dbus_new (DBusCondVarPThread, 1);
00186   if (pcond == NULL)
00187     return NULL;
00188 
00189   result = pthread_cond_init (&pcond->cond, NULL);
00190 
00191   if (result == EAGAIN || result == ENOMEM)
00192     {
00193       dbus_free (pcond);
00194       return NULL;
00195     }
00196   else
00197     {
00198       PTHREAD_CHECK ("pthread_cond_init", result);
00199     }
00200   
00201   return DBUS_COND_VAR (pcond);
00202 }
00203 
00204 static void
00205 _dbus_pthread_condvar_free (DBusCondVar *cond)
00206 {  
00207   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00208   
00209   PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
00210 
00211   dbus_free (pcond);
00212 }
00213 
00214 static void
00215 _dbus_pthread_condvar_wait (DBusCondVar *cond,
00216                             DBusMutex   *mutex)
00217 {
00218   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
00219   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00220   int old_count;
00221   
00222   _dbus_assert (pmutex->count > 0);
00223   _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));
00224 
00225   old_count = pmutex->count;
00226   pmutex->count = 0;            
00227   PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
00228   _dbus_assert (pmutex->count == 0);
00229   pmutex->count = old_count;
00230   pmutex->holder = pthread_self(); 
00231 }
00232 
00233 static dbus_bool_t
00234 _dbus_pthread_condvar_wait_timeout (DBusCondVar               *cond,
00235                                     DBusMutex                 *mutex,
00236                                     int                        timeout_milliseconds)
00237 {
00238   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
00239   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00240   struct timeval time_now;
00241   struct timespec end_time;
00242   int result;
00243   int old_count;
00244   
00245   _dbus_assert (pmutex->count > 0);
00246   _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));  
00247   
00248   gettimeofday (&time_now, NULL);
00249   
00250   end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
00251   end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
00252   if (end_time.tv_nsec > 1000*1000*1000)
00253     {
00254       end_time.tv_sec += 1;
00255       end_time.tv_nsec -= 1000*1000*1000;
00256     }
00257 
00258   old_count = pmutex->count;
00259   pmutex->count = 0;
00260   result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
00261   
00262   if (result != ETIMEDOUT)
00263     {
00264       PTHREAD_CHECK ("pthread_cond_timedwait", result);
00265     }
00266 
00267   _dbus_assert (pmutex->count == 0);
00268   pmutex->count = old_count;
00269   pmutex->holder = pthread_self(); 
00270   
00271   
00272   return result != ETIMEDOUT;
00273 }
00274 
00275 static void
00276 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
00277 {
00278   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00279 
00280   PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
00281 }
00282 
00283 static void
00284 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
00285 {
00286   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
00287   
00288   PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
00289 }
00290 
00291 static const DBusThreadFunctions pthread_functions =
00292 {
00293   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK |
00294   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK |
00295   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK |
00296   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK |
00297   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
00298   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
00299   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
00300   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
00301   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
00302   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
00303   NULL, NULL, NULL, NULL,
00304   _dbus_pthread_condvar_new,
00305   _dbus_pthread_condvar_free,
00306   _dbus_pthread_condvar_wait,
00307   _dbus_pthread_condvar_wait_timeout,
00308   _dbus_pthread_condvar_wake_one,
00309   _dbus_pthread_condvar_wake_all,
00310   _dbus_pthread_mutex_new,
00311   _dbus_pthread_mutex_free,
00312   _dbus_pthread_mutex_lock,
00313   _dbus_pthread_mutex_unlock
00314 };
00315 
00316 dbus_bool_t
00317 _dbus_threads_init_platform_specific (void)
00318 {
00319   return dbus_threads_init (&pthread_functions);
00320 }