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           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   /* We leave pmutex->holder set to ourselves, its content is undefined if count is 0 */
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;            /* allow other threads to lock */
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(); /* other threads may have locked the mutex in the meantime */
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(); /* other threads may have locked the mutex in the meantime */
00270   
00271   /* return true if we did not time out */
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 }

Generated on Fri Sep 21 18:12:13 2007 for D-Bus by  doxygen 1.5.1