D-Bus 1.12.20
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
29static int thread_init_generation = 0;
30
52void
54{
55 _dbus_assert (location_p != NULL);
56
58 {
59 *location_p = NULL;
60 return;
61 }
62
63 *location_p = _dbus_platform_rmutex_new ();
64}
65
76void
78{
79 _dbus_assert (location_p != NULL);
80
82 {
83 *location_p = NULL;
84 return;
85 }
86
87 *location_p = _dbus_platform_cmutex_new ();
88}
89
93void
95{
96 if (location_p == NULL)
97 return;
98
99 if (*location_p != NULL)
100 _dbus_platform_rmutex_free (*location_p);
101}
102
106void
108{
109 if (location_p == NULL)
110 return;
111
112 if (*location_p != NULL)
113 _dbus_platform_cmutex_free (*location_p);
114}
115
121void
123{
124 if (mutex == NULL)
125 return;
126
127 _dbus_platform_rmutex_lock (mutex);
128}
129
135void
137{
138 if (mutex == NULL)
139 return;
140
141 _dbus_platform_cmutex_lock (mutex);
142}
143
149void
151{
152 if (mutex == NULL)
153 return;
154
155 _dbus_platform_rmutex_unlock (mutex);
156}
157
163void
165{
166 if (mutex == NULL)
167 return;
168
169 _dbus_platform_cmutex_unlock (mutex);
170}
171
182{
184 return NULL;
185
186 return _dbus_platform_condvar_new ();
187}
188
189
198void
200{
201 _dbus_assert (location_p != NULL);
202
203 *location_p = _dbus_condvar_new();
204}
205
206
211void
213{
214 if (cond == NULL)
215 return;
216
217 _dbus_platform_condvar_free (cond);
218}
219
223void
225{
226 if (location_p == NULL)
227 return;
228
229 if (*location_p != NULL)
230 _dbus_platform_condvar_free (*location_p);
231}
232
239void
241 DBusCMutex *mutex)
242{
243 if (cond == NULL || mutex == NULL)
244 return;
245
246 _dbus_platform_condvar_wait (cond, mutex);
247}
248
262 DBusCMutex *mutex,
263 int timeout_milliseconds)
264{
265 if (cond == NULL || mutex == NULL)
266 return TRUE;
267
268 return _dbus_platform_condvar_wait_timeout (cond, mutex,
269 timeout_milliseconds);
270}
271
277void
279{
280 if (cond == NULL)
281 return;
282
283 _dbus_platform_condvar_wake_one (cond);
284}
285
286static DBusRMutex *global_locks[_DBUS_N_GLOBAL_LOCKS] = { NULL };
287
288static void
289shutdown_global_locks (void *nil)
290{
291 int i;
292
293 for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
294 {
295 _dbus_assert (global_locks[i] != NULL);
296 _dbus_platform_rmutex_free (global_locks[i]);
297 global_locks[i] = NULL;
298 }
299}
300
301static dbus_bool_t
302init_global_locks (void)
303{
304 int i;
305 dbus_bool_t ok;
306
307 for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
308 {
309 _dbus_assert (global_locks[i] == NULL);
310
311 global_locks[i] = _dbus_platform_rmutex_new ();
312
313 if (global_locks[i] == NULL)
314 goto failed;
315 }
316
317 _dbus_platform_rmutex_lock (global_locks[_DBUS_LOCK_shutdown_funcs]);
318 ok = _dbus_register_shutdown_func_unlocked (shutdown_global_locks, NULL);
319 _dbus_platform_rmutex_unlock (global_locks[_DBUS_LOCK_shutdown_funcs]);
320
321 if (!ok)
322 goto failed;
323
324 return TRUE;
325
326 failed:
327 for (i = i - 1; i >= 0; i--)
328 {
329 _dbus_platform_rmutex_free (global_locks[i]);
330 global_locks[i] = NULL;
331 }
332
333 return FALSE;
334}
335
337_dbus_lock (DBusGlobalLock lock)
338{
339 _dbus_assert (lock >= 0);
340 _dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
341
342 if (thread_init_generation != _dbus_current_generation &&
344 return FALSE;
345
346 _dbus_platform_rmutex_lock (global_locks[lock]);
347 return TRUE;
348}
349
350void
351_dbus_unlock (DBusGlobalLock lock)
352{
353 _dbus_assert (lock >= 0);
354 _dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
355
356 _dbus_platform_rmutex_unlock (global_locks[lock]);
357}
358 /* end of internals */
360
392{
394
395 if (thread_init_generation == _dbus_current_generation)
396 {
398 return TRUE;
399 }
400
402 !init_global_locks ())
403 {
405 return FALSE;
406 }
407
408 thread_init_generation = _dbus_current_generation;
409
411 return TRUE;
412}
413
414
415
416/* Default thread implemenation */
417
439{
440 return dbus_threads_init (NULL);
441}
442
443
446#ifdef DBUS_ENABLE_EMBEDDED_TESTS
447
449_dbus_threads_init_debug (void)
450{
451 return dbus_threads_init (NULL);
452}
453
454#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called,...
Definition: dbus-memory.c:782
void _dbus_threads_lock_platform_specific(void)
Lock a static mutex used to protect _dbus_threads_init_platform_specific().
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific().
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:53
void _dbus_cmutex_free_at_location(DBusCMutex **location_p)
Frees a DBusCMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:107
DBusCondVar * _dbus_condvar_new(void)
Creates a new condition variable using the function supplied to dbus_threads_init(),...
Definition: dbus-threads.c:181
void _dbus_condvar_free_at_location(DBusCondVar **location_p)
Frees a condition variable; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:224
void _dbus_rmutex_unlock(DBusRMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:150
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:240
void _dbus_condvar_new_at_location(DBusCondVar **location_p)
This does the same thing as _dbus_condvar_new.
Definition: dbus-threads.c:199
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:77
void _dbus_condvar_wake_one(DBusCondVar *cond)
If there are threads waiting on the condition variable, wake up exactly one.
Definition: dbus-threads.c:278
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,...
Definition: dbus-threads.c:261
void _dbus_cmutex_lock(DBusCMutex *mutex)
Locks a mutex.
Definition: dbus-threads.c:136
void _dbus_cmutex_unlock(DBusCMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:164
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:94
void _dbus_rmutex_lock(DBusRMutex *mutex)
Locks a mutex.
Definition: dbus-threads.c:122
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:212
dbus_bool_t dbus_threads_init_default(void)
Initializes threads.
Definition: dbus-threads.c:438
dbus_bool_t dbus_threads_init(const DBusThreadFunctions *functions)
Initializes threads, like dbus_threads_init_default().
Definition: dbus-threads.c:391
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
Functions that must be implemented to make the D-Bus library thread-aware.
Definition: dbus-threads.h:153