25 #include "dbus-internals.h" 
   26 #include "dbus-sysdeps.h" 
   27 #include "dbus-threads.h" 
   40 #ifdef HAVE_MONOTONIC_CLOCK 
   61 #define DBUS_MUTEX(m)         ((DBusMutex*) m) 
   62 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m) 
   64 #define DBUS_COND_VAR(c)         ((DBusCondVar*) c) 
   65 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c) 
   68 #ifdef DBUS_DISABLE_ASSERT 
   70 #define PTHREAD_CHECK(func_name, result_or_call)    \ 
   71     do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0) 
   73 #define PTHREAD_CHECK(func_name, result_or_call) do {                                  \ 
   74     int tmp = (result_or_call);                                                        \ 
   76       _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s",          \ 
   77                                func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME);    \ 
   83 _dbus_platform_cmutex_new (
void)
 
   92   result = pthread_mutex_init (&pmutex->
lock, 
NULL);
 
   94   if (result == ENOMEM || result == EAGAIN)
 
  101       PTHREAD_CHECK (
"pthread_mutex_init", result);
 
  108 _dbus_platform_rmutex_new (
void)
 
  111   pthread_mutexattr_t mutexattr;
 
  118   pthread_mutexattr_init (&mutexattr);
 
  119   pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
 
  120   result = pthread_mutex_init (&pmutex->
lock, &mutexattr);
 
  121   pthread_mutexattr_destroy (&mutexattr);
 
  123   if (result == ENOMEM || result == EAGAIN)
 
  130       PTHREAD_CHECK (
"pthread_mutex_init", result);
 
  137 _dbus_platform_cmutex_free (
DBusCMutex *mutex)
 
  139   PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&mutex->
lock));
 
  144 _dbus_platform_rmutex_free (
DBusRMutex *mutex)
 
  146   PTHREAD_CHECK (
"pthread_mutex_destroy", pthread_mutex_destroy (&mutex->
lock));
 
  151 _dbus_platform_cmutex_lock (
DBusCMutex *mutex)
 
  153   PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&mutex->
lock));
 
  157 _dbus_platform_rmutex_lock (
DBusRMutex *mutex)
 
  159   PTHREAD_CHECK (
"pthread_mutex_lock", pthread_mutex_lock (&mutex->
lock));
 
  163 _dbus_platform_cmutex_unlock (
DBusCMutex *mutex)
 
  165   PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&mutex->
lock));
 
  169 _dbus_platform_rmutex_unlock (
DBusRMutex *mutex)
 
  171   PTHREAD_CHECK (
"pthread_mutex_unlock", pthread_mutex_unlock (&mutex->
lock));
 
  175 _dbus_platform_condvar_new (
void)
 
  178   pthread_condattr_t attr;
 
  185   pthread_condattr_init (&attr);
 
  186 #ifdef HAVE_MONOTONIC_CLOCK 
  187   if (have_monotonic_clock)
 
  188     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
 
  191   result = pthread_cond_init (&pcond->
cond, &attr);
 
  192   pthread_condattr_destroy (&attr);
 
  194   if (result == EAGAIN || result == ENOMEM)
 
  201       PTHREAD_CHECK (
"pthread_cond_init", result);
 
  210   PTHREAD_CHECK (
"pthread_cond_destroy", pthread_cond_destroy (&cond->
cond));
 
  218   PTHREAD_CHECK (
"pthread_cond_wait", pthread_cond_wait (&cond->
cond, &mutex->
lock));
 
  222 _dbus_platform_condvar_wait_timeout (
DBusCondVar               *cond,
 
  224                                      int                        timeout_milliseconds)
 
  226   struct timeval time_now;
 
  227   struct timespec end_time;
 
  230 #ifdef HAVE_MONOTONIC_CLOCK 
  231   if (have_monotonic_clock)
 
  233       struct timespec monotonic_timer;
 
  234       clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
 
  235       time_now.tv_sec = monotonic_timer.tv_sec;
 
  236       time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
 
  241   gettimeofday (&time_now, 
NULL);
 
  243   end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
 
  244   end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
 
  245   if (end_time.tv_nsec > 1000*1000*1000)
 
  247       end_time.tv_sec += 1;
 
  248       end_time.tv_nsec -= 1000*1000*1000;
 
  251   result = pthread_cond_timedwait (&cond->
cond, &mutex->
lock, &end_time);
 
  253   if (result != ETIMEDOUT)
 
  255       PTHREAD_CHECK (
"pthread_cond_timedwait", result);
 
  259   return result != ETIMEDOUT;
 
  263 _dbus_platform_condvar_wake_one (
DBusCondVar *cond)
 
  265   PTHREAD_CHECK (
"pthread_cond_signal", pthread_cond_signal (&cond->
cond));
 
  269 check_monotonic_clock (
void)
 
  271 #ifdef HAVE_MONOTONIC_CLOCK 
  272   struct timespec dummy;
 
  273   if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
 
  274     have_monotonic_clock = 
TRUE;
 
  285   check_monotonic_clock ();
 
  291 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
 
  296   pthread_mutex_lock (&init_mutex);
 
  302   pthread_mutex_unlock (&init_mutex);
 
  305 #ifdef DBUS_ENABLE_VERBOSE_MODE 
  310 _dbus_print_thread (
void)
 
  314   fprintf (stderr, 
"%lu: 0x%lx: ", 
_dbus_pid_for_log (), (
unsigned long) pthread_self ());