1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-pthread.c Implements threads using pthreads (internal to libdbus)
4 * Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-internals.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-threads.h"
39 /* Whether we have a "monotonic" clock; i.e. a clock not affected by
40 * changes in system time.
41 * This is initialized once in check_monotonic_clock below.
42 * https://bugs.freedesktop.org/show_bug.cgi?id=18121
44 static dbus_bool_t have_monotonic_clock = 0;
47 pthread_mutex_t lock; /**< the lock */
51 pthread_cond_t cond; /**< the condition */
54 #define DBUS_MUTEX(m) ((DBusMutex*) m)
55 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
57 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
58 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
61 #ifdef DBUS_DISABLE_ASSERT
62 /* (tmp != 0) is a no-op usage to silence compiler */
63 #define PTHREAD_CHECK(func_name, result_or_call) \
64 do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
66 #define PTHREAD_CHECK(func_name, result_or_call) do { \
67 int tmp = (result_or_call); \
69 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n", \
70 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
73 #endif /* !DBUS_DISABLE_ASSERT */
76 _dbus_pthread_cmutex_new (void)
78 DBusMutexPThread *pmutex;
81 pmutex = dbus_new (DBusMutexPThread, 1);
85 result = pthread_mutex_init (&pmutex->lock, NULL);
87 if (result == ENOMEM || result == EAGAIN)
94 PTHREAD_CHECK ("pthread_mutex_init", result);
97 return DBUS_MUTEX (pmutex);
101 _dbus_pthread_rmutex_new (void)
103 DBusMutexPThread *pmutex;
104 pthread_mutexattr_t mutexattr;
107 pmutex = dbus_new (DBusMutexPThread, 1);
111 pthread_mutexattr_init (&mutexattr);
112 pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
113 result = pthread_mutex_init (&pmutex->lock, &mutexattr);
114 pthread_mutexattr_destroy (&mutexattr);
116 if (result == ENOMEM || result == EAGAIN)
123 PTHREAD_CHECK ("pthread_mutex_init", result);
126 return DBUS_MUTEX (pmutex);
130 _dbus_pthread_mutex_free (DBusMutex *mutex)
132 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
134 PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->lock));
139 _dbus_pthread_mutex_lock (DBusMutex *mutex)
141 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
143 PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
148 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
150 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
152 PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
157 _dbus_pthread_condvar_new (void)
159 DBusCondVarPThread *pcond;
160 pthread_condattr_t attr;
163 pcond = dbus_new (DBusCondVarPThread, 1);
167 pthread_condattr_init (&attr);
168 #ifdef HAVE_MONOTONIC_CLOCK
169 if (have_monotonic_clock)
170 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
173 result = pthread_cond_init (&pcond->cond, &attr);
174 pthread_condattr_destroy (&attr);
176 if (result == EAGAIN || result == ENOMEM)
183 PTHREAD_CHECK ("pthread_cond_init", result);
186 return DBUS_COND_VAR (pcond);
190 _dbus_pthread_condvar_free (DBusCondVar *cond)
192 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
194 PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
200 _dbus_pthread_condvar_wait (DBusCondVar *cond,
203 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
204 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
206 PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
210 _dbus_pthread_condvar_wait_timeout (DBusCondVar *cond,
212 int timeout_milliseconds)
214 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
215 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
216 struct timeval time_now;
217 struct timespec end_time;
220 #ifdef HAVE_MONOTONIC_CLOCK
221 if (have_monotonic_clock)
223 struct timespec monotonic_timer;
224 clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
225 time_now.tv_sec = monotonic_timer.tv_sec;
226 time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
229 /* This else falls through to gettimeofday */
231 gettimeofday (&time_now, NULL);
233 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
234 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
235 if (end_time.tv_nsec > 1000*1000*1000)
237 end_time.tv_sec += 1;
238 end_time.tv_nsec -= 1000*1000*1000;
241 result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
243 if (result != ETIMEDOUT)
245 PTHREAD_CHECK ("pthread_cond_timedwait", result);
248 /* return true if we did not time out */
249 return result != ETIMEDOUT;
253 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
255 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
257 PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
261 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
263 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
265 PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
268 static const DBusThreadFunctions pthread_functions =
270 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
271 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
272 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
273 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
274 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK |
275 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK |
276 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK |
277 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK |
278 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
279 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
280 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
281 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
282 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
283 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
284 _dbus_pthread_cmutex_new,
285 _dbus_pthread_mutex_free,
286 _dbus_pthread_mutex_lock,
287 _dbus_pthread_mutex_unlock,
288 _dbus_pthread_condvar_new,
289 _dbus_pthread_condvar_free,
290 _dbus_pthread_condvar_wait,
291 _dbus_pthread_condvar_wait_timeout,
292 _dbus_pthread_condvar_wake_one,
293 _dbus_pthread_condvar_wake_all,
294 _dbus_pthread_rmutex_new,
295 _dbus_pthread_mutex_free,
296 (void (*) (DBusMutex *)) _dbus_pthread_mutex_lock,
297 (void (*) (DBusMutex *)) _dbus_pthread_mutex_unlock
301 check_monotonic_clock (void)
303 #ifdef HAVE_MONOTONIC_CLOCK
304 struct timespec dummy;
305 if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
306 have_monotonic_clock = TRUE;
311 _dbus_threads_init_platform_specific (void)
313 check_monotonic_clock ();
314 return dbus_threads_init (&pthread_functions);