1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
26 #include "dbus-threads.h"
33 pthread_mutex_t lock; /**< lock protecting count field */
34 volatile int count; /**< count of how many times lock holder has recursively locked */
35 volatile pthread_t holder; /**< holder of the lock if count >0,
36 valid but arbitrary thread if count
37 has ever been >0, uninitialized memory
38 if count has never been >0 */
42 pthread_cond_t cond; /**< the condition */
45 #define DBUS_MUTEX(m) ((DBusMutex*) m)
46 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
48 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
49 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
52 #ifdef DBUS_DISABLE_ASSERT
53 #define PTHREAD_CHECK(func_name, result_or_call) do { \
54 do { (result_or_call) } while (0)
56 #define PTHREAD_CHECK(func_name, result_or_call) do { \
57 int tmp = (result_or_call); \
59 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n", \
60 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
63 #endif /* !DBUS_DISABLE_ASSERT */
66 _dbus_pthread_mutex_new (void)
68 DBusMutexPThread *pmutex;
71 pmutex = dbus_new (DBusMutexPThread, 1);
75 result = pthread_mutex_init (&pmutex->lock, NULL);
77 if (result == ENOMEM || result == EAGAIN)
84 PTHREAD_CHECK ("pthread_mutex_init", result);
90 /* There's no portable way to have a "null" pthread afaik so we
91 * can't set pmutex->holder to anything sensible. We only access it
92 * once the lock is held (which means we've set it).
95 return DBUS_MUTEX (pmutex);
99 _dbus_pthread_mutex_free (DBusMutex *mutex)
101 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
103 _dbus_assert (pmutex->count == 0);
105 PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->lock));
111 _dbus_pthread_mutex_lock (DBusMutex *mutex)
113 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
114 pthread_t self = pthread_self ();
116 /* If the count is > 0 then someone had the lock, maybe us. If it is
117 * 0, then it might immediately change right after we read it,
118 * but it will be changed by another thread; i.e. if we read 0,
119 * we assume that this thread doesn't have the lock.
121 * Not 100% sure this is safe, but ... seems like it should be.
123 if (pmutex->count == 0)
125 /* We know we don't have the lock; someone may have the lock. */
127 PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
129 /* We now have the lock. Count must be 0 since it must be 0 when
130 * the lock is released by another thread, and we just now got
133 _dbus_assert (pmutex->count == 0);
135 pmutex->holder = self;
140 /* We know someone had the lock, possibly us. Thus
141 * pmutex->holder is not pointing to junk, though it may not be
142 * the lock holder anymore if the lock holder is not us. If the
143 * lock holder is us, then we definitely have the lock.
146 if (pthread_equal (pmutex->holder, self))
148 /* We already have the lock. */
149 _dbus_assert (pmutex->count > 0);
153 /* Wait for the lock */
154 PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
155 _dbus_assert (pmutex->count == 0);
163 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
165 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
167 _dbus_assert (pmutex->count > 0);
171 if (pmutex->count == 0)
172 PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
174 /* We leave pmutex->holder set to ourselves, its content is undefined if count is 0 */
178 _dbus_pthread_condvar_new (void)
180 DBusCondVarPThread *pcond;
183 pcond = dbus_new (DBusCondVarPThread, 1);
187 result = pthread_cond_init (&pcond->cond, NULL);
189 if (result == EAGAIN || result == ENOMEM)
196 PTHREAD_CHECK ("pthread_cond_init", result);
199 return DBUS_COND_VAR (pcond);
203 _dbus_pthread_condvar_free (DBusCondVar *cond)
205 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
207 PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
213 _dbus_pthread_condvar_wait (DBusCondVar *cond,
216 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
217 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
220 _dbus_assert (pmutex->count > 0);
221 _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));
223 old_count = pmutex->count;
225 PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
226 _dbus_assert (pmutex->count == 0);
227 pmutex->count = old_count;
231 _dbus_pthread_condvar_wait_timeout (DBusCondVar *cond,
233 int timeout_milliseconds)
235 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
236 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
237 struct timeval time_now;
238 struct timespec end_time;
242 _dbus_assert (pmutex->count > 0);
243 _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));
245 gettimeofday (&time_now, NULL);
247 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
248 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
249 if (end_time.tv_nsec > 1000*1000*1000)
251 end_time.tv_sec += 1;
252 end_time.tv_nsec -= 1000*1000*1000;
255 old_count = pmutex->count;
257 result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
259 if (result != ETIMEDOUT)
261 PTHREAD_CHECK ("pthread_cond_timedwait", result);
264 _dbus_assert (pmutex->count == 0);
265 pmutex->count = old_count;
267 /* return true if we did not time out */
268 return result != ETIMEDOUT;
272 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
274 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
276 PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
280 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
282 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
284 PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
287 static const DBusThreadFunctions pthread_functions =
289 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK |
290 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK |
291 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK |
292 DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK |
293 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
294 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
295 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
296 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
297 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
298 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
299 NULL, NULL, NULL, NULL,
300 _dbus_pthread_condvar_new,
301 _dbus_pthread_condvar_free,
302 _dbus_pthread_condvar_wait,
303 _dbus_pthread_condvar_wait_timeout,
304 _dbus_pthread_condvar_wake_one,
305 _dbus_pthread_condvar_wake_all,
306 _dbus_pthread_mutex_new,
307 _dbus_pthread_mutex_free,
308 _dbus_pthread_mutex_lock,
309 _dbus_pthread_mutex_unlock
313 _dbus_threads_init_platform_specific (void)
315 return dbus_threads_init (&pthread_functions);