25 #include "dbus-internals.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-threads.h"
39 #ifdef HAVE_MONOTONIC_CLOCK
60 #define DBUS_MUTEX(m) ((DBusMutex*) m)
61 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
63 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
64 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
67 #ifdef DBUS_DISABLE_ASSERT
69 #define PTHREAD_CHECK(func_name, result_or_call) \
70 do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
72 #define PTHREAD_CHECK(func_name, result_or_call) do { \
73 int tmp = (result_or_call); \
75 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n", \
76 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
82 _dbus_platform_cmutex_new (
void)
91 result = pthread_mutex_init (&pmutex->
lock,
NULL);
93 if (result == ENOMEM || result == EAGAIN)
100 PTHREAD_CHECK (
"pthread_mutex_init", result);
107 _dbus_platform_rmutex_new (
void)
110 pthread_mutexattr_t mutexattr;
117 pthread_mutexattr_init (&mutexattr);
118 pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
119 result = pthread_mutex_init (&pmutex->
lock, &mutexattr);
120 pthread_mutexattr_destroy (&mutexattr);
122 if (result == ENOMEM || result == EAGAIN)
129 PTHREAD_CHECK (
"pthread_mutex_init", result);
136 _dbus_platform_cmutex_free (
DBusCMutex *mutex)
138 PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&mutex->
lock));
143 _dbus_platform_rmutex_free (
DBusRMutex *mutex)
145 PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&mutex->
lock));
150 _dbus_platform_cmutex_lock (
DBusCMutex *mutex)
152 PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&mutex->
lock));
156 _dbus_platform_rmutex_lock (
DBusRMutex *mutex)
158 PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&mutex->
lock));
162 _dbus_platform_cmutex_unlock (
DBusCMutex *mutex)
164 PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&mutex->
lock));
168 _dbus_platform_rmutex_unlock (
DBusRMutex *mutex)
170 PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&mutex->
lock));
174 _dbus_platform_condvar_new (
void)
177 pthread_condattr_t attr;
184 pthread_condattr_init (&attr);
185 #ifdef HAVE_MONOTONIC_CLOCK
186 if (have_monotonic_clock)
187 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
190 result = pthread_cond_init (&pcond->
cond, &attr);
191 pthread_condattr_destroy (&attr);
193 if (result == EAGAIN || result == ENOMEM)
200 PTHREAD_CHECK (
"pthread_cond_init", result);
209 PTHREAD_CHECK (
"pthread_cond_destroy", pthread_cond_destroy (&cond->
cond));
217 PTHREAD_CHECK (
"pthread_cond_wait", pthread_cond_wait (&cond->
cond, &mutex->
lock));
221 _dbus_platform_condvar_wait_timeout (
DBusCondVar *cond,
223 int timeout_milliseconds)
225 struct timeval time_now;
226 struct timespec end_time;
229 #ifdef HAVE_MONOTONIC_CLOCK
230 if (have_monotonic_clock)
232 struct timespec monotonic_timer;
233 clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
234 time_now.tv_sec = monotonic_timer.tv_sec;
235 time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
240 gettimeofday (&time_now,
NULL);
242 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
243 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
244 if (end_time.tv_nsec > 1000*1000*1000)
246 end_time.tv_sec += 1;
247 end_time.tv_nsec -= 1000*1000*1000;
250 result = pthread_cond_timedwait (&cond->
cond, &mutex->
lock, &end_time);
252 if (result != ETIMEDOUT)
254 PTHREAD_CHECK (
"pthread_cond_timedwait", result);
258 return result != ETIMEDOUT;
262 _dbus_platform_condvar_wake_one (
DBusCondVar *cond)
264 PTHREAD_CHECK (
"pthread_cond_signal", pthread_cond_signal (&cond->
cond));
268 check_monotonic_clock (
void)
270 #ifdef HAVE_MONOTONIC_CLOCK
271 struct timespec dummy;
272 if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
273 have_monotonic_clock =
TRUE;
284 check_monotonic_clock ();
290 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
295 pthread_mutex_lock (&init_mutex);
301 pthread_mutex_unlock (&init_mutex);
#define NULL
A null pointer, defined appropriately for C or C++.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
dbus_bool_t _dbus_check_setuid(void)
NOTE: If you modify this function, please also consider making the corresponding change in GLib...
dbus_bool_t _dbus_threads_init_platform_specific(void)
Initialize threads as in dbus_threads_init_default(), appropriately for the platform.
pthread_mutex_t lock
the lock
pthread_mutex_t lock
the lock
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific().
void _dbus_threads_lock_platform_specific(void)
Lock a static mutex used to protect _dbus_threads_init_platform_specific().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
#define TRUE
Expands to "1".
pthread_cond_t cond
the condition