1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-threads.h D-Bus threads handling
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
24 #include "dbus-threads.h"
25 #include "dbus-internals.h"
26 #include "dbus-threads-internal.h"
27 #include "dbus-list.h"
29 static int thread_init_generation = 0;
32 * @defgroup DBusThreadsInternals Thread functions
33 * @ingroup DBusInternals
34 * @brief _dbus_rmutex_lock(), etc.
36 * Functions and macros related to threads and thread locks.
43 * or creates a no-op mutex if threads are not initialized.
44 * May return #NULL even if threads are initialized, indicating
47 * If possible, the mutex returned by this function is recursive, to
48 * avoid deadlocks. However, that cannot be relied on.
50 * @param location_p the location of the new mutex, can return #NULL on OOM
53 _dbus_rmutex_new_at_location (DBusRMutex **location_p)
55 _dbus_assert (location_p != NULL);
57 if (!dbus_threads_init_default ())
63 *location_p = _dbus_platform_rmutex_new ();
68 * or creates a no-op mutex if threads are not initialized.
69 * May return #NULL even if threads are initialized, indicating
72 * The returned mutex is suitable for use with condition variables.
74 * @param location_p the location of the new mutex, can return #NULL on OOM
77 _dbus_cmutex_new_at_location (DBusCMutex **location_p)
79 _dbus_assert (location_p != NULL);
81 if (!dbus_threads_init_default ())
87 *location_p = _dbus_platform_cmutex_new ();
91 * Frees a DBusRMutex; does nothing if passed a #NULL pointer.
94 _dbus_rmutex_free_at_location (DBusRMutex **location_p)
96 if (location_p == NULL)
99 if (*location_p != NULL)
100 _dbus_platform_rmutex_free (*location_p);
104 * Frees a DBusCMutex; does nothing if passed a #NULL pointer.
107 _dbus_cmutex_free_at_location (DBusCMutex **location_p)
109 if (location_p == NULL)
112 if (*location_p != NULL)
113 _dbus_platform_cmutex_free (*location_p);
117 * Locks a mutex. Does nothing if passed a #NULL pointer.
118 * Locks may be recursive if threading implementation initialized
122 _dbus_rmutex_lock (DBusRMutex *mutex)
127 _dbus_platform_rmutex_lock (mutex);
131 * Locks a mutex. Does nothing if passed a #NULL pointer.
132 * Locks may be recursive if threading implementation initialized
136 _dbus_cmutex_lock (DBusCMutex *mutex)
141 _dbus_platform_cmutex_lock (mutex);
145 * Unlocks a mutex. Does nothing if passed a #NULL pointer.
147 * @returns #TRUE on success
150 _dbus_rmutex_unlock (DBusRMutex *mutex)
155 _dbus_platform_rmutex_unlock (mutex);
159 * Unlocks a mutex. Does nothing if passed a #NULL pointer.
161 * @returns #TRUE on success
164 _dbus_cmutex_unlock (DBusCMutex *mutex)
169 _dbus_platform_cmutex_unlock (mutex);
173 * Creates a new condition variable using the function supplied
174 * to dbus_threads_init(), or creates a no-op condition variable
175 * if threads are not initialized. May return #NULL even if
176 * threads are initialized, indicating out-of-memory.
178 * @returns new mutex or #NULL
181 _dbus_condvar_new (void)
183 if (!dbus_threads_init_default ())
186 return _dbus_platform_condvar_new ();
191 * This does the same thing as _dbus_condvar_new. It however
192 * gives another level of indirection by allocating a pointer
193 * to point to the condvar location; this used to be useful.
195 * @returns the location of a new condvar or #NULL on OOM
199 _dbus_condvar_new_at_location (DBusCondVar **location_p)
201 _dbus_assert (location_p != NULL);
203 *location_p = _dbus_condvar_new();
208 * Frees a conditional variable created with dbus_condvar_new(); does
209 * nothing if passed a #NULL pointer.
212 _dbus_condvar_free (DBusCondVar *cond)
217 _dbus_platform_condvar_free (cond);
221 * Frees a condition variable; does nothing if passed a #NULL pointer.
224 _dbus_condvar_free_at_location (DBusCondVar **location_p)
226 if (location_p == NULL)
229 if (*location_p != NULL)
230 _dbus_platform_condvar_free (*location_p);
234 * Atomically unlocks the mutex and waits for the conditions
235 * variable to be signalled. Locks the mutex again before
237 * Does nothing if passed a #NULL pointer.
240 _dbus_condvar_wait (DBusCondVar *cond,
243 if (cond == NULL || mutex == NULL)
246 _dbus_platform_condvar_wait (cond, mutex);
250 * Atomically unlocks the mutex and waits for the conditions variable
251 * to be signalled, or for a timeout. Locks the mutex again before
252 * returning. Does nothing if passed a #NULL pointer. Return value
253 * is #FALSE if we timed out, #TRUE otherwise.
255 * @param cond the condition variable
256 * @param mutex the mutex
257 * @param timeout_milliseconds the maximum time to wait
258 * @returns #FALSE if the timeout occurred, #TRUE if not
261 _dbus_condvar_wait_timeout (DBusCondVar *cond,
263 int timeout_milliseconds)
265 if (cond == NULL || mutex == NULL)
268 return _dbus_platform_condvar_wait_timeout (cond, mutex,
269 timeout_milliseconds);
273 * If there are threads waiting on the condition variable, wake
275 * Does nothing if passed a #NULL pointer.
278 _dbus_condvar_wake_one (DBusCondVar *cond)
283 _dbus_platform_condvar_wake_one (cond);
286 static DBusRMutex *global_locks[_DBUS_N_GLOBAL_LOCKS] = { NULL };
289 shutdown_global_locks (void *nil)
293 for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
295 _dbus_assert (global_locks[i] != NULL);
296 _dbus_platform_rmutex_free (global_locks[i]);
297 global_locks[i] = NULL;
302 init_global_locks (void)
307 for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
309 _dbus_assert (global_locks[i] == NULL);
311 global_locks[i] = _dbus_platform_rmutex_new ();
313 if (global_locks[i] == NULL)
317 _dbus_platform_rmutex_lock (global_locks[_DBUS_LOCK_shutdown_funcs]);
318 ok = _dbus_register_shutdown_func_unlocked (shutdown_global_locks, NULL);
319 _dbus_platform_rmutex_unlock (global_locks[_DBUS_LOCK_shutdown_funcs]);
327 for (i = i - 1; i >= 0; i--)
329 _dbus_platform_rmutex_free (global_locks[i]);
330 global_locks[i] = NULL;
337 _dbus_lock (DBusGlobalLock lock)
339 _dbus_assert (lock >= 0);
340 _dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
342 if (thread_init_generation != _dbus_current_generation &&
343 !dbus_threads_init_default ())
346 _dbus_platform_rmutex_lock (global_locks[lock]);
351 _dbus_unlock (DBusGlobalLock lock)
353 _dbus_assert (lock >= 0);
354 _dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
356 _dbus_platform_rmutex_unlock (global_locks[lock]);
359 /** @} */ /* end of internals */
362 * @defgroup DBusThreads Thread functions
364 * @brief dbus_threads_init() and dbus_threads_init_default()
366 * Functions and macros related to threads and thread locks.
368 * If threads are initialized, the D-Bus library has locks on all
369 * global data structures. In addition, each #DBusConnection has a
370 * lock, so only one thread at a time can touch the connection. (See
371 * @ref DBusConnection for more on connection locking.)
373 * Most other objects, however, do not have locks - they can only be
374 * used from a single thread at a time, unless you lock them yourself.
375 * For example, a #DBusMessage can't be modified from two threads
382 * Initializes threads, like dbus_threads_init_default().
383 * This version previously allowed user-specified threading
384 * primitives, but since D-Bus 1.6 it ignores them and behaves
385 * exactly like dbus_threads_init_default().
387 * @param functions ignored, formerly functions for using threads
388 * @returns #TRUE on success, #FALSE if no memory
391 dbus_threads_init (const DBusThreadFunctions *functions)
393 _dbus_threads_lock_platform_specific ();
395 if (thread_init_generation == _dbus_current_generation)
397 _dbus_threads_unlock_platform_specific ();
401 if (!_dbus_threads_init_platform_specific() ||
402 !init_global_locks ())
404 _dbus_threads_unlock_platform_specific ();
408 thread_init_generation = _dbus_current_generation;
410 _dbus_threads_unlock_platform_specific ();
416 /* Default thread implemenation */
419 * Initializes threads. If this function is not called, the D-Bus
420 * library will not lock any data structures. If it is called, D-Bus
421 * will do locking, at some cost in efficiency.
423 * Since D-Bus 1.7 it is safe to call this function from any thread,
424 * any number of times (but it must be called before any other
425 * libdbus API is used).
427 * In D-Bus 1.6 or older, this function must be called in the main thread
428 * before any other thread starts. As a result, it is not sufficient to
429 * call this function in a library or plugin, unless the library or plugin
430 * imposes a similar requirement on its callers.
432 * dbus_shutdown() reverses the effects of this function when it
433 * resets all global state in libdbus.
435 * @returns #TRUE on success, #FALSE if not enough memory
438 dbus_threads_init_default (void)
440 return dbus_threads_init (NULL);
446 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
449 _dbus_threads_init_debug (void)
451 return dbus_threads_init (NULL);
454 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */