D-Bus 1.12.4

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
52 void
53_dbus_rmutex_new_at_location (DBusRMutex **location_p)
54{
55 _dbus_assert (location_p != NULL);
56
57 if (!dbus_threads_init_default ())
58 {
59 *location_p = NULL;
60 return;
61 }
62
63 *location_p = _dbus_platform_rmutex_new ();
64}
65
76 void
77_dbus_cmutex_new_at_location (DBusCMutex **location_p)
78{
79 _dbus_assert (location_p != NULL);
80
81 if (!dbus_threads_init_default ())
82 {
83 *location_p = NULL;
84 return;
85 }
86
87 *location_p = _dbus_platform_cmutex_new ();
88}
89
93 void
94_dbus_rmutex_free_at_location (DBusRMutex **location_p)
95{
96 if (location_p == NULL)
97 return;
98
99 if (*location_p != NULL)
100 _dbus_platform_rmutex_free (*location_p);
101}
102
106 void
107_dbus_cmutex_free_at_location (DBusCMutex **location_p)
108{
109 if (location_p == NULL)
110 return;
111
112 if (*location_p != NULL)
113 _dbus_platform_cmutex_free (*location_p);
114}
115
121 void
122_dbus_rmutex_lock (DBusRMutex *mutex)
123{
124 if (mutex == NULL)
125 return;
126
127 _dbus_platform_rmutex_lock (mutex);
128}
129
135 void
136_dbus_cmutex_lock (DBusCMutex *mutex)
137{
138 if (mutex == NULL)
139 return;
140
141 _dbus_platform_cmutex_lock (mutex);
142}
143
149 void
150_dbus_rmutex_unlock (DBusRMutex *mutex)
151{
152 if (mutex == NULL)
153 return;
154
155 _dbus_platform_rmutex_unlock (mutex);
156}
157
163 void
164_dbus_cmutex_unlock (DBusCMutex *mutex)
165{
166 if (mutex == NULL)
167 return;
168
169 _dbus_platform_cmutex_unlock (mutex);
170}
171
180 DBusCondVar *
181_dbus_condvar_new (void)
182{
183 if (!dbus_threads_init_default ())
184 return NULL;
185
186 return _dbus_platform_condvar_new ();
187}
188
189
198 void
199_dbus_condvar_new_at_location (DBusCondVar **location_p)
200{
201 _dbus_assert (location_p != NULL);
202
203 *location_p = _dbus_condvar_new();
204}
205
206
211 void
212_dbus_condvar_free (DBusCondVar *cond)
213{
214 if (cond == NULL)
215 return;
216
217 _dbus_platform_condvar_free (cond);
218}
219
223 void
224_dbus_condvar_free_at_location (DBusCondVar **location_p)
225{
226 if (location_p == NULL)
227 return;
228
229 if (*location_p != NULL)
230 _dbus_platform_condvar_free (*location_p);
231}
232
239 void
240_dbus_condvar_wait (DBusCondVar *cond,
241 DBusCMutex *mutex)
242{
243 if (cond == NULL || mutex == NULL)
244 return;
245
246 _dbus_platform_condvar_wait (cond, mutex);
247}
248
260 dbus_bool_t
261_dbus_condvar_wait_timeout (DBusCondVar *cond,
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
277 void
278_dbus_condvar_wake_one (DBusCondVar *cond)
279{
280 if (cond == NULL)
281 return;
282
283 _dbus_platform_condvar_wake_one (cond);
284}
285
286 static DBusRMutex *global_locks[_DBUS_N_GLOBAL_LOCKS] = { NULL };
287
288 static 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
301 static 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
336 dbus_bool_t
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 &&
343 !dbus_threads_init_default ())
344 return FALSE;
345
346 _dbus_platform_rmutex_lock (global_locks[lock]);
347 return TRUE;
348}
349
350 void
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
390 dbus_bool_t
391 dbus_threads_init (const DBusThreadFunctions *functions)
392{
393 _dbus_threads_lock_platform_specific ();
394
395 if (thread_init_generation == _dbus_current_generation)
396 {
397 _dbus_threads_unlock_platform_specific ();
398 return TRUE;
399 }
400
401 if (!_dbus_threads_init_platform_specific() ||
402 !init_global_locks ())
403 {
404 _dbus_threads_unlock_platform_specific ();
405 return FALSE;
406 }
407
408 thread_init_generation = _dbus_current_generation;
409
410 _dbus_threads_unlock_platform_specific ();
411 return TRUE;
412}
413
414
415
416/* Default thread implemenation */
417
437 dbus_bool_t
438 dbus_threads_init_default (void)
439{
440 return dbus_threads_init (NULL);
441}
442
443
446#ifdef DBUS_ENABLE_EMBEDDED_TESTS
447
448 dbus_bool_t
449_dbus_threads_init_debug (void)
450{
451 return dbus_threads_init (NULL);
452}
453
454#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:152
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
TRUE
#define TRUE
Expands to "1".
FALSE
#define FALSE
Expands to "0".
_dbus_current_generation
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called,...
Definition: dbus-memory.c:782
_dbus_threads_lock_platform_specific
void _dbus_threads_lock_platform_specific(void)
Lock a static mutex used to protect _dbus_threads_init_platform_specific().
Definition: dbus-sysdeps-pthread.c:294
_dbus_threads_unlock_platform_specific
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific().
Definition: dbus-sysdeps-pthread.c:300
_dbus_threads_init_platform_specific
dbus_bool_t _dbus_threads_init_platform_specific(void)
Initialize threads as in dbus_threads_init_default(), appropriately for the platform.
Definition: dbus-sysdeps-pthread.c:279
_dbus_rmutex_new_at_location
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
_dbus_cmutex_free_at_location
void _dbus_cmutex_free_at_location(DBusCMutex **location_p)
Frees a DBusCMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:107
_dbus_condvar_new
DBusCondVar * _dbus_condvar_new(void)
Creates a new condition variable using the function supplied to dbus_threads_init(),...
Definition: dbus-threads.c:181
_dbus_condvar_free_at_location
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
_dbus_rmutex_unlock
void _dbus_rmutex_unlock(DBusRMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:150
_dbus_condvar_wait
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
_dbus_condvar_new_at_location
void _dbus_condvar_new_at_location(DBusCondVar **location_p)
This does the same thing as _dbus_condvar_new.
Definition: dbus-threads.c:199
_dbus_cmutex_new_at_location
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
_dbus_condvar_wake_one
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_condvar_wait_timeout
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
_dbus_cmutex_lock
void _dbus_cmutex_lock(DBusCMutex *mutex)
Locks a mutex.
Definition: dbus-threads.c:136
_dbus_cmutex_unlock
void _dbus_cmutex_unlock(DBusCMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:164
_dbus_rmutex_free_at_location
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:94
_dbus_rmutex_lock
void _dbus_rmutex_lock(DBusRMutex *mutex)
Locks a mutex.
Definition: dbus-threads.c:122
_dbus_condvar_free
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_threads_init_default
dbus_bool_t dbus_threads_init_default(void)
Initializes threads.
Definition: dbus-threads.c:438
dbus_threads_init
dbus_bool_t dbus_threads_init(const DBusThreadFunctions *functions)
Initializes threads, like dbus_threads_init_default().
Definition: dbus-threads.c:391
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
DBusCMutex
Definition: dbus-sysdeps-pthread.c:53
DBusCondVar
Definition: dbus-sysdeps-pthread.c:57
DBusRMutex
Definition: dbus-sysdeps-pthread.c:49
DBusThreadFunctions
Functions that must be implemented to make the D-Bus library thread-aware.
Definition: dbus-threads.h:153