2003-04-27 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-threads.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-threads.h  D-BUS threads handling
3  *
4  * Copyright (C) 2002, 2003 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-threads.h"
24 #include "dbus-internals.h"
25
26 static DBusThreadFunctions thread_functions =
27 {
28   0,
29   NULL, NULL, NULL, NULL,
30   NULL, NULL, NULL, NULL, NULL,
31
32   NULL, NULL, NULL, NULL,
33   NULL, NULL, NULL, NULL
34 };
35 static int thread_init_generation = 0;
36
37 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
38 #define _DBUS_DUMMY_MUTEX ((DBusMutex*)0xABCDEF)
39
40 /** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
41 #define _DBUS_DUMMY_CONDVAR ((DBusCondVar*)0xABCDEF2)
42
43 /**
44  * @defgroup DBusThreads Thread functions
45  * @ingroup  DBus
46  * @brief dbus_threads_init(), dbus_mutex_lock(), etc.
47  *
48  * Functions and macros related to threads and thread locks.
49  *
50  * @{
51  */
52
53 /**
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
57  * out-of-memory.
58  *
59  * @returns new mutex or #NULL
60  */
61 DBusMutex*
62 dbus_mutex_new (void)
63 {
64   if (thread_functions.mutex_new)
65     return (* thread_functions.mutex_new) ();
66   else
67     return _DBUS_DUMMY_MUTEX;
68 }
69
70 /**
71  * Frees a mutex created with dbus_mutex_new(); does
72  * nothing if passed a #NULL pointer.
73  */
74 void
75 dbus_mutex_free (DBusMutex *mutex)
76 {
77   if (mutex && thread_functions.mutex_free)
78     (* thread_functions.mutex_free) (mutex);
79 }
80
81 /**
82  * Locks a mutex. Does nothing if passed a #NULL pointer.
83  * Locks are not recursive.
84  *
85  * @returns #TRUE on success
86  */
87 dbus_bool_t
88 dbus_mutex_lock (DBusMutex *mutex)
89 {
90   if (mutex && thread_functions.mutex_lock)
91     return (* thread_functions.mutex_lock) (mutex);
92   else
93     return TRUE;
94 }
95
96 /**
97  * Unlocks a mutex. Does nothing if passed a #NULL pointer.
98  *
99  * @returns #TRUE on success
100  */
101 dbus_bool_t
102 dbus_mutex_unlock (DBusMutex *mutex)
103 {
104   if (mutex && thread_functions.mutex_unlock)
105     return (* thread_functions.mutex_unlock) (mutex);
106   else
107     return TRUE;
108 }
109
110 /**
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.
115  *
116  * @returns new mutex or #NULL
117  */
118 DBusCondVar *
119 dbus_condvar_new (void)
120 {
121   if (thread_functions.condvar_new)
122     return (* thread_functions.condvar_new) ();
123   else
124     return _DBUS_DUMMY_CONDVAR;
125 }
126
127 /**
128  * Frees a conditional variable created with dbus_condvar_new(); does
129  * nothing if passed a #NULL pointer.
130  */
131 void
132 dbus_condvar_free (DBusCondVar *cond)
133 {
134   if (cond && thread_functions.condvar_free)
135     (* thread_functions.condvar_free) (cond);
136 }
137
138 /**
139  * Atomically unlocks the mutex and waits for the conditions
140  * variable to be signalled. Locks the mutex again before
141  * returning.
142  * Does nothing if passed a #NULL pointer.
143  */
144 void
145 dbus_condvar_wait (DBusCondVar *cond,
146                    DBusMutex   *mutex)
147 {
148   if (cond && mutex && thread_functions.condvar_wait)
149     (* thread_functions.condvar_wait) (cond, mutex);
150 }
151
152 /**
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.
157  *
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.
163  */
164 dbus_bool_t
165 dbus_condvar_wait_timeout (DBusCondVar               *cond,
166                            DBusMutex                 *mutex,
167                            int                        timeout_milliseconds)
168 {
169   if (cond && mutex && thread_functions.condvar_wait)
170     return (* thread_functions.condvar_wait_timeout) (cond, mutex, timeout_milliseconds);
171   else
172     return TRUE;
173 }
174
175 /**
176  * If there are threads waiting on the condition variable, wake
177  * up exactly one. 
178  * Does nothing if passed a #NULL pointer.
179  */
180 void
181 dbus_condvar_wake_one (DBusCondVar *cond)
182 {
183   if (cond && thread_functions.condvar_wake_one)
184     (* thread_functions.condvar_wake_one) (cond);
185 }
186
187 /**
188  * If there are threads waiting on the condition variable, wake
189  * up all of them. 
190  * Does nothing if passed a #NULL pointer.
191  */
192 void
193 dbus_condvar_wake_all (DBusCondVar *cond)
194 {
195   if (cond && thread_functions.condvar_wake_all)
196     (* thread_functions.condvar_wake_all) (cond);
197 }
198
199 static void
200 shutdown_global_locks (void *data)
201 {
202   DBusMutex ***locks = data;
203   int i;
204
205   i = 0;
206   while (i < _DBUS_N_GLOBAL_LOCKS)
207     {
208       dbus_mutex_free (*(locks[i]));
209       *(locks[i]) = NULL;
210       ++i;
211     }
212   
213   dbus_free (locks);
214 }
215
216 static dbus_bool_t
217 init_global_locks (void)
218 {
219   int i;
220   DBusMutex ***dynamic_global_locks;
221   
222   DBusMutex **global_locks[] = {
223 #define LOCK_ADDR(name) (& _dbus_lock_##name)
224     LOCK_ADDR (list),
225     LOCK_ADDR (connection_slots),
226     LOCK_ADDR (server_slots),
227     LOCK_ADDR (atomic),
228     LOCK_ADDR (message_handler),
229     LOCK_ADDR (bus),
230     LOCK_ADDR (shutdown_funcs),
231     LOCK_ADDR (system_users)
232 #undef LOCK_ADDR
233   };
234
235   _dbus_assert (_DBUS_N_ELEMENTS (global_locks) ==
236                 _DBUS_N_GLOBAL_LOCKS);
237
238   i = 0;
239   
240   dynamic_global_locks = dbus_new (DBusMutex**, _DBUS_N_GLOBAL_LOCKS);
241   if (dynamic_global_locks == NULL)
242     goto failed;
243   
244   while (i < _DBUS_N_ELEMENTS (global_locks))
245     {
246       *global_locks[i] = dbus_mutex_new ();
247       
248       if (*global_locks[i] == NULL)
249         goto failed;
250
251       dynamic_global_locks[i] = global_locks[i];
252
253       ++i;
254     }
255   
256   if (!_dbus_register_shutdown_func (shutdown_global_locks,
257                                      dynamic_global_locks))
258     goto failed;
259   
260   return TRUE;
261
262  failed:
263   dbus_free (dynamic_global_locks);
264                                      
265   for (i = i - 1; i >= 0; i--)
266     {
267       dbus_mutex_free (*global_locks[i]);
268       *global_locks[i] = NULL;
269     }
270   return FALSE;
271 }
272
273
274 /**
275  * Initializes threads. If this function is not called,
276  * the D-BUS library will not lock any data structures.
277  * If it is called, D-BUS will do locking, at some cost
278  * in efficiency. Note that this function must be called
279  * BEFORE using any other D-BUS functions.
280  *
281  * @todo right now this function can only be called once,
282  * maybe we should instead silently ignore multiple calls.
283  *
284  * @param functions functions for using threads
285  * @returns #TRUE on success, #FALSE if no memory
286  */
287 dbus_bool_t
288 dbus_threads_init (const DBusThreadFunctions *functions)
289 {
290   _dbus_assert (functions != NULL);
291
292   /* these base functions are required. Future additions to
293    * DBusThreadFunctions may be optional.
294    */
295   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK);
296   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK);
297   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK);
298   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK);
299   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK);
300   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK);
301   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK);
302   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK);
303   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK);
304   _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK);
305   _dbus_assert (functions->mutex_new != NULL);
306   _dbus_assert (functions->mutex_free != NULL);
307   _dbus_assert (functions->mutex_lock != NULL);
308   _dbus_assert (functions->mutex_unlock != NULL);
309   _dbus_assert (functions->condvar_new != NULL);
310   _dbus_assert (functions->condvar_free != NULL);
311   _dbus_assert (functions->condvar_wait != NULL);
312   _dbus_assert (functions->condvar_wait_timeout != NULL);
313   _dbus_assert (functions->condvar_wake_one != NULL);
314   _dbus_assert (functions->condvar_wake_all != NULL);
315
316   /* Check that all bits in the mask actually are valid mask bits.
317    * ensures people won't write code that breaks when we add
318    * new bits.
319    */
320   _dbus_assert ((functions->mask & ~DBUS_THREAD_FUNCTIONS_ALL_MASK) == 0);
321
322   if (thread_init_generation != _dbus_current_generation)
323     thread_functions.mask = 0; /* allow re-init in new generation */
324   
325   if (thread_functions.mask != 0)
326     {
327       _dbus_warn ("dbus_threads_init() may only be called one time\n");
328       return FALSE;
329     }
330   
331   thread_functions.mutex_new = functions->mutex_new;
332   thread_functions.mutex_free = functions->mutex_free;
333   thread_functions.mutex_lock = functions->mutex_lock;
334   thread_functions.mutex_unlock = functions->mutex_unlock;
335   
336   thread_functions.condvar_new = functions->condvar_new;
337   thread_functions.condvar_free = functions->condvar_free;
338   thread_functions.condvar_wait = functions->condvar_wait;
339   thread_functions.condvar_wait_timeout = functions->condvar_wait_timeout;
340   thread_functions.condvar_wake_one = functions->condvar_wake_one;
341   thread_functions.condvar_wake_all = functions->condvar_wake_all;
342   
343   thread_functions.mask = functions->mask;
344
345   if (!init_global_locks ())
346     return FALSE;
347
348   thread_init_generation = _dbus_current_generation;
349   
350   return TRUE;
351 }
352
353 /** @} */
354
355 #ifdef DBUS_BUILD_TESTS
356 /** Fake mutex used for debugging */
357 typedef struct DBusFakeMutex DBusFakeMutex;
358 /** Fake mutex used for debugging */
359 struct DBusFakeMutex
360 {
361   dbus_bool_t locked; /**< Mutex is "locked" */
362 };      
363
364 static DBusMutex *  dbus_fake_mutex_new            (void);
365 static void         dbus_fake_mutex_free           (DBusMutex   *mutex);
366 static dbus_bool_t  dbus_fake_mutex_lock           (DBusMutex   *mutex);
367 static dbus_bool_t  dbus_fake_mutex_unlock         (DBusMutex   *mutex);
368 static DBusCondVar* dbus_fake_condvar_new          (void);
369 static void         dbus_fake_condvar_free         (DBusCondVar *cond);
370 static void         dbus_fake_condvar_wait         (DBusCondVar *cond,
371                                                     DBusMutex   *mutex);
372 static dbus_bool_t  dbus_fake_condvar_wait_timeout (DBusCondVar *cond,
373                                                     DBusMutex   *mutex,
374                                                     int          timeout_msec);
375 static void         dbus_fake_condvar_wake_one     (DBusCondVar *cond);
376 static void         dbus_fake_condvar_wake_all     (DBusCondVar *cond);
377
378
379 static const DBusThreadFunctions fake_functions =
380 {
381   DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
382   DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
383   DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
384   DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
385   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
386   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
387   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
388   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
389   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
390   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
391   dbus_fake_mutex_new,
392   dbus_fake_mutex_free,
393   dbus_fake_mutex_lock,
394   dbus_fake_mutex_unlock,
395   dbus_fake_condvar_new,
396   dbus_fake_condvar_free,
397   dbus_fake_condvar_wait,
398   dbus_fake_condvar_wait_timeout,
399   dbus_fake_condvar_wake_one,
400   dbus_fake_condvar_wake_all
401 };
402
403 static DBusMutex *
404 dbus_fake_mutex_new (void)
405 {
406   DBusFakeMutex *mutex;
407
408   mutex = dbus_new0 (DBusFakeMutex, 1);
409
410   return (DBusMutex *)mutex;
411 }
412
413 static void
414 dbus_fake_mutex_free (DBusMutex *mutex)
415 {
416   DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
417
418   _dbus_assert (!fake->locked);
419   
420   dbus_free (fake);
421 }
422
423 static dbus_bool_t
424 dbus_fake_mutex_lock (DBusMutex *mutex)
425 {
426   DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
427
428   _dbus_assert (!fake->locked);
429
430   fake->locked = TRUE;
431   
432   return TRUE;
433 }
434
435 static dbus_bool_t
436 dbus_fake_mutex_unlock (DBusMutex *mutex)
437 {
438   DBusFakeMutex *fake = (DBusFakeMutex*) mutex;
439
440   _dbus_assert (fake->locked);
441
442   fake->locked = FALSE;
443   
444   return TRUE;
445 }
446
447 static DBusCondVar*
448 dbus_fake_condvar_new (void)
449 {
450   return (DBusCondVar*) _dbus_strdup ("FakeCondvar");
451 }
452
453 static void
454 dbus_fake_condvar_free (DBusCondVar *cond)
455 {
456   dbus_free (cond);
457 }
458
459 static void
460 dbus_fake_condvar_wait (DBusCondVar *cond,
461                     DBusMutex   *mutex)
462 {
463   
464 }
465
466 static dbus_bool_t
467 dbus_fake_condvar_wait_timeout (DBusCondVar *cond,
468                                 DBusMutex   *mutex,
469                                 int         timeout_msec)
470 {
471   return TRUE;
472 }
473
474 static void
475 dbus_fake_condvar_wake_one (DBusCondVar *cond)
476 {
477
478 }
479
480 static void
481 dbus_fake_condvar_wake_all (DBusCondVar *cond)
482 {
483
484 }
485
486 dbus_bool_t
487 _dbus_threads_init_debug (void)
488 {
489   return dbus_threads_init (&fake_functions);
490 }
491
492 #endif /* DBUS_BUILD_TESTS */