25 #include "dbus-internals.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-threads.h"
58 #define DBUS_MUTEX(m) ((DBusMutex*) m)
59 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
61 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
62 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
65 #ifdef DBUS_DISABLE_ASSERT
67 #define PTHREAD_CHECK(func_name, result_or_call) \
68 do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
70 #define PTHREAD_CHECK(func_name, result_or_call) do { \
71 int tmp = (result_or_call); \
73 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n", \
74 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
80 _dbus_platform_cmutex_new (
void)
89 result = pthread_mutex_init (&pmutex->
lock,
NULL);
91 if (result == ENOMEM || result == EAGAIN)
98 PTHREAD_CHECK (
"pthread_mutex_init", result);
105 _dbus_platform_rmutex_new (
void)
108 pthread_mutexattr_t mutexattr;
115 pthread_mutexattr_init (&mutexattr);
116 pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
117 result = pthread_mutex_init (&pmutex->
lock, &mutexattr);
118 pthread_mutexattr_destroy (&mutexattr);
120 if (result == ENOMEM || result == EAGAIN)
127 PTHREAD_CHECK (
"pthread_mutex_init", result);
134 _dbus_platform_cmutex_free (
DBusCMutex *mutex)
136 PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&mutex->
lock));
141 _dbus_platform_rmutex_free (
DBusRMutex *mutex)
143 PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&mutex->
lock));
148 _dbus_platform_cmutex_lock (
DBusCMutex *mutex)
150 PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&mutex->
lock));
154 _dbus_platform_rmutex_lock (
DBusRMutex *mutex)
156 PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&mutex->
lock));
160 _dbus_platform_cmutex_unlock (
DBusCMutex *mutex)
162 PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&mutex->
lock));
166 _dbus_platform_rmutex_unlock (
DBusRMutex *mutex)
168 PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&mutex->
lock));
172 _dbus_platform_condvar_new (
void)
175 pthread_condattr_t attr;
182 pthread_condattr_init (&attr);
183 #ifdef HAVE_MONOTONIC_CLOCK
184 if (have_monotonic_clock)
185 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
188 result = pthread_cond_init (&pcond->
cond, &attr);
189 pthread_condattr_destroy (&attr);
191 if (result == EAGAIN || result == ENOMEM)
198 PTHREAD_CHECK (
"pthread_cond_init", result);
207 PTHREAD_CHECK (
"pthread_cond_destroy", pthread_cond_destroy (&cond->
cond));
215 PTHREAD_CHECK (
"pthread_cond_wait", pthread_cond_wait (&cond->
cond, &mutex->
lock));
219 _dbus_platform_condvar_wait_timeout (
DBusCondVar *cond,
221 int timeout_milliseconds)
223 struct timeval time_now;
224 struct timespec end_time;
227 #ifdef HAVE_MONOTONIC_CLOCK
228 if (have_monotonic_clock)
230 struct timespec monotonic_timer;
231 clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
232 time_now.tv_sec = monotonic_timer.tv_sec;
233 time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
238 gettimeofday (&time_now,
NULL);
240 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
241 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
242 if (end_time.tv_nsec > 1000*1000*1000)
244 end_time.tv_sec += 1;
245 end_time.tv_nsec -= 1000*1000*1000;
248 result = pthread_cond_timedwait (&cond->
cond, &mutex->
lock, &end_time);
250 if (result != ETIMEDOUT)
252 PTHREAD_CHECK (
"pthread_cond_timedwait", result);
256 return result != ETIMEDOUT;
260 _dbus_platform_condvar_wake_one (
DBusCondVar *cond)
262 PTHREAD_CHECK (
"pthread_cond_signal", pthread_cond_signal (&cond->
cond));
266 check_monotonic_clock (
void)
268 #ifdef HAVE_MONOTONIC_CLOCK
269 struct timespec dummy;
270 if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
271 have_monotonic_clock =
TRUE;
282 check_monotonic_clock ();
#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
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
#define TRUE
Expands to "1".
dbus_bool_t dbus_threads_init(const DBusThreadFunctions *functions)
Initializes threads, like dbus_threads_init_default().
pthread_cond_t cond
the condition