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 
52 void
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 
76 void
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 
93 void
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
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
123 {
124  if (mutex == NULL)
125  return;
126 
127  _dbus_platform_rmutex_lock (mutex);
128 }
129 
135 void
137 {
138  if (mutex == NULL)
139  return;
140 
141  _dbus_platform_cmutex_lock (mutex);
142 }
143 
149 void
151 {
152  if (mutex == NULL)
153  return;
154 
155  _dbus_platform_rmutex_unlock (mutex);
156 }
157 
163 void
165 {
166  if (mutex == NULL)
167  return;
168 
169  _dbus_platform_cmutex_unlock (mutex);
170 }
171 
180 DBusCondVar *
182 {
184  return NULL;
185 
186  return _dbus_platform_condvar_new ();
187 }
188 
189 
198 void
200 {
201  _dbus_assert (location_p != NULL);
202 
203  *location_p = _dbus_condvar_new();
204 }
205 
206 
211 void
213 {
214  if (cond == NULL)
215  return;
216 
217  _dbus_platform_condvar_free (cond);
218 }
219 
223 void
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
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 
277 void
279 {
280  if (cond == NULL)
281  return;
282 
283  _dbus_platform_condvar_wake_one (cond);
284 }
285 
286 #ifdef DBUS_HAVE_STATIC_RECURSIVE_MUTEXES
287 
288 static dbus_bool_t
289 init_global_locks (void)
290 {
291  return TRUE;
292 }
293 
294 /* implementations in dbus-sysdeps-pthread.c */
295 
296 #else /* !defined(DBUS_HAVE_STATIC_RECURSIVE_MUTEXES) */
297 
298 static DBusRMutex *global_locks[_DBUS_N_GLOBAL_LOCKS] = { NULL };
299 
300 static void
301 shutdown_global_locks (void *nil)
302 {
303  int i;
304 
305  for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
306  {
307  _dbus_assert (global_locks[i] != NULL);
308  _dbus_platform_rmutex_free (global_locks[i]);
309  global_locks[i] = NULL;
310  }
311 }
312 
313 static dbus_bool_t
314 init_global_locks (void)
315 {
316  int i;
317  dbus_bool_t ok;
318 
319  for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
320  {
321  _dbus_assert (global_locks[i] == NULL);
322 
323  global_locks[i] = _dbus_platform_rmutex_new ();
324 
325  if (global_locks[i] == NULL)
326  goto failed;
327  }
328 
329  _dbus_platform_rmutex_lock (global_locks[_DBUS_LOCK_shutdown_funcs]);
330  ok = _dbus_register_shutdown_func_unlocked (shutdown_global_locks, NULL);
331  _dbus_platform_rmutex_unlock (global_locks[_DBUS_LOCK_shutdown_funcs]);
332 
333  if (!ok)
334  goto failed;
335 
336  return TRUE;
337 
338  failed:
339  for (i = i - 1; i >= 0; i--)
340  {
341  _dbus_platform_rmutex_free (global_locks[i]);
342  global_locks[i] = NULL;
343  }
344 
345  return FALSE;
346 }
347 
349 _dbus_lock (DBusGlobalLock lock)
350 {
351  _dbus_assert (lock >= 0);
352  _dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
353 
354  if (thread_init_generation != _dbus_current_generation &&
356  return FALSE;
357 
358  _dbus_platform_rmutex_lock (global_locks[lock]);
359  return TRUE;
360 }
361 
362 void
363 _dbus_unlock (DBusGlobalLock lock)
364 {
365  _dbus_assert (lock >= 0);
366  _dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
367 
368  _dbus_platform_rmutex_unlock (global_locks[lock]);
369 }
370 
371 #endif /* !defined(DBUS_HAVE_STATIC_RECURSIVE_MUTEXES) */
372  /* end of internals */
374 
406 {
408 
409  if (thread_init_generation == _dbus_current_generation)
410  {
412  return TRUE;
413  }
414 
416  !init_global_locks ())
417  {
419  return FALSE;
420  }
421 
422  thread_init_generation = _dbus_current_generation;
423 
425  return TRUE;
426 }
427 
428 
429 
430 /* Default thread implemenation */
431 
453 {
454  return dbus_threads_init (NULL);
455 }
456 
457 
460 #ifdef DBUS_BUILD_TESTS
461 
463 _dbus_threads_init_debug (void)
464 {
465  return dbus_threads_init (NULL);
466 }
467 
468 #endif /* DBUS_BUILD_TESTS */