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 _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
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
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 }