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 DBusThreadFunctions thread_functions =
32 NULL, NULL, NULL, NULL, NULL,
33 NULL, NULL, NULL, NULL, NULL,
34 NULL, NULL, NULL, NULL,
36 NULL, NULL, NULL, NULL
39 static int thread_init_generation = 0;
41 static DBusList *uninitialized_rmutex_list = NULL;
42 static DBusList *uninitialized_cmutex_list = NULL;
43 static DBusList *uninitialized_condvar_list = NULL;
45 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
46 #define _DBUS_DUMMY_MUTEX ((DBusMutex*)0xABCDEF)
48 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
49 #define _DBUS_DUMMY_CONDVAR ((DBusCondVar*)0xABCDEF2)
51 static void _dbus_mutex_free (DBusMutex *mutex, DBusMutexFreeFunction dtor);
52 static void _dbus_mutex_new_at_location (DBusMutex **location_p,
53 DBusMutexNewFunction ctor,
54 DBusMutexFreeFunction dtor,
55 DBusList **uninitialized);
56 static void _dbus_mutex_free_at_location (DBusMutex **location_p,
57 DBusMutexFreeFunction dtor,
58 DBusList **uninitialized);
61 * @defgroup DBusThreadsInternals Thread functions
62 * @ingroup DBusInternals
63 * @brief _dbus_rmutex_lock(), etc.
65 * Functions and macros related to threads and thread locks.
71 _dbus_mutex_new (DBusMutexNewFunction ctor)
75 else if (thread_functions.recursive_mutex_new)
76 return (* thread_functions.recursive_mutex_new) ();
77 else if (thread_functions.mutex_new)
78 return (* thread_functions.mutex_new) ();
80 return _DBUS_DUMMY_MUTEX;
84 * Creates a new mutex using the function supplied to dbus_threads_init(),
85 * or creates a no-op mutex if threads are not initialized.
86 * May return #NULL even if threads are initialized, indicating
89 * If possible, the mutex returned by this function is recursive, to
90 * avoid deadlocks. However, that cannot be relied on.
92 * The extra level of indirection given by allocating a pointer
93 * to point to the mutex location allows the threading
94 * module to swap out dummy mutexes for real a real mutex so libraries
95 * can initialize threads even after the D-Bus API has been used.
97 * @param location_p the location of the new mutex, can return #NULL on OOM
100 _dbus_rmutex_new_at_location (DBusRMutex **location_p)
102 _dbus_mutex_new_at_location ((DBusMutex **) location_p,
103 thread_functions.recursive_mutex_new,
104 thread_functions.recursive_mutex_free,
105 &uninitialized_rmutex_list);
109 * Creates a new mutex using the function supplied to dbus_threads_init(),
110 * or creates a no-op mutex if threads are not initialized.
111 * May return #NULL even if threads are initialized, indicating
114 * The returned mutex is suitable for use with condition variables.
116 * The extra level of indirection given by allocating a pointer
117 * to point to the mutex location allows the threading
118 * module to swap out dummy mutexes for real a real mutex so libraries
119 * can initialize threads even after the D-Bus API has been used.
121 * @param location_p the location of the new mutex, can return #NULL on OOM
124 _dbus_cmutex_new_at_location (DBusCMutex **location_p)
126 _dbus_mutex_new_at_location ((DBusMutex **) location_p,
127 thread_functions.mutex_new,
128 thread_functions.mutex_free,
129 &uninitialized_cmutex_list);
133 _dbus_mutex_new_at_location (DBusMutex **location_p,
134 DBusMutexNewFunction ctor,
135 DBusMutexFreeFunction dtor,
136 DBusList **uninitialized)
138 _dbus_assert (location_p != NULL);
140 *location_p = _dbus_mutex_new (ctor);
142 if (thread_init_generation != _dbus_current_generation && *location_p)
144 if (!_dbus_list_append (uninitialized, location_p))
146 _dbus_mutex_free (*location_p, dtor);
153 _dbus_mutex_free (DBusMutex *mutex,
154 DBusMutexFreeFunction dtor)
160 else if (mutex && thread_functions.recursive_mutex_free)
161 (* thread_functions.recursive_mutex_free) (mutex);
162 else if (mutex && thread_functions.mutex_free)
163 (* thread_functions.mutex_free) (mutex);
168 _dbus_mutex_free_at_location (DBusMutex **location_p,
169 DBusMutexFreeFunction dtor,
170 DBusList **uninitialized)
174 if (thread_init_generation != _dbus_current_generation)
175 _dbus_list_remove (uninitialized, location_p);
177 _dbus_mutex_free (*location_p, dtor);
182 * Frees a DBusRMutex and removes it from the
183 * uninitialized mutex list;
184 * does nothing if passed a #NULL pointer.
187 _dbus_rmutex_free_at_location (DBusRMutex **location_p)
189 _dbus_mutex_free_at_location ((DBusMutex **) location_p,
190 thread_functions.recursive_mutex_free,
191 &uninitialized_rmutex_list);
195 * Frees a DBusCMutex and removes it from the
196 * uninitialized mutex list;
197 * does nothing if passed a #NULL pointer.
200 _dbus_cmutex_free_at_location (DBusCMutex **location_p)
202 _dbus_mutex_free_at_location ((DBusMutex **) location_p,
203 thread_functions.mutex_free,
204 &uninitialized_cmutex_list);
208 * Locks a mutex. Does nothing if passed a #NULL pointer.
209 * Locks may be recursive if threading implementation initialized
213 _dbus_rmutex_lock (DBusRMutex *mutex)
217 if (thread_functions.recursive_mutex_lock)
218 (* thread_functions.recursive_mutex_lock) ((DBusMutex *) mutex);
219 else if (thread_functions.mutex_lock)
220 (* thread_functions.mutex_lock) ((DBusMutex *) mutex);
225 * Locks a mutex. Does nothing if passed a #NULL pointer.
226 * Locks may be recursive if threading implementation initialized
230 _dbus_cmutex_lock (DBusCMutex *mutex)
234 if (thread_functions.mutex_lock)
235 (* thread_functions.mutex_lock) ((DBusMutex *) mutex);
236 else if (thread_functions.recursive_mutex_lock)
237 (* thread_functions.recursive_mutex_lock) ((DBusMutex *) mutex);
242 * Unlocks a mutex. Does nothing if passed a #NULL pointer.
244 * @returns #TRUE on success
247 _dbus_rmutex_unlock (DBusRMutex *mutex)
251 if (thread_functions.recursive_mutex_unlock)
252 (* thread_functions.recursive_mutex_unlock) ((DBusMutex *) mutex);
253 else if (thread_functions.mutex_unlock)
254 (* thread_functions.mutex_unlock) ((DBusMutex *) mutex);
259 * Unlocks a mutex. Does nothing if passed a #NULL pointer.
261 * @returns #TRUE on success
264 _dbus_cmutex_unlock (DBusCMutex *mutex)
268 if (thread_functions.mutex_unlock)
269 (* thread_functions.mutex_unlock) ((DBusMutex *) mutex);
270 else if (thread_functions.recursive_mutex_unlock)
271 (* thread_functions.recursive_mutex_unlock) ((DBusMutex *) mutex);
276 * Creates a new condition variable using the function supplied
277 * to dbus_threads_init(), or creates a no-op condition variable
278 * if threads are not initialized. May return #NULL even if
279 * threads are initialized, indicating out-of-memory.
281 * @returns new mutex or #NULL
284 _dbus_condvar_new (void)
286 if (thread_functions.condvar_new)
287 return (* thread_functions.condvar_new) ();
289 return _DBUS_DUMMY_CONDVAR;
294 * This does the same thing as _dbus_condvar_new. It however
295 * gives another level of indirection by allocating a pointer
296 * to point to the condvar location. This allows the threading
297 * module to swap out dummy condvars for real a real condvar so libraries
298 * can initialize threads even after the D-Bus API has been used.
300 * @returns the location of a new condvar or #NULL on OOM
304 _dbus_condvar_new_at_location (DBusCondVar **location_p)
306 *location_p = _dbus_condvar_new();
308 if (thread_init_generation != _dbus_current_generation && *location_p)
310 if (!_dbus_list_append (&uninitialized_condvar_list, location_p))
312 _dbus_condvar_free (*location_p);
320 * Frees a conditional variable created with dbus_condvar_new(); does
321 * nothing if passed a #NULL pointer.
324 _dbus_condvar_free (DBusCondVar *cond)
326 if (cond && thread_functions.condvar_free)
327 (* thread_functions.condvar_free) (cond);
331 * Frees a conditional variable and removes it from the
332 * uninitialized_condvar_list;
333 * does nothing if passed a #NULL pointer.
336 _dbus_condvar_free_at_location (DBusCondVar **location_p)
340 if (thread_init_generation != _dbus_current_generation)
341 _dbus_list_remove (&uninitialized_condvar_list, location_p);
343 _dbus_condvar_free (*location_p);
348 * Atomically unlocks the mutex and waits for the conditions
349 * variable to be signalled. Locks the mutex again before
351 * Does nothing if passed a #NULL pointer.
354 _dbus_condvar_wait (DBusCondVar *cond,
357 if (cond && mutex && thread_functions.condvar_wait)
358 (* thread_functions.condvar_wait) (cond, (DBusMutex *) mutex);
362 * Atomically unlocks the mutex and waits for the conditions variable
363 * to be signalled, or for a timeout. Locks the mutex again before
364 * returning. Does nothing if passed a #NULL pointer. Return value
365 * is #FALSE if we timed out, #TRUE otherwise.
367 * @param cond the condition variable
368 * @param mutex the mutex
369 * @param timeout_milliseconds the maximum time to wait
370 * @returns #FALSE if the timeout occurred, #TRUE if not
373 _dbus_condvar_wait_timeout (DBusCondVar *cond,
375 int timeout_milliseconds)
377 if (cond && mutex && thread_functions.condvar_wait)
378 return (* thread_functions.condvar_wait_timeout) (cond,
380 timeout_milliseconds);
386 * If there are threads waiting on the condition variable, wake
388 * Does nothing if passed a #NULL pointer.
391 _dbus_condvar_wake_one (DBusCondVar *cond)
393 if (cond && thread_functions.condvar_wake_one)
394 (* thread_functions.condvar_wake_one) (cond);
398 * If there are threads waiting on the condition variable, wake
400 * Does nothing if passed a #NULL pointer.
403 _dbus_condvar_wake_all (DBusCondVar *cond)
405 if (cond && thread_functions.condvar_wake_all)
406 (* thread_functions.condvar_wake_all) (cond);
410 shutdown_global_locks (void *data)
412 DBusMutex ***locks = data;
416 while (i < _DBUS_N_GLOBAL_LOCKS)
418 _dbus_mutex_free (*(locks[i]), thread_functions.recursive_mutex_free);
427 shutdown_uninitialized_locks (void *data)
429 _dbus_list_clear (&uninitialized_rmutex_list);
430 _dbus_list_clear (&uninitialized_cmutex_list);
431 _dbus_list_clear (&uninitialized_condvar_list);
435 init_uninitialized_locks (void)
439 _dbus_assert (thread_init_generation != _dbus_current_generation);
441 link = uninitialized_rmutex_list;
447 _dbus_assert (*mp == _DBUS_DUMMY_MUTEX);
449 *mp = _dbus_mutex_new (thread_functions.recursive_mutex_new);
453 link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
456 link = uninitialized_cmutex_list;
462 _dbus_assert (*mp == _DBUS_DUMMY_MUTEX);
464 *mp = _dbus_mutex_new (thread_functions.mutex_new);
468 link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
471 link = uninitialized_condvar_list;
476 cp = (DBusCondVar **)link->data;
477 _dbus_assert (*cp == _DBUS_DUMMY_CONDVAR);
479 *cp = _dbus_condvar_new ();
483 link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
486 _dbus_list_clear (&uninitialized_rmutex_list);
487 _dbus_list_clear (&uninitialized_cmutex_list);
488 _dbus_list_clear (&uninitialized_condvar_list);
490 if (!_dbus_register_shutdown_func (shutdown_uninitialized_locks,
497 link = uninitialized_condvar_list;
502 cp = (DBusCondVar **)link->data;
504 if (*cp != _DBUS_DUMMY_CONDVAR)
505 _dbus_condvar_free (*cp);
509 *cp = _DBUS_DUMMY_CONDVAR;
511 link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
515 link = uninitialized_rmutex_list;
522 if (*mp != _DBUS_DUMMY_MUTEX)
523 _dbus_mutex_free (*mp, thread_functions.recursive_mutex_free);
527 *mp = _DBUS_DUMMY_MUTEX;
529 link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
532 link = uninitialized_cmutex_list;
539 if (*mp != _DBUS_DUMMY_MUTEX)
540 _dbus_mutex_free (*mp, thread_functions.mutex_free);
544 *mp = _DBUS_DUMMY_MUTEX;
546 link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
556 DBusRMutex ***dynamic_global_locks;
557 DBusRMutex **global_locks[] = {
558 #define LOCK_ADDR(name) (& _dbus_lock_##name)
560 LOCK_ADDR (sid_atom_cache),
562 LOCK_ADDR (connection_slots),
563 LOCK_ADDR (pending_call_slots),
564 LOCK_ADDR (server_slots),
565 LOCK_ADDR (message_slots),
570 LOCK_ADDR (bus_datas),
571 LOCK_ADDR (shutdown_funcs),
572 LOCK_ADDR (system_users),
573 LOCK_ADDR (message_cache),
574 LOCK_ADDR (shared_connections),
575 LOCK_ADDR (machine_uuid)
579 _dbus_assert (_DBUS_N_ELEMENTS (global_locks) ==
580 _DBUS_N_GLOBAL_LOCKS);
584 dynamic_global_locks = dbus_new (DBusRMutex**, _DBUS_N_GLOBAL_LOCKS);
585 if (dynamic_global_locks == NULL)
588 while (i < _DBUS_N_ELEMENTS (global_locks))
590 *global_locks[i] = (DBusRMutex *) _dbus_mutex_new (thread_functions.recursive_mutex_new);
592 if (*global_locks[i] == NULL)
595 dynamic_global_locks[i] = global_locks[i];
600 if (!_dbus_register_shutdown_func (shutdown_global_locks,
601 dynamic_global_locks))
604 if (!init_uninitialized_locks ())
610 dbus_free (dynamic_global_locks);
612 for (i = i - 1; i >= 0; i--)
614 _dbus_mutex_free ((DBusMutex *) *global_locks[i],
615 thread_functions.recursive_mutex_free);
616 *global_locks[i] = NULL;
621 /** @} */ /* end of internals */
624 * @defgroup DBusThreads Thread functions
626 * @brief dbus_threads_init() and dbus_threads_init_default()
628 * Functions and macros related to threads and thread locks.
630 * If threads are initialized, the D-Bus library has locks on all
631 * global data structures. In addition, each #DBusConnection has a
632 * lock, so only one thread at a time can touch the connection. (See
633 * @ref DBusConnection for more on connection locking.)
635 * Most other objects, however, do not have locks - they can only be
636 * used from a single thread at a time, unless you lock them yourself.
637 * For example, a #DBusMessage can't be modified from two threads
645 * Initializes threads. If this function is not called, the D-Bus
646 * library will not lock any data structures. If it is called, D-Bus
647 * will do locking, at some cost in efficiency. Note that this
648 * function must be called BEFORE the second thread is started.
650 * Almost always, you should use dbus_threads_init_default() instead.
651 * The raw dbus_threads_init() is only useful if you require a
652 * particular thread implementation for some reason.
654 * A possible reason to use dbus_threads_init() rather than
655 * dbus_threads_init_default() is to insert debugging checks or print
658 * dbus_threads_init() may be called more than once. The first one
659 * wins and subsequent calls are ignored. (Unless you use
660 * dbus_shutdown() to reset libdbus, which will let you re-init
663 * Either recursive or nonrecursive mutex functions must be specified,
664 * but not both. New code should provide only the recursive functions
665 * - specifying the nonrecursive ones is deprecated.
667 * Because this function effectively sets global state, all code
668 * running in a given application must agree on the thread
669 * implementation. Most code won't care which thread implementation is
670 * used, so there's no problem. However, usually libraries should not
671 * call dbus_threads_init() or dbus_threads_init_default(), instead
672 * leaving this policy choice to applications.
674 * The exception is for application frameworks (GLib, Qt, etc.) and
675 * D-Bus bindings based on application frameworks. These frameworks
676 * define a cross-platform thread abstraction and can assume
677 * applications using the framework are OK with using that thread
680 * However, even these app frameworks may find it easier to simply call
681 * dbus_threads_init_default(), and there's no reason they shouldn't.
683 * @param functions functions for using threads
684 * @returns #TRUE on success, #FALSE if no memory
687 dbus_threads_init (const DBusThreadFunctions *functions)
689 dbus_bool_t mutex_set;
690 dbus_bool_t recursive_mutex_set;
692 _dbus_assert (functions != NULL);
694 /* these base functions are required. Future additions to
695 * DBusThreadFunctions may be optional.
697 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK);
698 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK);
699 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK);
700 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK);
701 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK);
702 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK);
703 _dbus_assert (functions->condvar_new != NULL);
704 _dbus_assert (functions->condvar_free != NULL);
705 _dbus_assert (functions->condvar_wait != NULL);
706 _dbus_assert (functions->condvar_wait_timeout != NULL);
707 _dbus_assert (functions->condvar_wake_one != NULL);
708 _dbus_assert (functions->condvar_wake_all != NULL);
710 /* Either the mutex function set or recursive mutex set needs
711 * to be available but not both
713 mutex_set = (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK) &&
714 (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK) &&
715 (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK) &&
716 (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK) &&
717 functions->mutex_new &&
718 functions->mutex_free &&
719 functions->mutex_lock &&
720 functions->mutex_unlock;
722 recursive_mutex_set =
723 (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK) &&
724 (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK) &&
725 (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK) &&
726 (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK) &&
727 functions->recursive_mutex_new &&
728 functions->recursive_mutex_free &&
729 functions->recursive_mutex_lock &&
730 functions->recursive_mutex_unlock;
732 if (!(mutex_set || recursive_mutex_set))
733 _dbus_assert_not_reached ("Either the nonrecusrive or recursive mutex "
734 "functions sets should be passed into "
735 "dbus_threads_init. Neither sets were passed.");
737 /* Check that all bits in the mask actually are valid mask bits.
738 * ensures people won't write code that breaks when we add
741 _dbus_assert ((functions->mask & ~DBUS_THREAD_FUNCTIONS_ALL_MASK) == 0);
743 if (thread_init_generation != _dbus_current_generation)
744 thread_functions.mask = 0; /* allow re-init in new generation */
746 /* Silently allow multiple init
747 * First init wins and D-Bus will always use its threading system
749 if (thread_functions.mask != 0)
752 thread_functions.mutex_new = functions->mutex_new;
753 thread_functions.mutex_free = functions->mutex_free;
754 thread_functions.mutex_lock = functions->mutex_lock;
755 thread_functions.mutex_unlock = functions->mutex_unlock;
757 thread_functions.condvar_new = functions->condvar_new;
758 thread_functions.condvar_free = functions->condvar_free;
759 thread_functions.condvar_wait = functions->condvar_wait;
760 thread_functions.condvar_wait_timeout = functions->condvar_wait_timeout;
761 thread_functions.condvar_wake_one = functions->condvar_wake_one;
762 thread_functions.condvar_wake_all = functions->condvar_wake_all;
764 if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK)
765 thread_functions.recursive_mutex_new = functions->recursive_mutex_new;
767 thread_functions.recursive_mutex_new = NULL;
769 if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK)
770 thread_functions.recursive_mutex_free = functions->recursive_mutex_free;
772 thread_functions.recursive_mutex_free = NULL;
774 if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK)
775 thread_functions.recursive_mutex_lock = functions->recursive_mutex_lock;
777 thread_functions.recursive_mutex_lock = NULL;
779 if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK)
780 thread_functions.recursive_mutex_unlock = functions->recursive_mutex_unlock;
782 thread_functions.recursive_mutex_unlock = NULL;
784 thread_functions.mask = functions->mask;
789 thread_init_generation = _dbus_current_generation;
796 /* Default thread implemenation */
800 * Calls dbus_threads_init() with a default set of
801 * #DBusThreadFunctions appropriate for the platform.
803 * Most applications should use this rather than dbus_threads_init().
805 * It's safe to call dbus_threads_init_default() as many times as you
806 * want, but only the first time will have an effect.
808 * dbus_shutdown() reverses the effects of this function when it
809 * resets all global state in libdbus.
811 * @returns #TRUE on success, #FALSE if not enough memory
814 dbus_threads_init_default (void)
816 return _dbus_threads_init_platform_specific ();
822 #ifdef DBUS_BUILD_TESTS
823 /** Fake mutex used for debugging */
824 typedef struct DBusFakeMutex DBusFakeMutex;
825 /** Fake mutex used for debugging */
828 dbus_bool_t locked; /**< Mutex is "locked" */
831 static DBusMutex * dbus_fake_mutex_new (void);
832 static void dbus_fake_mutex_free (DBusMutex *mutex);
833 static dbus_bool_t dbus_fake_mutex_lock (DBusMutex *mutex);
834 static dbus_bool_t dbus_fake_mutex_unlock (DBusMutex *mutex);
835 static DBusCondVar* dbus_fake_condvar_new (void);
836 static void dbus_fake_condvar_free (DBusCondVar *cond);
837 static void dbus_fake_condvar_wait (DBusCondVar *cond,
839 static dbus_bool_t dbus_fake_condvar_wait_timeout (DBusCondVar *cond,
842 static void dbus_fake_condvar_wake_one (DBusCondVar *cond);
843 static void dbus_fake_condvar_wake_all (DBusCondVar *cond);
846 static const DBusThreadFunctions fake_functions =
848 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
849 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
850 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
851 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
852 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
853 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
854 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
855 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
856 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
857 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
859 dbus_fake_mutex_free,
860 dbus_fake_mutex_lock,
861 dbus_fake_mutex_unlock,
862 dbus_fake_condvar_new,
863 dbus_fake_condvar_free,
864 dbus_fake_condvar_wait,
865 dbus_fake_condvar_wait_timeout,
866 dbus_fake_condvar_wake_one,
867 dbus_fake_condvar_wake_all
871 dbus_fake_mutex_new (void)
873 DBusFakeMutex *mutex;
875 mutex = dbus_new0 (DBusFakeMutex, 1);
877 return (DBusMutex *)mutex;
881 dbus_fake_mutex_free (DBusMutex *mutex)
883 DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
885 _dbus_assert (!fake->locked);
891 dbus_fake_mutex_lock (DBusMutex *mutex)
893 DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
895 _dbus_assert (!fake->locked);
903 dbus_fake_mutex_unlock (DBusMutex *mutex)
905 DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
907 _dbus_assert (fake->locked);
909 fake->locked = FALSE;
915 dbus_fake_condvar_new (void)
917 return (DBusCondVar*) _dbus_strdup ("FakeCondvar");
921 dbus_fake_condvar_free (DBusCondVar *cond)
927 dbus_fake_condvar_wait (DBusCondVar *cond,
934 dbus_fake_condvar_wait_timeout (DBusCondVar *cond,
942 dbus_fake_condvar_wake_one (DBusCondVar *cond)
948 dbus_fake_condvar_wake_all (DBusCondVar *cond)
954 _dbus_threads_init_debug (void)
957 return _dbus_threads_init_platform_specific();
959 return dbus_threads_init (&fake_functions);
963 #endif /* DBUS_BUILD_TESTS */