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;
31 static DBusList *uninitialized_rmutex_list = NULL;
32 static DBusList *uninitialized_cmutex_list = NULL;
33 static DBusList *uninitialized_condvar_list = NULL;
35 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
36 #define _DBUS_DUMMY_MUTEX ((DBusMutex*)0xABCDEF)
37 #define _DBUS_DUMMY_RMUTEX ((DBusRMutex *) _DBUS_DUMMY_MUTEX)
38 #define _DBUS_DUMMY_CMUTEX ((DBusCMutex *) _DBUS_DUMMY_MUTEX)
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 DBusThreadsInternals Thread functions
45 * @ingroup DBusInternals
46 * @brief _dbus_rmutex_lock(), etc.
48 * Functions and macros related to threads and thread locks.
55 * or creates a no-op mutex if threads are not initialized.
56 * May return #NULL even if threads are initialized, indicating
59 * If possible, the mutex returned by this function is recursive, to
60 * avoid deadlocks. However, that cannot be relied on.
62 * The extra level of indirection given by allocating a pointer
63 * to point to the mutex location allows the threading
64 * module to swap out dummy mutexes for a real mutex so libraries
65 * can initialize threads even after the D-Bus API has been used.
67 * @param location_p the location of the new mutex, can return #NULL on OOM
70 _dbus_rmutex_new_at_location (DBusRMutex **location_p)
72 _dbus_assert (location_p != NULL);
74 if (thread_init_generation == _dbus_current_generation)
76 *location_p = _dbus_platform_rmutex_new ();
80 *location_p = _DBUS_DUMMY_RMUTEX;
82 if (!_dbus_list_append (&uninitialized_rmutex_list, location_p))
89 * or creates a no-op mutex if threads are not initialized.
90 * May return #NULL even if threads are initialized, indicating
93 * The returned mutex is suitable for use with condition variables.
95 * The extra level of indirection given by allocating a pointer
96 * to point to the mutex location allows the threading
97 * module to swap out dummy mutexes for a real mutex so libraries
98 * can initialize threads even after the D-Bus API has been used.
100 * @param location_p the location of the new mutex, can return #NULL on OOM
103 _dbus_cmutex_new_at_location (DBusCMutex **location_p)
105 _dbus_assert (location_p != NULL);
107 if (thread_init_generation == _dbus_current_generation)
109 *location_p = _dbus_platform_cmutex_new ();
113 *location_p = _DBUS_DUMMY_CMUTEX;
115 if (!_dbus_list_append (&uninitialized_cmutex_list, location_p))
121 * Frees a DBusRMutex or removes it from the uninitialized mutex list;
122 * does nothing if passed a #NULL pointer.
125 _dbus_rmutex_free_at_location (DBusRMutex **location_p)
127 if (location_p == NULL)
130 if (thread_init_generation == _dbus_current_generation)
132 if (*location_p != NULL)
133 _dbus_platform_rmutex_free (*location_p);
137 _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_RMUTEX);
139 _dbus_list_remove (&uninitialized_rmutex_list, location_p);
144 * Frees a DBusCMutex and removes it from the
145 * uninitialized mutex list;
146 * does nothing if passed a #NULL pointer.
149 _dbus_cmutex_free_at_location (DBusCMutex **location_p)
151 if (location_p == NULL)
154 if (thread_init_generation == _dbus_current_generation)
156 if (*location_p != NULL)
157 _dbus_platform_cmutex_free (*location_p);
161 _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_CMUTEX);
163 _dbus_list_remove (&uninitialized_cmutex_list, location_p);
168 * Locks a mutex. Does nothing if passed a #NULL pointer.
169 * Locks may be recursive if threading implementation initialized
173 _dbus_rmutex_lock (DBusRMutex *mutex)
175 if (mutex && thread_init_generation == _dbus_current_generation)
176 _dbus_platform_rmutex_lock (mutex);
180 * Locks a mutex. Does nothing if passed a #NULL pointer.
181 * Locks may be recursive if threading implementation initialized
185 _dbus_cmutex_lock (DBusCMutex *mutex)
187 if (mutex && thread_init_generation == _dbus_current_generation)
188 _dbus_platform_cmutex_lock (mutex);
192 * Unlocks a mutex. Does nothing if passed a #NULL pointer.
194 * @returns #TRUE on success
197 _dbus_rmutex_unlock (DBusRMutex *mutex)
199 if (mutex && thread_init_generation == _dbus_current_generation)
200 _dbus_platform_rmutex_unlock (mutex);
204 * Unlocks a mutex. Does nothing if passed a #NULL pointer.
206 * @returns #TRUE on success
209 _dbus_cmutex_unlock (DBusCMutex *mutex)
211 if (mutex && thread_init_generation == _dbus_current_generation)
212 _dbus_platform_cmutex_unlock (mutex);
216 * Creates a new condition variable using the function supplied
217 * to dbus_threads_init(), or creates a no-op condition variable
218 * if threads are not initialized. May return #NULL even if
219 * threads are initialized, indicating out-of-memory.
221 * @returns new mutex or #NULL
224 _dbus_condvar_new (void)
226 if (thread_init_generation == _dbus_current_generation)
227 return _dbus_platform_condvar_new ();
229 return _DBUS_DUMMY_CONDVAR;
234 * This does the same thing as _dbus_condvar_new. It however
235 * gives another level of indirection by allocating a pointer
236 * to point to the condvar location. This allows the threading
237 * module to swap out dummy condvars for a real condvar so libraries
238 * can initialize threads even after the D-Bus API has been used.
240 * @returns the location of a new condvar or #NULL on OOM
244 _dbus_condvar_new_at_location (DBusCondVar **location_p)
246 _dbus_assert (location_p != NULL);
248 if (thread_init_generation == _dbus_current_generation)
250 *location_p = _dbus_condvar_new();
254 *location_p = _DBUS_DUMMY_CONDVAR;
256 if (!_dbus_list_append (&uninitialized_condvar_list, location_p))
263 * Frees a conditional variable created with dbus_condvar_new(); does
264 * nothing if passed a #NULL pointer.
267 _dbus_condvar_free (DBusCondVar *cond)
269 if (cond && thread_init_generation == _dbus_current_generation)
270 _dbus_platform_condvar_free (cond);
274 * Frees a conditional variable and removes it from the
275 * uninitialized_condvar_list;
276 * does nothing if passed a #NULL pointer.
279 _dbus_condvar_free_at_location (DBusCondVar **location_p)
281 if (location_p == NULL)
284 if (thread_init_generation == _dbus_current_generation)
286 if (*location_p != NULL)
287 _dbus_platform_condvar_free (*location_p);
291 _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_CONDVAR);
293 _dbus_list_remove (&uninitialized_condvar_list, location_p);
298 * Atomically unlocks the mutex and waits for the conditions
299 * variable to be signalled. Locks the mutex again before
301 * Does nothing if passed a #NULL pointer.
304 _dbus_condvar_wait (DBusCondVar *cond,
307 if (cond && mutex && thread_init_generation == _dbus_current_generation)
308 _dbus_platform_condvar_wait (cond, mutex);
312 * Atomically unlocks the mutex and waits for the conditions variable
313 * to be signalled, or for a timeout. Locks the mutex again before
314 * returning. Does nothing if passed a #NULL pointer. Return value
315 * is #FALSE if we timed out, #TRUE otherwise.
317 * @param cond the condition variable
318 * @param mutex the mutex
319 * @param timeout_milliseconds the maximum time to wait
320 * @returns #FALSE if the timeout occurred, #TRUE if not
323 _dbus_condvar_wait_timeout (DBusCondVar *cond,
325 int timeout_milliseconds)
327 if (cond && mutex && thread_init_generation == _dbus_current_generation)
328 return _dbus_platform_condvar_wait_timeout (cond, mutex,
329 timeout_milliseconds);
335 * If there are threads waiting on the condition variable, wake
337 * Does nothing if passed a #NULL pointer.
340 _dbus_condvar_wake_one (DBusCondVar *cond)
342 if (cond && thread_init_generation == _dbus_current_generation)
343 _dbus_platform_condvar_wake_one (cond);
347 * If there are threads waiting on the condition variable, wake
349 * Does nothing if passed a #NULL pointer.
352 _dbus_condvar_wake_all (DBusCondVar *cond)
354 if (cond && thread_init_generation == _dbus_current_generation)
355 _dbus_platform_condvar_wake_all (cond);
359 shutdown_global_locks (void *data)
361 DBusRMutex ***locks = data;
365 while (i < _DBUS_N_GLOBAL_LOCKS)
367 if (*(locks[i]) != NULL)
368 _dbus_platform_rmutex_free (*(locks[i]));
378 shutdown_uninitialized_locks (void *data)
380 _dbus_list_clear (&uninitialized_rmutex_list);
381 _dbus_list_clear (&uninitialized_cmutex_list);
382 _dbus_list_clear (&uninitialized_condvar_list);
386 init_uninitialized_locks (void)
390 _dbus_assert (thread_init_generation != _dbus_current_generation);
392 link = uninitialized_rmutex_list;
398 _dbus_assert (*mp == _DBUS_DUMMY_RMUTEX);
400 *mp = _dbus_platform_rmutex_new ();
404 link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
407 link = uninitialized_cmutex_list;
413 _dbus_assert (*mp == _DBUS_DUMMY_CMUTEX);
415 *mp = _dbus_platform_cmutex_new ();
419 link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
422 link = uninitialized_condvar_list;
427 cp = (DBusCondVar **)link->data;
428 _dbus_assert (*cp == _DBUS_DUMMY_CONDVAR);
430 *cp = _dbus_platform_condvar_new ();
434 link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
437 _dbus_list_clear (&uninitialized_rmutex_list);
438 _dbus_list_clear (&uninitialized_cmutex_list);
439 _dbus_list_clear (&uninitialized_condvar_list);
441 if (!_dbus_register_shutdown_func (shutdown_uninitialized_locks,
448 link = uninitialized_condvar_list;
455 if (*cp != _DBUS_DUMMY_CONDVAR && *cp != NULL)
456 _dbus_platform_condvar_free (*cp);
458 *cp = _DBUS_DUMMY_CONDVAR;
460 link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
464 link = uninitialized_rmutex_list;
471 if (*mp != _DBUS_DUMMY_RMUTEX && *mp != NULL)
472 _dbus_platform_rmutex_free (*mp);
474 *mp = _DBUS_DUMMY_RMUTEX;
476 link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
479 link = uninitialized_cmutex_list;
486 if (*mp != _DBUS_DUMMY_CMUTEX && *mp != NULL)
487 _dbus_platform_cmutex_free (*mp);
489 *mp = _DBUS_DUMMY_CMUTEX;
491 link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
501 DBusRMutex ***dynamic_global_locks;
502 DBusRMutex **global_locks[] = {
503 #define LOCK_ADDR(name) (& _dbus_lock_##name)
505 LOCK_ADDR (sid_atom_cache),
507 LOCK_ADDR (connection_slots),
508 LOCK_ADDR (pending_call_slots),
509 LOCK_ADDR (server_slots),
510 LOCK_ADDR (message_slots),
515 LOCK_ADDR (bus_datas),
516 LOCK_ADDR (shutdown_funcs),
517 LOCK_ADDR (system_users),
518 LOCK_ADDR (message_cache),
519 LOCK_ADDR (shared_connections),
520 LOCK_ADDR (machine_uuid)
524 _dbus_assert (_DBUS_N_ELEMENTS (global_locks) ==
525 _DBUS_N_GLOBAL_LOCKS);
529 dynamic_global_locks = dbus_new (DBusRMutex**, _DBUS_N_GLOBAL_LOCKS);
530 if (dynamic_global_locks == NULL)
533 while (i < _DBUS_N_ELEMENTS (global_locks))
535 *global_locks[i] = _dbus_platform_rmutex_new ();
537 if (*global_locks[i] == NULL)
540 dynamic_global_locks[i] = global_locks[i];
545 if (!_dbus_register_shutdown_func (shutdown_global_locks,
546 dynamic_global_locks))
549 if (!init_uninitialized_locks ())
555 dbus_free (dynamic_global_locks);
557 for (i = i - 1; i >= 0; i--)
559 _dbus_platform_rmutex_free (*global_locks[i]);
560 *global_locks[i] = NULL;
565 /** @} */ /* end of internals */
568 * @defgroup DBusThreads Thread functions
570 * @brief dbus_threads_init() and dbus_threads_init_default()
572 * Functions and macros related to threads and thread locks.
574 * If threads are initialized, the D-Bus library has locks on all
575 * global data structures. In addition, each #DBusConnection has a
576 * lock, so only one thread at a time can touch the connection. (See
577 * @ref DBusConnection for more on connection locking.)
579 * Most other objects, however, do not have locks - they can only be
580 * used from a single thread at a time, unless you lock them yourself.
581 * For example, a #DBusMessage can't be modified from two threads
588 * Initializes threads, like dbus_threads_init_default().
589 * This version previously allowed user-specified threading
590 * primitives, but since D-Bus 1.6 it ignores them and behaves
591 * exactly like dbus_threads_init_default().
593 * @param functions ignored, formerly functions for using threads
594 * @returns #TRUE on success, #FALSE if no memory
597 dbus_threads_init (const DBusThreadFunctions *functions)
599 if (thread_init_generation == _dbus_current_generation)
605 thread_init_generation = _dbus_current_generation;
612 /* Default thread implemenation */
615 * Initializes threads. If this function is not called, the D-Bus
616 * library will not lock any data structures. If it is called, D-Bus
617 * will do locking, at some cost in efficiency. Note that this
618 * function must be called BEFORE the second thread is started.
620 * It's safe to call dbus_threads_init_default() as many times as you
621 * want, but only the first time will have an effect.
623 * dbus_shutdown() reverses the effects of this function when it
624 * resets all global state in libdbus.
626 * @returns #TRUE on success, #FALSE if not enough memory
629 dbus_threads_init_default (void)
631 return _dbus_threads_init_platform_specific ();
637 #ifdef DBUS_BUILD_TESTS
640 _dbus_threads_init_debug (void)
642 return _dbus_threads_init_platform_specific();
645 #endif /* DBUS_BUILD_TESTS */