27 #include "dbus-internals.h"
28 #include "dbus-sysdeps.h"
29 #include "dbus-threads.h"
42 #ifdef HAVE_MONOTONIC_CLOCK
63 #define DBUS_MUTEX(m) ((DBusMutex*) m)
64 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
66 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
67 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
70 #ifdef DBUS_DISABLE_ASSERT
72 #define PTHREAD_CHECK(func_name, result_or_call) \
73 do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
75 #define PTHREAD_CHECK(func_name, result_or_call) do { \
76 int tmp = (result_or_call); \
78 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s", \
79 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
85 _dbus_platform_cmutex_new (
void)
94 result = pthread_mutex_init (&pmutex->
lock,
NULL);
96 if (result == ENOMEM || result == EAGAIN)
103 PTHREAD_CHECK (
"pthread_mutex_init", result);
110 _dbus_platform_rmutex_new (
void)
113 pthread_mutexattr_t mutexattr;
120 pthread_mutexattr_init (&mutexattr);
121 pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
122 result = pthread_mutex_init (&pmutex->
lock, &mutexattr);
123 pthread_mutexattr_destroy (&mutexattr);
125 if (result == ENOMEM || result == EAGAIN)
132 PTHREAD_CHECK (
"pthread_mutex_init", result);
139 _dbus_platform_cmutex_free (
DBusCMutex *mutex)
141 PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&mutex->
lock));
146 _dbus_platform_rmutex_free (
DBusRMutex *mutex)
148 PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&mutex->
lock));
153 _dbus_platform_cmutex_lock (
DBusCMutex *mutex)
155 PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&mutex->
lock));
159 _dbus_platform_rmutex_lock (
DBusRMutex *mutex)
161 PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&mutex->
lock));
165 _dbus_platform_cmutex_unlock (
DBusCMutex *mutex)
167 PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&mutex->
lock));
171 _dbus_platform_rmutex_unlock (
DBusRMutex *mutex)
173 PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&mutex->
lock));
177 _dbus_platform_condvar_new (
void)
180 pthread_condattr_t attr;
187 pthread_condattr_init (&attr);
188 #ifdef HAVE_MONOTONIC_CLOCK
189 if (have_monotonic_clock)
190 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
193 result = pthread_cond_init (&pcond->
cond, &attr);
194 pthread_condattr_destroy (&attr);
196 if (result == EAGAIN || result == ENOMEM)
203 PTHREAD_CHECK (
"pthread_cond_init", result);
212 PTHREAD_CHECK (
"pthread_cond_destroy", pthread_cond_destroy (&cond->
cond));
220 PTHREAD_CHECK (
"pthread_cond_wait", pthread_cond_wait (&cond->
cond, &mutex->
lock));
224 _dbus_platform_condvar_wait_timeout (
DBusCondVar *cond,
226 int timeout_milliseconds)
228 struct timeval time_now;
229 struct timespec end_time;
232 #ifdef HAVE_MONOTONIC_CLOCK
233 if (have_monotonic_clock)
235 struct timespec monotonic_timer;
236 clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
237 time_now.tv_sec = monotonic_timer.tv_sec;
238 time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
243 gettimeofday (&time_now,
NULL);
245 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
246 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
247 if (end_time.tv_nsec > 1000*1000*1000)
249 end_time.tv_sec += 1;
250 end_time.tv_nsec -= 1000*1000*1000;
253 result = pthread_cond_timedwait (&cond->
cond, &mutex->
lock, &end_time);
255 if (result != ETIMEDOUT)
257 PTHREAD_CHECK (
"pthread_cond_timedwait", result);
261 return result != ETIMEDOUT;
265 _dbus_platform_condvar_wake_one (
DBusCondVar *cond)
267 PTHREAD_CHECK (
"pthread_cond_signal", pthread_cond_signal (&cond->
cond));
271 check_monotonic_clock (
void)
273 #ifdef HAVE_MONOTONIC_CLOCK
274 struct timespec dummy;
275 if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
276 have_monotonic_clock =
TRUE;
287 check_monotonic_clock ();
293 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
298 pthread_mutex_lock (&init_mutex);
304 pthread_mutex_unlock (&init_mutex);
307 #ifdef DBUS_ENABLE_VERBOSE_MODE
312 _dbus_print_thread (
void)
316 fprintf (stderr,
"%lu: 0x%lx: ",
_dbus_pid_for_log (), (
unsigned long) pthread_self ());
#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".
unsigned long _dbus_pid_for_log(void)
The only reason this is separate from _dbus_getpid() is to allow it on Windows for logging but not fo...
pthread_cond_t cond
the condition