25 #include "dbus-internals.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-threads.h"
49 volatile pthread_t holder;
59 #define DBUS_MUTEX(m) ((DBusMutex*) m)
60 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
62 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
63 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
66 #ifdef DBUS_DISABLE_ASSERT
68 #define PTHREAD_CHECK(func_name, result_or_call) \
69 do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
71 #define PTHREAD_CHECK(func_name, result_or_call) do { \
72 int tmp = (result_or_call); \
74 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n", \
75 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
81 _dbus_pthread_mutex_new (
void)
90 result = pthread_mutex_init (&pmutex->
lock,
NULL);
92 if (result == ENOMEM || result == EAGAIN)
99 PTHREAD_CHECK (
"pthread_mutex_init", result);
110 return DBUS_MUTEX (pmutex);
114 _dbus_pthread_mutex_free (
DBusMutex *mutex)
120 PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->
lock));
126 _dbus_pthread_mutex_lock (
DBusMutex *mutex)
129 pthread_t
self = pthread_self ();
138 if (pmutex->
count == 0)
142 PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&pmutex->
lock));
161 if (pthread_equal (pmutex->
holder,
self))
169 PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&pmutex->
lock));
179 _dbus_pthread_mutex_unlock (
DBusMutex *mutex)
187 if (pmutex->
count == 0)
188 PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->
lock));
194 _dbus_pthread_condvar_new (
void)
197 pthread_condattr_t attr;
204 pthread_condattr_init (&attr);
205 #ifdef HAVE_MONOTONIC_CLOCK
206 if (have_monotonic_clock)
207 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
210 result = pthread_cond_init (&pcond->
cond, &attr);
211 pthread_condattr_destroy (&attr);
213 if (result == EAGAIN || result == ENOMEM)
220 PTHREAD_CHECK (
"pthread_cond_init", result);
223 return DBUS_COND_VAR (pcond);
231 PTHREAD_CHECK (
"pthread_cond_destroy", pthread_cond_destroy (&pcond->
cond));
247 old_count = pmutex->
count;
249 PTHREAD_CHECK (
"pthread_cond_wait", pthread_cond_wait (&pcond->
cond, &pmutex->
lock));
251 pmutex->
count = old_count;
252 pmutex->
holder = pthread_self();
256 _dbus_pthread_condvar_wait_timeout (
DBusCondVar *cond,
258 int timeout_milliseconds)
262 struct timeval time_now;
263 struct timespec end_time;
270 #ifdef HAVE_MONOTONIC_CLOCK
271 if (have_monotonic_clock)
273 struct timespec monotonic_timer;
274 clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
275 time_now.tv_sec = monotonic_timer.tv_sec;
276 time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
281 gettimeofday (&time_now,
NULL);
283 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
284 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
285 if (end_time.tv_nsec > 1000*1000*1000)
287 end_time.tv_sec += 1;
288 end_time.tv_nsec -= 1000*1000*1000;
291 old_count = pmutex->
count;
293 result = pthread_cond_timedwait (&pcond->
cond, &pmutex->
lock, &end_time);
295 if (result != ETIMEDOUT)
297 PTHREAD_CHECK (
"pthread_cond_timedwait", result);
301 pmutex->
count = old_count;
302 pmutex->
holder = pthread_self();
305 return result != ETIMEDOUT;
313 PTHREAD_CHECK (
"pthread_cond_signal", pthread_cond_signal (&pcond->
cond));
321 PTHREAD_CHECK (
"pthread_cond_broadcast", pthread_cond_broadcast (&pcond->
cond));
326 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK |
327 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK |
328 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK |
329 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK |
330 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
331 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
332 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
333 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
334 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
335 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
337 _dbus_pthread_condvar_new,
338 _dbus_pthread_condvar_free,
339 _dbus_pthread_condvar_wait,
340 _dbus_pthread_condvar_wait_timeout,
341 _dbus_pthread_condvar_wake_one,
342 _dbus_pthread_condvar_wake_all,
343 _dbus_pthread_mutex_new,
344 _dbus_pthread_mutex_free,
345 _dbus_pthread_mutex_lock,
346 _dbus_pthread_mutex_unlock
350 check_monotonic_clock (
void)
352 #ifdef HAVE_MONOTONIC_CLOCK
353 struct timespec dummy;
354 if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
355 have_monotonic_clock =
TRUE;
362 check_monotonic_clock ();