Make _dbus_mutex_new, _dbus_mutex_free static
[platform/upstream/dbus.git] / dbus / dbus-threads.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-threads.h  D-Bus threads handling
3  *
4  * Copyright (C) 2002, 2003, 2006 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23 #include <config.h>
24 #include "dbus-threads.h"
25 #include "dbus-internals.h"
26 #include "dbus-threads-internal.h"
27 #include "dbus-list.h"
28
29 static DBusThreadFunctions thread_functions =
30 {
31   0,
32   NULL, NULL, NULL, NULL, NULL,
33   NULL, NULL, NULL, NULL, NULL,
34   NULL, NULL, NULL, NULL,
35   
36   NULL, NULL, NULL, NULL
37 };
38
39 static int thread_init_generation = 0;
40  
41 static DBusList *uninitialized_mutex_list = NULL;
42 static DBusList *uninitialized_condvar_list = NULL;
43
44 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
45 #define _DBUS_DUMMY_MUTEX ((DBusMutex*)0xABCDEF)
46
47 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
48 #define _DBUS_DUMMY_CONDVAR ((DBusCondVar*)0xABCDEF2)
49
50 static void _dbus_mutex_free (DBusMutex *mutex);
51
52 /**
53  * @defgroup DBusThreadsInternals Thread functions
54  * @ingroup  DBusInternals
55  * @brief _dbus_mutex_lock(), etc.
56  *
57  * Functions and macros related to threads and thread locks.
58  *
59  * @{
60  */
61
62 static DBusMutex *
63 _dbus_mutex_new (void)
64 {
65   if (thread_functions.recursive_mutex_new)
66     return (* thread_functions.recursive_mutex_new) ();
67   else if (thread_functions.mutex_new)
68     return (* thread_functions.mutex_new) ();
69   else
70     return _DBUS_DUMMY_MUTEX;
71 }
72
73 /**
74  * Creates a new mutex using the function supplied to dbus_threads_init(),
75  * or creates a no-op mutex if threads are not initialized.
76  * May return #NULL even if threads are initialized, indicating
77  * out-of-memory.
78  *
79  * The extra level of indirection given by allocating a pointer
80  * to point to the mutex location allows the threading
81  * module to swap out dummy mutexes for real a real mutex so libraries
82  * can initialize threads even after the D-Bus API has been used.
83  *
84  * @param location_p the location of the new mutex, can return #NULL on OOM
85  */
86 void
87 _dbus_mutex_new_at_location (DBusMutex **location_p)
88 {
89   _dbus_assert (location_p != NULL);
90
91   *location_p = _dbus_mutex_new();
92
93   if (thread_init_generation != _dbus_current_generation && *location_p)
94     {
95       if (!_dbus_list_append (&uninitialized_mutex_list, location_p))
96         {
97           _dbus_mutex_free (*location_p);
98           *location_p = NULL;
99         }
100     }
101 }
102
103 static void
104 _dbus_mutex_free (DBusMutex *mutex)
105 {
106   if (mutex)
107     {
108       if (mutex && thread_functions.recursive_mutex_free)
109         (* thread_functions.recursive_mutex_free) (mutex);
110       else if (mutex && thread_functions.mutex_free)
111         (* thread_functions.mutex_free) (mutex);
112     }
113 }
114
115 /**
116  * Frees a mutex and removes it from the 
117  * uninitialized_mutex_list;
118  * does nothing if passed a #NULL pointer.
119  */
120 void
121 _dbus_mutex_free_at_location (DBusMutex **location_p)
122 {
123   if (location_p)
124     {
125       if (thread_init_generation != _dbus_current_generation)
126         _dbus_list_remove (&uninitialized_mutex_list, location_p);
127
128       _dbus_mutex_free (*location_p);
129     }
130 }
131
132 /**
133  * Locks a mutex. Does nothing if passed a #NULL pointer.
134  * Locks may be recursive if threading implementation initialized
135  * recursive locks.
136  */
137 void
138 _dbus_mutex_lock (DBusMutex *mutex)
139 {
140   if (mutex) 
141     {
142       if (thread_functions.recursive_mutex_lock)
143         (* thread_functions.recursive_mutex_lock) (mutex);
144       else if (thread_functions.mutex_lock)
145         (* thread_functions.mutex_lock) (mutex);
146     }
147 }
148
149 /**
150  * Unlocks a mutex. Does nothing if passed a #NULL pointer.
151  *
152  * @returns #TRUE on success
153  */
154 void
155 _dbus_mutex_unlock (DBusMutex *mutex)
156 {
157   if (mutex)
158     {
159       if (thread_functions.recursive_mutex_unlock)
160         (* thread_functions.recursive_mutex_unlock) (mutex);
161       else if (thread_functions.mutex_unlock)
162         (* thread_functions.mutex_unlock) (mutex);
163     }
164 }
165
166 /**
167  * Creates a new condition variable using the function supplied
168  * to dbus_threads_init(), or creates a no-op condition variable
169  * if threads are not initialized. May return #NULL even if
170  * threads are initialized, indicating out-of-memory.
171  *
172  * @returns new mutex or #NULL
173  */
174 DBusCondVar *
175 _dbus_condvar_new (void)
176 {
177   if (thread_functions.condvar_new)
178     return (* thread_functions.condvar_new) ();
179   else
180     return _DBUS_DUMMY_CONDVAR;
181 }
182
183
184 /**
185  * This does the same thing as _dbus_condvar_new.  It however
186  * gives another level of indirection by allocating a pointer
187  * to point to the condvar location.  This allows the threading
188  * module to swap out dummy condvars for real a real condvar so libraries
189  * can initialize threads even after the D-Bus API has been used.
190  *
191  * @returns the location of a new condvar or #NULL on OOM
192  */
193
194 void 
195 _dbus_condvar_new_at_location (DBusCondVar **location_p)
196 {
197   *location_p = _dbus_condvar_new();
198
199   if (thread_init_generation != _dbus_current_generation && *location_p)
200     {
201       if (!_dbus_list_append (&uninitialized_condvar_list, location_p))
202         {
203           _dbus_condvar_free (*location_p);
204           *location_p = NULL;
205         }
206     }
207 }
208
209
210 /**
211  * Frees a conditional variable created with dbus_condvar_new(); does
212  * nothing if passed a #NULL pointer.
213  */
214 void
215 _dbus_condvar_free (DBusCondVar *cond)
216 {
217   if (cond && thread_functions.condvar_free)
218     (* thread_functions.condvar_free) (cond);
219 }
220
221 /**
222  * Frees a conditional variable and removes it from the 
223  * uninitialized_condvar_list; 
224  * does nothing if passed a #NULL pointer.
225  */
226 void
227 _dbus_condvar_free_at_location (DBusCondVar **location_p)
228 {
229   if (location_p)
230     {
231       if (thread_init_generation != _dbus_current_generation)
232         _dbus_list_remove (&uninitialized_condvar_list, location_p);
233
234       _dbus_condvar_free (*location_p);
235     }
236 }
237
238 /**
239  * Atomically unlocks the mutex and waits for the conditions
240  * variable to be signalled. Locks the mutex again before
241  * returning.
242  * Does nothing if passed a #NULL pointer.
243  */
244 void
245 _dbus_condvar_wait (DBusCondVar *cond,
246                     DBusMutex   *mutex)
247 {
248   if (cond && mutex && thread_functions.condvar_wait)
249     (* thread_functions.condvar_wait) (cond, mutex);
250 }
251
252 /**
253  * Atomically unlocks the mutex and waits for the conditions variable
254  * to be signalled, or for a timeout. Locks the mutex again before
255  * returning.  Does nothing if passed a #NULL pointer.  Return value
256  * is #FALSE if we timed out, #TRUE otherwise.
257  *
258  * @param cond the condition variable
259  * @param mutex the mutex
260  * @param timeout_milliseconds the maximum time to wait
261  * @returns #FALSE if the timeout occurred, #TRUE if not
262  */
263 dbus_bool_t
264 _dbus_condvar_wait_timeout (DBusCondVar               *cond,
265                             DBusMutex                 *mutex,
266                             int                        timeout_milliseconds)
267 {
268   if (cond && mutex && thread_functions.condvar_wait)
269     return (* thread_functions.condvar_wait_timeout) (cond, mutex, timeout_milliseconds);
270   else
271     return TRUE;
272 }
273
274 /**
275  * If there are threads waiting on the condition variable, wake
276  * up exactly one. 
277  * Does nothing if passed a #NULL pointer.
278  */
279 void
280 _dbus_condvar_wake_one (DBusCondVar *cond)
281 {
282   if (cond && thread_functions.condvar_wake_one)
283     (* thread_functions.condvar_wake_one) (cond);
284 }
285
286 /**
287  * If there are threads waiting on the condition variable, wake
288  * up all of them. 
289  * Does nothing if passed a #NULL pointer.
290  */
291 void
292 _dbus_condvar_wake_all (DBusCondVar *cond)
293 {
294   if (cond && thread_functions.condvar_wake_all)
295     (* thread_functions.condvar_wake_all) (cond);
296 }
297
298 static void
299 shutdown_global_locks (void *data)
300 {
301   DBusMutex ***locks = data;
302   int i;
303
304   i = 0;
305   while (i < _DBUS_N_GLOBAL_LOCKS)
306     {
307       _dbus_mutex_free (*(locks[i]));
308       *(locks[i]) = NULL;
309       ++i;
310     }
311   
312   dbus_free (locks);
313 }
314
315 static void
316 shutdown_uninitialized_locks (void *data)
317 {
318   _dbus_list_clear (&uninitialized_mutex_list);
319   _dbus_list_clear (&uninitialized_condvar_list);
320 }
321
322 static dbus_bool_t
323 init_uninitialized_locks (void)
324 {
325   DBusList *link;
326
327   _dbus_assert (thread_init_generation != _dbus_current_generation);
328
329   link = uninitialized_mutex_list;
330   while (link != NULL)
331     {
332       DBusMutex **mp;
333
334       mp = (DBusMutex **)link->data;
335       _dbus_assert (*mp == _DBUS_DUMMY_MUTEX);
336
337       *mp = _dbus_mutex_new ();
338       if (*mp == NULL)
339         goto fail_mutex;
340
341       link = _dbus_list_get_next_link (&uninitialized_mutex_list, link);
342     }
343
344   link = uninitialized_condvar_list;
345   while (link != NULL)
346     {
347       DBusCondVar **cp;
348
349       cp = (DBusCondVar **)link->data;
350       _dbus_assert (*cp == _DBUS_DUMMY_CONDVAR);
351
352       *cp = _dbus_condvar_new ();
353       if (*cp == NULL)
354         goto fail_condvar;
355
356       link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
357     }
358
359   _dbus_list_clear (&uninitialized_mutex_list);
360   _dbus_list_clear (&uninitialized_condvar_list);
361
362   if (!_dbus_register_shutdown_func (shutdown_uninitialized_locks,
363                                      NULL))
364     goto fail_condvar;
365
366   return TRUE;
367
368  fail_condvar:
369   link = uninitialized_condvar_list;
370   while (link != NULL)
371     {
372       DBusCondVar **cp;
373
374       cp = (DBusCondVar **)link->data;
375
376       if (*cp != _DBUS_DUMMY_CONDVAR)
377         _dbus_condvar_free (*cp);
378       else
379         break;
380
381       *cp = _DBUS_DUMMY_CONDVAR;
382
383       link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
384     }
385
386  fail_mutex:
387   link = uninitialized_mutex_list;
388   while (link != NULL)
389     {
390       DBusMutex **mp;
391
392       mp = (DBusMutex **)link->data;
393
394       if (*mp != _DBUS_DUMMY_MUTEX)
395         _dbus_mutex_free (*mp);
396       else
397         break;
398
399       *mp = _DBUS_DUMMY_MUTEX;
400
401       link = _dbus_list_get_next_link (&uninitialized_mutex_list, link);
402     }
403
404   return FALSE;
405 }
406
407 static dbus_bool_t
408 init_locks (void)
409 {
410   int i;
411   DBusMutex ***dynamic_global_locks;
412   
413   DBusMutex **global_locks[] = {
414 #define LOCK_ADDR(name) (& _dbus_lock_##name)
415     LOCK_ADDR (win_fds),
416     LOCK_ADDR (sid_atom_cache),
417     LOCK_ADDR (list),
418     LOCK_ADDR (connection_slots),
419     LOCK_ADDR (pending_call_slots),
420     LOCK_ADDR (server_slots),
421     LOCK_ADDR (message_slots),
422 #if !DBUS_USE_SYNC
423     LOCK_ADDR (atomic),
424 #endif
425     LOCK_ADDR (bus),
426     LOCK_ADDR (bus_datas),
427     LOCK_ADDR (shutdown_funcs),
428     LOCK_ADDR (system_users),
429     LOCK_ADDR (message_cache),
430     LOCK_ADDR (shared_connections),
431     LOCK_ADDR (machine_uuid)
432 #undef LOCK_ADDR
433   };
434
435   _dbus_assert (_DBUS_N_ELEMENTS (global_locks) ==
436                 _DBUS_N_GLOBAL_LOCKS);
437
438   i = 0;
439   
440   dynamic_global_locks = dbus_new (DBusMutex**, _DBUS_N_GLOBAL_LOCKS);
441   if (dynamic_global_locks == NULL)
442     goto failed;
443   
444   while (i < _DBUS_N_ELEMENTS (global_locks))
445     {
446       *global_locks[i] = _dbus_mutex_new ();
447       
448       if (*global_locks[i] == NULL)
449         goto failed;
450
451       dynamic_global_locks[i] = global_locks[i];
452
453       ++i;
454     }
455   
456   if (!_dbus_register_shutdown_func (shutdown_global_locks,
457                                      dynamic_global_locks))
458     goto failed;
459
460   if (!init_uninitialized_locks ())
461     goto failed;
462   
463   return TRUE;
464
465  failed:
466   dbus_free (dynamic_global_locks);
467                                      
468   for (i = i - 1; i >= 0; i--)
469     {
470       _dbus_mutex_free (*global_locks[i]);
471       *global_locks[i] = NULL;
472     }
473   return FALSE;
474 }
475
476 /** @} */ /* end of internals */
477
478 /**
479  * @defgroup DBusThreads Thread functions
480  * @ingroup  DBus
481  * @brief dbus_threads_init() and dbus_threads_init_default()
482  *
483  * Functions and macros related to threads and thread locks.
484  *
485  * If threads are initialized, the D-Bus library has locks on all
486  * global data structures.  In addition, each #DBusConnection has a
487  * lock, so only one thread at a time can touch the connection.  (See
488  * @ref DBusConnection for more on connection locking.)
489  *
490  * Most other objects, however, do not have locks - they can only be
491  * used from a single thread at a time, unless you lock them yourself.
492  * For example, a #DBusMessage can't be modified from two threads
493  * at once.
494  * 
495  * @{
496  */
497
498 /**
499  * 
500  * Initializes threads. If this function is not called, the D-Bus
501  * library will not lock any data structures.  If it is called, D-Bus
502  * will do locking, at some cost in efficiency. Note that this
503  * function must be called BEFORE the second thread is started.
504  *
505  * Almost always, you should use dbus_threads_init_default() instead.
506  * The raw dbus_threads_init() is only useful if you require a
507  * particular thread implementation for some reason.
508  *
509  * A possible reason to use dbus_threads_init() rather than
510  * dbus_threads_init_default() is to insert debugging checks or print
511  * statements.
512  *
513  * dbus_threads_init() may be called more than once.  The first one
514  * wins and subsequent calls are ignored. (Unless you use
515  * dbus_shutdown() to reset libdbus, which will let you re-init
516  * threads.)
517  *
518  * Either recursive or nonrecursive mutex functions must be specified,
519  * but not both. New code should provide only the recursive functions
520  * - specifying the nonrecursive ones is deprecated.
521  *
522  * Because this function effectively sets global state, all code
523  * running in a given application must agree on the thread
524  * implementation. Most code won't care which thread implementation is
525  * used, so there's no problem. However, usually libraries should not
526  * call dbus_threads_init() or dbus_threads_init_default(), instead
527  * leaving this policy choice to applications.
528  *
529  * The exception is for application frameworks (GLib, Qt, etc.)  and
530  * D-Bus bindings based on application frameworks. These frameworks
531  * define a cross-platform thread abstraction and can assume
532  * applications using the framework are OK with using that thread
533  * abstraction.
534  *
535  * However, even these app frameworks may find it easier to simply call
536  * dbus_threads_init_default(), and there's no reason they shouldn't.
537  * 
538  * @param functions functions for using threads
539  * @returns #TRUE on success, #FALSE if no memory
540  */
541 dbus_bool_t
542 dbus_threads_init (const DBusThreadFunctions *functions)
543 {
544   dbus_bool_t mutex_set;
545   dbus_bool_t recursive_mutex_set;
546
547   _dbus_assert (functions != NULL);
548
549   /* these base functions are required. Future additions to
550    * DBusThreadFunctions may be optional.
551    */
552   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK);
553   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK);
554   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK);
555   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK);
556   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK);
557   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK);
558   _dbus_assert (functions->condvar_new != NULL);
559   _dbus_assert (functions->condvar_free != NULL);
560   _dbus_assert (functions->condvar_wait != NULL);
561   _dbus_assert (functions->condvar_wait_timeout != NULL);
562   _dbus_assert (functions->condvar_wake_one != NULL);
563   _dbus_assert (functions->condvar_wake_all != NULL);
564
565   /* Either the mutex function set or recursive mutex set needs 
566    * to be available but not both
567    */
568   mutex_set = (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK) &&  
569               (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK) && 
570               (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK) &&
571               (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK) &&
572                functions->mutex_new &&
573                functions->mutex_free &&
574                functions->mutex_lock &&
575                functions->mutex_unlock;
576
577   recursive_mutex_set = 
578               (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK) && 
579               (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK) && 
580               (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK) && 
581               (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK) &&
582                 functions->recursive_mutex_new &&
583                 functions->recursive_mutex_free &&
584                 functions->recursive_mutex_lock &&
585                 functions->recursive_mutex_unlock;
586
587   if (!(mutex_set || recursive_mutex_set))
588     _dbus_assert_not_reached ("Either the nonrecusrive or recursive mutex " 
589                               "functions sets should be passed into "
590                               "dbus_threads_init. Neither sets were passed.");
591
592   if (mutex_set && recursive_mutex_set)
593     _dbus_assert_not_reached ("Either the nonrecusrive or recursive mutex " 
594                               "functions sets should be passed into "
595                               "dbus_threads_init. Both sets were passed. "
596                               "You most likely just want to set the recursive "
597                               "mutex functions to avoid deadlocks in D-Bus.");
598                           
599   /* Check that all bits in the mask actually are valid mask bits.
600    * ensures people won't write code that breaks when we add
601    * new bits.
602    */
603   _dbus_assert ((functions->mask & ~DBUS_THREAD_FUNCTIONS_ALL_MASK) == 0);
604
605   if (thread_init_generation != _dbus_current_generation)
606     thread_functions.mask = 0; /* allow re-init in new generation */
607  
608   /* Silently allow multiple init
609    * First init wins and D-Bus will always use its threading system 
610    */ 
611   if (thread_functions.mask != 0)
612     return TRUE;
613   
614   thread_functions.mutex_new = functions->mutex_new;
615   thread_functions.mutex_free = functions->mutex_free;
616   thread_functions.mutex_lock = functions->mutex_lock;
617   thread_functions.mutex_unlock = functions->mutex_unlock;
618   
619   thread_functions.condvar_new = functions->condvar_new;
620   thread_functions.condvar_free = functions->condvar_free;
621   thread_functions.condvar_wait = functions->condvar_wait;
622   thread_functions.condvar_wait_timeout = functions->condvar_wait_timeout;
623   thread_functions.condvar_wake_one = functions->condvar_wake_one;
624   thread_functions.condvar_wake_all = functions->condvar_wake_all;
625  
626   if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK)
627     thread_functions.recursive_mutex_new = functions->recursive_mutex_new;
628   
629   if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK)
630     thread_functions.recursive_mutex_free = functions->recursive_mutex_free;
631   
632   if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK)
633     thread_functions.recursive_mutex_lock = functions->recursive_mutex_lock;
634
635   if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK)
636     thread_functions.recursive_mutex_unlock = functions->recursive_mutex_unlock;
637
638   thread_functions.mask = functions->mask;
639
640   if (!init_locks ())
641     return FALSE;
642
643   thread_init_generation = _dbus_current_generation;
644   
645   return TRUE;
646 }
647
648
649
650 /* Default thread implemenation */
651
652 /**
653  *
654  * Calls dbus_threads_init() with a default set of
655  * #DBusThreadFunctions appropriate for the platform.
656  *
657  * Most applications should use this rather than dbus_threads_init().
658  *
659  * It's safe to call dbus_threads_init_default() as many times as you
660  * want, but only the first time will have an effect.
661  *
662  * dbus_shutdown() reverses the effects of this function when it
663  * resets all global state in libdbus.
664  * 
665  * @returns #TRUE on success, #FALSE if not enough memory
666  */
667 dbus_bool_t
668 dbus_threads_init_default (void)
669 {
670   return _dbus_threads_init_platform_specific ();
671 }
672
673
674 /** @} */
675
676 #ifdef DBUS_BUILD_TESTS
677 /** Fake mutex used for debugging */
678 typedef struct DBusFakeMutex DBusFakeMutex;
679 /** Fake mutex used for debugging */
680 struct DBusFakeMutex
681 {
682   dbus_bool_t locked; /**< Mutex is "locked" */
683 };      
684
685 static DBusMutex *  dbus_fake_mutex_new            (void);
686 static void         dbus_fake_mutex_free           (DBusMutex   *mutex);
687 static dbus_bool_t  dbus_fake_mutex_lock           (DBusMutex   *mutex);
688 static dbus_bool_t  dbus_fake_mutex_unlock         (DBusMutex   *mutex);
689 static DBusCondVar* dbus_fake_condvar_new          (void);
690 static void         dbus_fake_condvar_free         (DBusCondVar *cond);
691 static void         dbus_fake_condvar_wait         (DBusCondVar *cond,
692                                                     DBusMutex   *mutex);
693 static dbus_bool_t  dbus_fake_condvar_wait_timeout (DBusCondVar *cond,
694                                                     DBusMutex   *mutex,
695                                                     int          timeout_msec);
696 static void         dbus_fake_condvar_wake_one     (DBusCondVar *cond);
697 static void         dbus_fake_condvar_wake_all     (DBusCondVar *cond);
698
699
700 static const DBusThreadFunctions fake_functions =
701 {
702   DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
703   DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
704   DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
705   DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
706   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
707   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
708   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
709   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
710   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
711   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
712   dbus_fake_mutex_new,
713   dbus_fake_mutex_free,
714   dbus_fake_mutex_lock,
715   dbus_fake_mutex_unlock,
716   dbus_fake_condvar_new,
717   dbus_fake_condvar_free,
718   dbus_fake_condvar_wait,
719   dbus_fake_condvar_wait_timeout,
720   dbus_fake_condvar_wake_one,
721   dbus_fake_condvar_wake_all
722 };
723
724 static DBusMutex *
725 dbus_fake_mutex_new (void)
726 {
727   DBusFakeMutex *mutex;
728
729   mutex = dbus_new0 (DBusFakeMutex, 1);
730
731   return (DBusMutex *)mutex;
732 }
733
734 static void
735 dbus_fake_mutex_free (DBusMutex *mutex)
736 {
737   DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
738
739   _dbus_assert (!fake->locked);
740   
741   dbus_free (fake);
742 }
743
744 static dbus_bool_t
745 dbus_fake_mutex_lock (DBusMutex *mutex)
746 {
747   DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
748
749   _dbus_assert (!fake->locked);
750
751   fake->locked = TRUE;
752   
753   return TRUE;
754 }
755
756 static dbus_bool_t
757 dbus_fake_mutex_unlock (DBusMutex *mutex)
758 {
759   DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
760
761   _dbus_assert (fake->locked);
762
763   fake->locked = FALSE;
764   
765   return TRUE;
766 }
767
768 static DBusCondVar*
769 dbus_fake_condvar_new (void)
770 {
771   return (DBusCondVar*) _dbus_strdup ("FakeCondvar");
772 }
773
774 static void
775 dbus_fake_condvar_free (DBusCondVar *cond)
776 {
777   dbus_free (cond);
778 }
779
780 static void
781 dbus_fake_condvar_wait (DBusCondVar *cond,
782                         DBusMutex   *mutex)
783 {
784   
785 }
786
787 static dbus_bool_t
788 dbus_fake_condvar_wait_timeout (DBusCondVar *cond,
789                                 DBusMutex   *mutex,
790                                 int         timeout_msec)
791 {
792   return TRUE;
793 }
794
795 static void
796 dbus_fake_condvar_wake_one (DBusCondVar *cond)
797 {
798
799 }
800
801 static void
802 dbus_fake_condvar_wake_all (DBusCondVar *cond)
803 {
804
805 }
806
807 dbus_bool_t
808 _dbus_threads_init_debug (void)
809 {
810 #ifdef DBUS_WIN
811   return _dbus_threads_init_platform_specific();
812 #else
813   return dbus_threads_init (&fake_functions);
814 #endif
815 }
816
817 #endif /* DBUS_BUILD_TESTS */