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; /**< lock protecting count field */
48 volatile int count; /**< count of how many times lock holder has recursively locked */
49 volatile pthread_t holder; /**< holder of the lock if count >0,
50 valid but arbitrary thread if count
51 has ever been >0, uninitialized memory
52 if count has never been >0 */
56 pthread_cond_t cond; /**< the condition */
59 #define DBUS_MUTEX(m) ((DBusMutex*) m)
60 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
62 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
63 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
66 #ifdef DBUS_DISABLE_ASSERT
67 /* (tmp != 0) is a no-op usage to silence compiler */
68 #define PTHREAD_CHECK(func_name, result_or_call) \
69 do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
71 #define PTHREAD_CHECK(func_name, result_or_call) do { \
72 int tmp = (result_or_call); \
74 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n", \
75 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
78 #endif /* !DBUS_DISABLE_ASSERT */
81 _dbus_pthread_mutex_new (void)
83 DBusMutexPThread *pmutex;
86 pmutex = dbus_new (DBusMutexPThread, 1);
90 result = pthread_mutex_init (&pmutex->lock, NULL);
92 if (result == ENOMEM || result == EAGAIN)
99 PTHREAD_CHECK ("pthread_mutex_init", result);
105 /* There's no portable way to have a "null" pthread afaik so we
106 * can't set pmutex->holder to anything sensible. We only access it
107 * once the lock is held (which means we've set it).
110 return DBUS_MUTEX (pmutex);
114 _dbus_pthread_mutex_free (DBusMutex *mutex)
116 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
118 _dbus_assert (pmutex->count == 0);
120 PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->lock));
126 _dbus_pthread_mutex_lock (DBusMutex *mutex)
128 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
129 pthread_t self = pthread_self ();
131 /* If the count is > 0 then someone had the lock, maybe us. If it is
132 * 0, then it might immediately change right after we read it,
133 * but it will be changed by another thread; i.e. if we read 0,
134 * we assume that this thread doesn't have the lock.
136 * Not 100% sure this is safe, but ... seems like it should be.
138 if (pmutex->count == 0)
140 /* We know we don't have the lock; someone may have the lock. */
142 PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
144 /* We now have the lock. Count must be 0 since it must be 0 when
145 * the lock is released by another thread, and we just now got
148 _dbus_assert (pmutex->count == 0);
150 pmutex->holder = self;
155 /* We know someone had the lock, possibly us. Thus
156 * pmutex->holder is not pointing to junk, though it may not be
157 * the lock holder anymore if the lock holder is not us. If the
158 * lock holder is us, then we definitely have the lock.
161 if (pthread_equal (pmutex->holder, self))
163 /* We already have the lock. */
164 _dbus_assert (pmutex->count > 0);
168 /* Wait for the lock */
169 PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
170 pmutex->holder = self;
171 _dbus_assert (pmutex->count == 0);
179 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
181 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
183 _dbus_assert (pmutex->count > 0);
187 if (pmutex->count == 0)
188 PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
190 /* We leave pmutex->holder set to ourselves, its content is undefined if count is 0 */
194 _dbus_pthread_condvar_new (void)
196 DBusCondVarPThread *pcond;
197 pthread_condattr_t attr;
200 pcond = dbus_new (DBusCondVarPThread, 1);
204 pthread_condattr_init (&attr);
205 #ifdef HAVE_MONOTONIC_CLOCK
206 if (have_monotonic_clock)
207 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
210 result = pthread_cond_init (&pcond->cond, &attr);
211 pthread_condattr_destroy (&attr);
213 if (result == EAGAIN || result == ENOMEM)
220 PTHREAD_CHECK ("pthread_cond_init", result);
223 return DBUS_COND_VAR (pcond);
227 _dbus_pthread_condvar_free (DBusCondVar *cond)
229 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
231 PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
237 _dbus_pthread_condvar_wait (DBusCondVar *cond,
240 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
241 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
244 _dbus_assert (pmutex->count > 0);
245 _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));
247 old_count = pmutex->count;
248 pmutex->count = 0; /* allow other threads to lock */
249 PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
250 _dbus_assert (pmutex->count == 0);
251 pmutex->count = old_count;
252 pmutex->holder = pthread_self(); /* other threads may have locked the mutex in the meantime */
256 _dbus_pthread_condvar_wait_timeout (DBusCondVar *cond,
258 int timeout_milliseconds)
260 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
261 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
262 struct timeval time_now;
263 struct timespec end_time;
267 _dbus_assert (pmutex->count > 0);
268 _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));
270 #ifdef HAVE_MONOTONIC_CLOCK
271 if (have_monotonic_clock)
273 struct timespec monotonic_timer;
274 clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
275 time_now.tv_sec = monotonic_timer.tv_sec;
276 time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
279 /* This else falls through to gettimeofday */
281 gettimeofday (&time_now, NULL);
283 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
284 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
285 if (end_time.tv_nsec > 1000*1000*1000)
287 end_time.tv_sec += 1;
288 end_time.tv_nsec -= 1000*1000*1000;
291 old_count = pmutex->count;
293 result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
295 if (result != ETIMEDOUT)
297 PTHREAD_CHECK ("pthread_cond_timedwait", result);
300 _dbus_assert (pmutex->count == 0);
301 pmutex->count = old_count;
302 pmutex->holder = pthread_self(); /* other threads may have locked the mutex in the meantime */
304 /* return true if we did not time out */
305 return result != ETIMEDOUT;
309 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
311 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
313 PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
317 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
319 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
321 PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
324 static const DBusThreadFunctions pthread_functions =
326 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK |
327 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK |
328 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK |
329 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK |
330 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
331 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
332 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
333 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
334 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
335 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
336 NULL, NULL, NULL, NULL,
337 _dbus_pthread_condvar_new,
338 _dbus_pthread_condvar_free,
339 _dbus_pthread_condvar_wait,
340 _dbus_pthread_condvar_wait_timeout,
341 _dbus_pthread_condvar_wake_one,
342 _dbus_pthread_condvar_wake_all,
343 _dbus_pthread_mutex_new,
344 _dbus_pthread_mutex_free,
345 _dbus_pthread_mutex_lock,
346 _dbus_pthread_mutex_unlock
350 check_monotonic_clock (void)
352 #ifdef HAVE_MONOTONIC_CLOCK
353 struct timespec dummy;
354 if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
355 have_monotonic_clock = TRUE;
360 _dbus_threads_init_platform_specific (void)
362 check_monotonic_clock ();
363 return dbus_threads_init (&pthread_functions);