1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-threads.h D-BUS threads handling
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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
23 #include "dbus-threads.h"
24 #include "dbus-internals.h"
26 static DBusThreadFunctions thread_functions =
29 NULL, NULL, NULL, NULL,
30 NULL, NULL, NULL, NULL, NULL,
32 NULL, NULL, NULL, NULL,
33 NULL, NULL, NULL, NULL
35 static int thread_init_generation = 0;
37 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
38 #define _DBUS_DUMMY_MUTEX ((DBusMutex*)0xABCDEF)
40 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
41 #define _DBUS_DUMMY_CONDVAR ((DBusCondVar*)0xABCDEF2)
44 * @defgroup DBusThreads Thread functions
46 * @brief dbus_threads_init(), dbus_mutex_lock(), etc.
48 * Functions and macros related to threads and thread locks.
54 * Creates a new mutex using the function supplied to dbus_threads_init(),
55 * or creates a no-op mutex if threads are not initialized.
56 * May return #NULL even if threads are initialized, indicating
59 * @returns new mutex or #NULL
64 if (thread_functions.mutex_new)
65 return (* thread_functions.mutex_new) ();
67 return _DBUS_DUMMY_MUTEX;
71 * Frees a mutex created with dbus_mutex_new(); does
72 * nothing if passed a #NULL pointer.
75 dbus_mutex_free (DBusMutex *mutex)
77 if (mutex && thread_functions.mutex_free)
78 (* thread_functions.mutex_free) (mutex);
82 * Locks a mutex. Does nothing if passed a #NULL pointer.
83 * Locks are not recursive.
85 * @returns #TRUE on success
88 dbus_mutex_lock (DBusMutex *mutex)
90 if (mutex && thread_functions.mutex_lock)
91 return (* thread_functions.mutex_lock) (mutex);
97 * Unlocks a mutex. Does nothing if passed a #NULL pointer.
99 * @returns #TRUE on success
102 dbus_mutex_unlock (DBusMutex *mutex)
104 if (mutex && thread_functions.mutex_unlock)
105 return (* thread_functions.mutex_unlock) (mutex);
111 * Creates a new condition variable using the function supplied
112 * to dbus_threads_init(), or creates a no-op condition variable
113 * if threads are not initialized. May return #NULL even if
114 * threads are initialized, indicating out-of-memory.
116 * @returns new mutex or #NULL
119 dbus_condvar_new (void)
121 if (thread_functions.condvar_new)
122 return (* thread_functions.condvar_new) ();
124 return _DBUS_DUMMY_CONDVAR;
128 * Frees a conditional variable created with dbus_condvar_new(); does
129 * nothing if passed a #NULL pointer.
132 dbus_condvar_free (DBusCondVar *cond)
134 if (cond && thread_functions.condvar_free)
135 (* thread_functions.condvar_free) (cond);
139 * Atomically unlocks the mutex and waits for the conditions
140 * variable to be signalled. Locks the mutex again before
142 * Does nothing if passed a #NULL pointer.
145 dbus_condvar_wait (DBusCondVar *cond,
148 if (cond && mutex && thread_functions.condvar_wait)
149 (* thread_functions.condvar_wait) (cond, mutex);
153 * Atomically unlocks the mutex and waits for the conditions
154 * variable to be signalled, or for a timeout. Locks the
155 * mutex again before returning.
156 * Does nothing if passed a #NULL pointer.
158 * @param cond the condition variable
159 * @param mutex the mutex
160 * @param timeout_milliseconds the maximum time to wait
161 * @returns TRUE if the condition was reached, or FALSE if the
162 * timeout was reached.
165 dbus_condvar_wait_timeout (DBusCondVar *cond,
167 int timeout_milliseconds)
169 if (cond && mutex && thread_functions.condvar_wait)
170 return (* thread_functions.condvar_wait_timeout) (cond, mutex, timeout_milliseconds);
176 * If there are threads waiting on the condition variable, wake
178 * Does nothing if passed a #NULL pointer.
181 dbus_condvar_wake_one (DBusCondVar *cond)
183 if (cond && thread_functions.condvar_wake_one)
184 (* thread_functions.condvar_wake_one) (cond);
188 * If there are threads waiting on the condition variable, wake
190 * Does nothing if passed a #NULL pointer.
193 dbus_condvar_wake_all (DBusCondVar *cond)
195 if (cond && thread_functions.condvar_wake_all)
196 (* thread_functions.condvar_wake_all) (cond);
200 shutdown_global_locks (void *data)
202 DBusMutex ***locks = data;
206 while (i < _DBUS_N_GLOBAL_LOCKS)
208 dbus_mutex_free (*(locks[i]));
217 init_global_locks (void)
220 DBusMutex ***dynamic_global_locks;
222 DBusMutex **global_locks[] = {
223 #define LOCK_ADDR(name) (& _dbus_lock_##name)
225 LOCK_ADDR (connection_slots),
226 LOCK_ADDR (server_slots),
227 LOCK_ADDR (message_slots),
229 LOCK_ADDR (message_handler),
230 LOCK_ADDR (callback_object),
232 LOCK_ADDR (shutdown_funcs),
233 LOCK_ADDR (system_users)
237 _dbus_assert (_DBUS_N_ELEMENTS (global_locks) ==
238 _DBUS_N_GLOBAL_LOCKS);
242 dynamic_global_locks = dbus_new (DBusMutex**, _DBUS_N_GLOBAL_LOCKS);
243 if (dynamic_global_locks == NULL)
246 while (i < _DBUS_N_ELEMENTS (global_locks))
248 *global_locks[i] = dbus_mutex_new ();
250 if (*global_locks[i] == NULL)
253 dynamic_global_locks[i] = global_locks[i];
258 if (!_dbus_register_shutdown_func (shutdown_global_locks,
259 dynamic_global_locks))
265 dbus_free (dynamic_global_locks);
267 for (i = i - 1; i >= 0; i--)
269 dbus_mutex_free (*global_locks[i]);
270 *global_locks[i] = NULL;
277 * Initializes threads. If this function is not called,
278 * the D-BUS library will not lock any data structures.
279 * If it is called, D-BUS will do locking, at some cost
280 * in efficiency. Note that this function must be called
281 * BEFORE using any other D-BUS functions.
283 * This function may be called more than once, as long
284 * as you pass in the same functions each time. If it's
285 * called multiple times with different functions, then
286 * a warning is printed, because someone is confused.
288 * @param functions functions for using threads
289 * @returns #TRUE on success, #FALSE if no memory
292 dbus_threads_init (const DBusThreadFunctions *functions)
294 _dbus_assert (functions != NULL);
296 /* these base functions are required. Future additions to
297 * DBusThreadFunctions may be optional.
299 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK);
300 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK);
301 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK);
302 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK);
303 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK);
304 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK);
305 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK);
306 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK);
307 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK);
308 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK);
309 _dbus_assert (functions->mutex_new != NULL);
310 _dbus_assert (functions->mutex_free != NULL);
311 _dbus_assert (functions->mutex_lock != NULL);
312 _dbus_assert (functions->mutex_unlock != NULL);
313 _dbus_assert (functions->condvar_new != NULL);
314 _dbus_assert (functions->condvar_free != NULL);
315 _dbus_assert (functions->condvar_wait != NULL);
316 _dbus_assert (functions->condvar_wait_timeout != NULL);
317 _dbus_assert (functions->condvar_wake_one != NULL);
318 _dbus_assert (functions->condvar_wake_all != NULL);
320 /* Check that all bits in the mask actually are valid mask bits.
321 * ensures people won't write code that breaks when we add
324 _dbus_assert ((functions->mask & ~DBUS_THREAD_FUNCTIONS_ALL_MASK) == 0);
326 if (thread_init_generation != _dbus_current_generation)
327 thread_functions.mask = 0; /* allow re-init in new generation */
329 if (thread_functions.mask != 0)
331 /* Silently allow multiple init if the functions are the same ones.
332 * Well, we only bother checking two of them, just out of laziness.
334 if (thread_functions.mask == functions->mask &&
335 thread_functions.mutex_new == functions->mutex_new &&
336 thread_functions.condvar_new == functions->condvar_new)
342 _dbus_warn ("dbus_threads_init() called twice with two different sets of functions\n");
347 thread_functions.mutex_new = functions->mutex_new;
348 thread_functions.mutex_free = functions->mutex_free;
349 thread_functions.mutex_lock = functions->mutex_lock;
350 thread_functions.mutex_unlock = functions->mutex_unlock;
352 thread_functions.condvar_new = functions->condvar_new;
353 thread_functions.condvar_free = functions->condvar_free;
354 thread_functions.condvar_wait = functions->condvar_wait;
355 thread_functions.condvar_wait_timeout = functions->condvar_wait_timeout;
356 thread_functions.condvar_wake_one = functions->condvar_wake_one;
357 thread_functions.condvar_wake_all = functions->condvar_wake_all;
359 thread_functions.mask = functions->mask;
361 if (!init_global_locks ())
364 thread_init_generation = _dbus_current_generation;
371 #ifdef DBUS_BUILD_TESTS
372 /** Fake mutex used for debugging */
373 typedef struct DBusFakeMutex DBusFakeMutex;
374 /** Fake mutex used for debugging */
377 dbus_bool_t locked; /**< Mutex is "locked" */
380 static DBusMutex * dbus_fake_mutex_new (void);
381 static void dbus_fake_mutex_free (DBusMutex *mutex);
382 static dbus_bool_t dbus_fake_mutex_lock (DBusMutex *mutex);
383 static dbus_bool_t dbus_fake_mutex_unlock (DBusMutex *mutex);
384 static DBusCondVar* dbus_fake_condvar_new (void);
385 static void dbus_fake_condvar_free (DBusCondVar *cond);
386 static void dbus_fake_condvar_wait (DBusCondVar *cond,
388 static dbus_bool_t dbus_fake_condvar_wait_timeout (DBusCondVar *cond,
391 static void dbus_fake_condvar_wake_one (DBusCondVar *cond);
392 static void dbus_fake_condvar_wake_all (DBusCondVar *cond);
395 static const DBusThreadFunctions fake_functions =
397 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
398 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
399 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
400 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
401 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
402 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
403 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
404 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
405 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
406 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
408 dbus_fake_mutex_free,
409 dbus_fake_mutex_lock,
410 dbus_fake_mutex_unlock,
411 dbus_fake_condvar_new,
412 dbus_fake_condvar_free,
413 dbus_fake_condvar_wait,
414 dbus_fake_condvar_wait_timeout,
415 dbus_fake_condvar_wake_one,
416 dbus_fake_condvar_wake_all
420 dbus_fake_mutex_new (void)
422 DBusFakeMutex *mutex;
424 mutex = dbus_new0 (DBusFakeMutex, 1);
426 return (DBusMutex *)mutex;
430 dbus_fake_mutex_free (DBusMutex *mutex)
432 DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
434 _dbus_assert (!fake->locked);
440 dbus_fake_mutex_lock (DBusMutex *mutex)
442 DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
444 _dbus_assert (!fake->locked);
452 dbus_fake_mutex_unlock (DBusMutex *mutex)
454 DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
456 _dbus_assert (fake->locked);
458 fake->locked = FALSE;
464 dbus_fake_condvar_new (void)
466 return (DBusCondVar*) _dbus_strdup ("FakeCondvar");
470 dbus_fake_condvar_free (DBusCondVar *cond)
476 dbus_fake_condvar_wait (DBusCondVar *cond,
483 dbus_fake_condvar_wait_timeout (DBusCondVar *cond,
491 dbus_fake_condvar_wake_one (DBusCondVar *cond)
497 dbus_fake_condvar_wake_all (DBusCondVar *cond)
503 _dbus_threads_init_debug (void)
505 return dbus_threads_init (&fake_functions);
508 #endif /* DBUS_BUILD_TESTS */