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;
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)
41 #define _DBUS_DUMMY_CONDVAR ((DBusCondVar*)0xABCDEF2)
76 *location_p = _dbus_platform_rmutex_new ();
80 *location_p = _DBUS_DUMMY_RMUTEX;
109 *location_p = _dbus_platform_cmutex_new ();
113 *location_p = _DBUS_DUMMY_CMUTEX;
127 if (location_p ==
NULL)
132 if (*location_p !=
NULL)
133 _dbus_platform_rmutex_free (*location_p);
151 if (location_p ==
NULL)
156 if (*location_p !=
NULL)
157 _dbus_platform_cmutex_free (*location_p);
176 _dbus_platform_rmutex_lock (mutex);
188 _dbus_platform_cmutex_lock (mutex);
200 _dbus_platform_rmutex_unlock (mutex);
212 _dbus_platform_cmutex_unlock (mutex);
227 return _dbus_platform_condvar_new ();
229 return _DBUS_DUMMY_CONDVAR;
254 *location_p = _DBUS_DUMMY_CONDVAR;
270 _dbus_platform_condvar_free (cond);
281 if (location_p ==
NULL)
286 if (*location_p !=
NULL)
287 _dbus_platform_condvar_free (*location_p);
308 _dbus_platform_condvar_wait (cond, mutex);
325 int timeout_milliseconds)
328 return _dbus_platform_condvar_wait_timeout (cond, mutex,
329 timeout_milliseconds);
343 _dbus_platform_condvar_wake_one (cond);
347 shutdown_global_locks (
void *data)
353 while (i < _DBUS_N_GLOBAL_LOCKS)
355 if (*(locks[i]) !=
NULL)
356 _dbus_platform_rmutex_free (*(locks[i]));
366 shutdown_uninitialized_locks (
void *data)
374 init_uninitialized_locks (
void)
380 link = uninitialized_rmutex_list;
388 *mp = _dbus_platform_rmutex_new ();
395 link = uninitialized_cmutex_list;
403 *mp = _dbus_platform_cmutex_new ();
410 link = uninitialized_condvar_list;
418 *cp = _dbus_platform_condvar_new ();
436 link = uninitialized_condvar_list;
443 if (*cp != _DBUS_DUMMY_CONDVAR && *cp !=
NULL)
444 _dbus_platform_condvar_free (*cp);
446 *cp = _DBUS_DUMMY_CONDVAR;
452 link = uninitialized_rmutex_list;
459 if (*mp != _DBUS_DUMMY_RMUTEX && *mp !=
NULL)
460 _dbus_platform_rmutex_free (*mp);
462 *mp = _DBUS_DUMMY_RMUTEX;
467 link = uninitialized_cmutex_list;
474 if (*mp != _DBUS_DUMMY_CMUTEX && *mp !=
NULL)
475 _dbus_platform_cmutex_free (*mp);
477 *mp = _DBUS_DUMMY_CMUTEX;
491 #define LOCK_ADDR(name) (& _dbus_lock_##name)
493 LOCK_ADDR (sid_atom_cache),
495 LOCK_ADDR (connection_slots),
496 LOCK_ADDR (pending_call_slots),
497 LOCK_ADDR (server_slots),
498 LOCK_ADDR (message_slots),
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)
513 _DBUS_N_GLOBAL_LOCKS);
518 if (dynamic_global_locks ==
NULL)
523 *global_locks[i] = _dbus_platform_rmutex_new ();
525 if (*global_locks[i] ==
NULL)
528 dynamic_global_locks[i] = global_locks[i];
534 dynamic_global_locks))
537 if (!init_uninitialized_locks ())
545 for (i = i - 1; i >= 0; i--)
547 _dbus_platform_rmutex_free (*global_locks[i]);
548 *global_locks[i] =
NULL;
625 #ifdef DBUS_BUILD_TESTS
628 _dbus_threads_init_debug (
void)
Functions that must be implemented to make the D-Bus library thread-aware.
#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().
void _dbus_condvar_wait(DBusCondVar *cond, DBusCMutex *mutex)
Atomically unlocks the mutex and waits for the conditions variable to be signalled.
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.
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void * data
Data stored at this element.
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.
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
void _dbus_rmutex_unlock(DBusRMutex *mutex)
Unlocks a mutex.
void _dbus_condvar_free(DBusCondVar *cond)
Frees a conditional variable created with dbus_condvar_new(); does nothing if passed a NULL pointer...
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
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's been called.
void _dbus_cmutex_unlock(DBusCMutex *mutex)
Unlocks a mutex.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
#define TRUE
Expands to "1".
void _dbus_condvar_new_at_location(DBusCondVar **location_p)
This does the same thing as _dbus_condvar_new.
void _dbus_condvar_wake_one(DBusCondVar *cond)
If there are threads waiting on the condition variable, wake up exactly one.
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.
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...
void _dbus_cmutex_lock(DBusCMutex *mutex)
Locks a mutex.
void _dbus_rmutex_lock(DBusRMutex *mutex)
Locks a mutex.
#define FALSE
Expands to "0".
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...
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...
dbus_bool_t dbus_threads_init(const DBusThreadFunctions *functions)
Initializes threads, like dbus_threads_init_default().
dbus_bool_t dbus_threads_init_default(void)
Initializes threads.
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...
void _dbus_cmutex_new_at_location(DBusCMutex **location_p)
Creates a new mutex or creates a no-op mutex if threads are not initialized.
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.