D-Bus  1.6.12
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 int thread_init_generation = 0;
30 
31 static DBusList *uninitialized_rmutex_list = NULL;
32 static DBusList *uninitialized_cmutex_list = NULL;
33 static DBusList *uninitialized_condvar_list = NULL;
34 
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)
39 
41 #define _DBUS_DUMMY_CONDVAR ((DBusCondVar*)0xABCDEF2)
42 
69 void
71 {
72  _dbus_assert (location_p != NULL);
73 
74  if (thread_init_generation == _dbus_current_generation)
75  {
76  *location_p = _dbus_platform_rmutex_new ();
77  }
78  else
79  {
80  *location_p = _DBUS_DUMMY_RMUTEX;
81 
82  if (!_dbus_list_append (&uninitialized_rmutex_list, location_p))
83  *location_p = NULL;
84  }
85 }
86 
102 void
104 {
105  _dbus_assert (location_p != NULL);
106 
107  if (thread_init_generation == _dbus_current_generation)
108  {
109  *location_p = _dbus_platform_cmutex_new ();
110  }
111  else
112  {
113  *location_p = _DBUS_DUMMY_CMUTEX;
114 
115  if (!_dbus_list_append (&uninitialized_cmutex_list, location_p))
116  *location_p = NULL;
117  }
118 }
119 
124 void
126 {
127  if (location_p == NULL)
128  return;
129 
130  if (thread_init_generation == _dbus_current_generation)
131  {
132  if (*location_p != NULL)
133  _dbus_platform_rmutex_free (*location_p);
134  }
135  else
136  {
137  _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_RMUTEX);
138 
139  _dbus_list_remove (&uninitialized_rmutex_list, location_p);
140  }
141 }
142 
148 void
150 {
151  if (location_p == NULL)
152  return;
153 
154  if (thread_init_generation == _dbus_current_generation)
155  {
156  if (*location_p != NULL)
157  _dbus_platform_cmutex_free (*location_p);
158  }
159  else
160  {
161  _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_CMUTEX);
162 
163  _dbus_list_remove (&uninitialized_cmutex_list, location_p);
164  }
165 }
166 
172 void
174 {
175  if (mutex && thread_init_generation == _dbus_current_generation)
176  _dbus_platform_rmutex_lock (mutex);
177 }
178 
184 void
186 {
187  if (mutex && thread_init_generation == _dbus_current_generation)
188  _dbus_platform_cmutex_lock (mutex);
189 }
190 
196 void
198 {
199  if (mutex && thread_init_generation == _dbus_current_generation)
200  _dbus_platform_rmutex_unlock (mutex);
201 }
202 
208 void
210 {
211  if (mutex && thread_init_generation == _dbus_current_generation)
212  _dbus_platform_cmutex_unlock (mutex);
213 }
214 
223 DBusCondVar *
225 {
226  if (thread_init_generation == _dbus_current_generation)
227  return _dbus_platform_condvar_new ();
228  else
229  return _DBUS_DUMMY_CONDVAR;
230 }
231 
232 
243 void
245 {
246  _dbus_assert (location_p != NULL);
247 
248  if (thread_init_generation == _dbus_current_generation)
249  {
250  *location_p = _dbus_condvar_new();
251  }
252  else
253  {
254  *location_p = _DBUS_DUMMY_CONDVAR;
255 
256  if (!_dbus_list_append (&uninitialized_condvar_list, location_p))
257  *location_p = NULL;
258  }
259 }
260 
261 
266 void
268 {
269  if (cond && thread_init_generation == _dbus_current_generation)
270  _dbus_platform_condvar_free (cond);
271 }
272 
278 void
280 {
281  if (location_p == NULL)
282  return;
283 
284  if (thread_init_generation == _dbus_current_generation)
285  {
286  if (*location_p != NULL)
287  _dbus_platform_condvar_free (*location_p);
288  }
289  else
290  {
291  _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_CONDVAR);
292 
293  _dbus_list_remove (&uninitialized_condvar_list, location_p);
294  }
295 }
296 
303 void
305  DBusCMutex *mutex)
306 {
307  if (cond && mutex && thread_init_generation == _dbus_current_generation)
308  _dbus_platform_condvar_wait (cond, mutex);
309 }
310 
324  DBusCMutex *mutex,
325  int timeout_milliseconds)
326 {
327  if (cond && mutex && thread_init_generation == _dbus_current_generation)
328  return _dbus_platform_condvar_wait_timeout (cond, mutex,
329  timeout_milliseconds);
330  else
331  return TRUE;
332 }
333 
339 void
341 {
342  if (cond && thread_init_generation == _dbus_current_generation)
343  _dbus_platform_condvar_wake_one (cond);
344 }
345 
346 static void
347 shutdown_global_locks (void *data)
348 {
349  DBusRMutex ***locks = data;
350  int i;
351 
352  i = 0;
353  while (i < _DBUS_N_GLOBAL_LOCKS)
354  {
355  if (*(locks[i]) != NULL)
356  _dbus_platform_rmutex_free (*(locks[i]));
357 
358  *(locks[i]) = NULL;
359  ++i;
360  }
361 
362  dbus_free (locks);
363 }
364 
365 static void
366 shutdown_uninitialized_locks (void *data)
367 {
368  _dbus_list_clear (&uninitialized_rmutex_list);
369  _dbus_list_clear (&uninitialized_cmutex_list);
370  _dbus_list_clear (&uninitialized_condvar_list);
371 }
372 
373 static dbus_bool_t
374 init_uninitialized_locks (void)
375 {
376  DBusList *link;
377 
378  _dbus_assert (thread_init_generation != _dbus_current_generation);
379 
380  link = uninitialized_rmutex_list;
381  while (link != NULL)
382  {
383  DBusRMutex **mp;
384 
385  mp = link->data;
386  _dbus_assert (*mp == _DBUS_DUMMY_RMUTEX);
387 
388  *mp = _dbus_platform_rmutex_new ();
389  if (*mp == NULL)
390  goto fail_mutex;
391 
392  link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
393  }
394 
395  link = uninitialized_cmutex_list;
396  while (link != NULL)
397  {
398  DBusCMutex **mp;
399 
400  mp = link->data;
401  _dbus_assert (*mp == _DBUS_DUMMY_CMUTEX);
402 
403  *mp = _dbus_platform_cmutex_new ();
404  if (*mp == NULL)
405  goto fail_mutex;
406 
407  link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
408  }
409 
410  link = uninitialized_condvar_list;
411  while (link != NULL)
412  {
413  DBusCondVar **cp;
414 
415  cp = (DBusCondVar **)link->data;
416  _dbus_assert (*cp == _DBUS_DUMMY_CONDVAR);
417 
418  *cp = _dbus_platform_condvar_new ();
419  if (*cp == NULL)
420  goto fail_condvar;
421 
422  link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
423  }
424 
425  _dbus_list_clear (&uninitialized_rmutex_list);
426  _dbus_list_clear (&uninitialized_cmutex_list);
427  _dbus_list_clear (&uninitialized_condvar_list);
428 
429  if (!_dbus_register_shutdown_func (shutdown_uninitialized_locks,
430  NULL))
431  goto fail_condvar;
432 
433  return TRUE;
434 
435  fail_condvar:
436  link = uninitialized_condvar_list;
437  while (link != NULL)
438  {
439  DBusCondVar **cp;
440 
441  cp = link->data;
442 
443  if (*cp != _DBUS_DUMMY_CONDVAR && *cp != NULL)
444  _dbus_platform_condvar_free (*cp);
445 
446  *cp = _DBUS_DUMMY_CONDVAR;
447 
448  link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
449  }
450 
451  fail_mutex:
452  link = uninitialized_rmutex_list;
453  while (link != NULL)
454  {
455  DBusRMutex **mp;
456 
457  mp = link->data;
458 
459  if (*mp != _DBUS_DUMMY_RMUTEX && *mp != NULL)
460  _dbus_platform_rmutex_free (*mp);
461 
462  *mp = _DBUS_DUMMY_RMUTEX;
463 
464  link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
465  }
466 
467  link = uninitialized_cmutex_list;
468  while (link != NULL)
469  {
470  DBusCMutex **mp;
471 
472  mp = link->data;
473 
474  if (*mp != _DBUS_DUMMY_CMUTEX && *mp != NULL)
475  _dbus_platform_cmutex_free (*mp);
476 
477  *mp = _DBUS_DUMMY_CMUTEX;
478 
479  link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
480  }
481 
482  return FALSE;
483 }
484 
485 static dbus_bool_t
486 init_locks (void)
487 {
488  int i;
489  DBusRMutex ***dynamic_global_locks;
490  DBusRMutex **global_locks[] = {
491 #define LOCK_ADDR(name) (& _dbus_lock_##name)
492  LOCK_ADDR (win_fds),
493  LOCK_ADDR (sid_atom_cache),
494  LOCK_ADDR (list),
495  LOCK_ADDR (connection_slots),
496  LOCK_ADDR (pending_call_slots),
497  LOCK_ADDR (server_slots),
498  LOCK_ADDR (message_slots),
499 #if !DBUS_USE_SYNC
500  LOCK_ADDR (atomic),
501 #endif
502  LOCK_ADDR (bus),
503  LOCK_ADDR (bus_datas),
504  LOCK_ADDR (shutdown_funcs),
505  LOCK_ADDR (system_users),
506  LOCK_ADDR (message_cache),
507  LOCK_ADDR (shared_connections),
508  LOCK_ADDR (machine_uuid)
509 #undef LOCK_ADDR
510  };
511 
512  _dbus_assert (_DBUS_N_ELEMENTS (global_locks) ==
513  _DBUS_N_GLOBAL_LOCKS);
514 
515  i = 0;
516 
517  dynamic_global_locks = dbus_new (DBusRMutex**, _DBUS_N_GLOBAL_LOCKS);
518  if (dynamic_global_locks == NULL)
519  goto failed;
520 
521  while (i < _DBUS_N_ELEMENTS (global_locks))
522  {
523  *global_locks[i] = _dbus_platform_rmutex_new ();
524 
525  if (*global_locks[i] == NULL)
526  goto failed;
527 
528  dynamic_global_locks[i] = global_locks[i];
529 
530  ++i;
531  }
532 
533  if (!_dbus_register_shutdown_func (shutdown_global_locks,
534  dynamic_global_locks))
535  goto failed;
536 
537  if (!init_uninitialized_locks ())
538  goto failed;
539 
540  return TRUE;
541 
542  failed:
543  dbus_free (dynamic_global_locks);
544 
545  for (i = i - 1; i >= 0; i--)
546  {
547  _dbus_platform_rmutex_free (*global_locks[i]);
548  *global_locks[i] = NULL;
549  }
550  return FALSE;
551 }
552  /* end of internals */
554 
586 {
587  if (thread_init_generation == _dbus_current_generation)
588  return TRUE;
589 
590  if (!init_locks ())
591  return FALSE;
592 
593  thread_init_generation = _dbus_current_generation;
594 
595  return TRUE;
596 }
597 
598 
599 
600 /* Default thread implemenation */
601 
618 {
620 }
621 
622 
625 #ifdef DBUS_BUILD_TESTS
626 
628 _dbus_threads_init_debug (void)
629 {
631 }
632 
633 #endif /* DBUS_BUILD_TESTS */
Functions that must be implemented to make the D-Bus library thread-aware.
Definition: dbus-threads.h:152
#define NULL
A null pointer, defined appropriately for C or C++.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
void _dbus_condvar_wait(DBusCondVar *cond, DBusCMutex *mutex)
Atomically unlocks the mutex and waits for the conditions variable to be signalled.
Definition: dbus-threads.c:304
dbus_bool_t _dbus_condvar_wait_timeout(DBusCondVar *cond, DBusCMutex *mutex, int timeout_milliseconds)
Atomically unlocks the mutex and waits for the conditions variable to be signalled, or for a timeout.
Definition: dbus-threads.c:323
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:58
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void * data
Data stored at this element.
Definition: dbus-list.h:38
dbus_bool_t _dbus_threads_init_platform_specific(void)
Initialize threads as in dbus_threads_init_default(), appropriately for the platform.
void _dbus_rmutex_new_at_location(DBusRMutex **location_p)
Creates a new mutex or creates a no-op mutex if threads are not initialized.
Definition: dbus-threads.c:70
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
Definition: dbus-list.c:404
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
Definition: dbus-list.h:90
void _dbus_rmutex_unlock(DBusRMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:197
void _dbus_condvar_free(DBusCondVar *cond)
Frees a conditional variable created with dbus_condvar_new(); does nothing if passed a NULL pointer...
Definition: dbus-threads.c:267
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:259
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called, so we can reinit things after it&#39;s been called.
Definition: dbus-memory.c:780
void _dbus_cmutex_unlock(DBusCMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:209
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
#define TRUE
Expands to &quot;1&quot;.
void _dbus_condvar_new_at_location(DBusCondVar **location_p)
This does the same thing as _dbus_condvar_new.
Definition: dbus-threads.c:244
void _dbus_condvar_wake_one(DBusCondVar *cond)
If there are threads waiting on the condition variable, wake up exactly one.
Definition: dbus-threads.c:340
DBusCondVar * _dbus_condvar_new(void)
Creates a new condition variable using the function supplied to dbus_threads_init(), or creates a no-op condition variable if threads are not initialized.
Definition: dbus-threads.c:224
A node in a linked list.
Definition: dbus-list.h:34
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex or removes it from the uninitialized mutex list; does nothing if passed a NULL poi...
Definition: dbus-threads.c:125
void _dbus_cmutex_lock(DBusCMutex *mutex)
Locks a mutex.
Definition: dbus-threads.c:185
void _dbus_rmutex_lock(DBusRMutex *mutex)
Locks a mutex.
Definition: dbus-threads.c:173
#define FALSE
Expands to &quot;0&quot;.
dbus_bool_t _dbus_register_shutdown_func(DBusShutdownFunction function, void *data)
Register a cleanup function to be called exactly once the next time dbus_shutdown() is called...
Definition: dbus-memory.c:809
void _dbus_cmutex_free_at_location(DBusCMutex **location_p)
Frees a DBusCMutex and removes it from the uninitialized mutex list; does nothing if passed a NULL po...
Definition: dbus-threads.c:149
dbus_bool_t dbus_threads_init(const DBusThreadFunctions *functions)
Initializes threads, like dbus_threads_init_default().
Definition: dbus-threads.c:585
dbus_bool_t dbus_threads_init_default(void)
Initializes threads.
Definition: dbus-threads.c:617
void _dbus_condvar_free_at_location(DBusCondVar **location_p)
Frees a conditional variable and removes it from the uninitialized_condvar_list; does nothing if pass...
Definition: dbus-threads.c:279
void _dbus_cmutex_new_at_location(DBusCMutex **location_p)
Creates a new mutex or creates a no-op mutex if threads are not initialized.
Definition: dbus-threads.c:103
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:531