dbus-threads.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-threads.h D-Bus threads handling
*
* Copyright (C) 2002, 2003, 2006 Red Hat Inc.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-threads.h"
#include "dbus-internals.h"
#include "dbus-threads-internal.h"
#include "dbus-list.h"
/* Protected by _dbus_threads_lock_platform_specific() */
static int thread_init_generation = 0;
void
_dbus_rmutex_new_at_location (DBusRMutex **location_p)
{
_dbus_assert (location_p != NULL);
if (!dbus_threads_init_default ())
{
*location_p = NULL;
return;
}
*location_p = _dbus_platform_rmutex_new ();
}
void
_dbus_cmutex_new_at_location (DBusCMutex **location_p)
{
_dbus_assert (location_p != NULL);
if (!dbus_threads_init_default ())
{
*location_p = NULL;
return;
}
*location_p = _dbus_platform_cmutex_new ();
}
void
_dbus_rmutex_free_at_location (DBusRMutex **location_p)
{
if (location_p == NULL)
return;
if (*location_p != NULL)
_dbus_platform_rmutex_free (*location_p);
}
void
_dbus_cmutex_free_at_location (DBusCMutex **location_p)
{
if (location_p == NULL)
return;
if (*location_p != NULL)
_dbus_platform_cmutex_free (*location_p);
}
void
_dbus_rmutex_lock (DBusRMutex *mutex)
{
if (mutex == NULL)
return;
_dbus_platform_rmutex_lock (mutex);
}
void
_dbus_cmutex_lock (DBusCMutex *mutex)
{
if (mutex == NULL)
return;
_dbus_platform_cmutex_lock (mutex);
}
void
_dbus_rmutex_unlock (DBusRMutex *mutex)
{
if (mutex == NULL)
return;
_dbus_platform_rmutex_unlock (mutex);
}
void
_dbus_cmutex_unlock (DBusCMutex *mutex)
{
if (mutex == NULL)
return;
_dbus_platform_cmutex_unlock (mutex);
}
DBusCondVar *
_dbus_condvar_new (void)
{
if (!dbus_threads_init_default ())
return NULL;
return _dbus_platform_condvar_new ();
}
void
_dbus_condvar_new_at_location (DBusCondVar **location_p)
{
_dbus_assert (location_p != NULL);
*location_p = _dbus_condvar_new();
}
void
_dbus_condvar_free (DBusCondVar *cond)
{
if (cond == NULL)
return;
_dbus_platform_condvar_free (cond);
}
void
_dbus_condvar_free_at_location (DBusCondVar **location_p)
{
if (location_p == NULL)
return;
if (*location_p != NULL)
_dbus_platform_condvar_free (*location_p);
}
void
_dbus_condvar_wait (DBusCondVar *cond,
DBusCMutex *mutex)
{
if (cond == NULL || mutex == NULL)
return;
_dbus_platform_condvar_wait (cond, mutex);
}
dbus_bool_t
_dbus_condvar_wait_timeout (DBusCondVar *cond,
DBusCMutex *mutex,
int timeout_milliseconds)
{
if (cond == NULL || mutex == NULL)
return TRUE;
return _dbus_platform_condvar_wait_timeout (cond, mutex,
timeout_milliseconds);
}
void
_dbus_condvar_wake_one (DBusCondVar *cond)
{
if (cond == NULL)
return;
_dbus_platform_condvar_wake_one (cond);
}
/* Protected by _dbus_threads_lock_platform_specific() */
static DBusRMutex *global_locks[_DBUS_N_GLOBAL_LOCKS] = { NULL };
static void
shutdown_global_locks (void *nil)
{
int i;
for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
{
_dbus_assert (global_locks[i] != NULL);
_dbus_platform_rmutex_free (global_locks[i]);
global_locks[i] = NULL;
}
}
static dbus_bool_t
init_global_locks (void)
{
int i;
dbus_bool_t ok;
for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
{
_dbus_assert (global_locks[i] == NULL);
global_locks[i] = _dbus_platform_rmutex_new ();
if (global_locks[i] == NULL)
goto failed;
}
_dbus_platform_rmutex_lock (global_locks[_DBUS_LOCK_shutdown_funcs]);
ok = _dbus_register_shutdown_func_unlocked (shutdown_global_locks, NULL);
_dbus_platform_rmutex_unlock (global_locks[_DBUS_LOCK_shutdown_funcs]);
if (!ok)
goto failed;
return TRUE;
failed:
for (i = i - 1; i >= 0; i--)
{
_dbus_platform_rmutex_free (global_locks[i]);
global_locks[i] = NULL;
}
return FALSE;
}
dbus_bool_t
_dbus_lock (DBusGlobalLock lock)
{
_dbus_assert (lock >= 0);
_dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
if (thread_init_generation != _dbus_current_generation &&
!dbus_threads_init_default ())
return FALSE;
_dbus_platform_rmutex_lock (global_locks[lock]);
return TRUE;
}
void
_dbus_unlock (DBusGlobalLock lock)
{
_dbus_assert (lock >= 0);
_dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
_dbus_platform_rmutex_unlock (global_locks[lock]);
}
/* end of internals */
dbus_bool_t
dbus_threads_init (const DBusThreadFunctions *functions)
{
_dbus_threads_lock_platform_specific ();
if (thread_init_generation == _dbus_current_generation)
{
_dbus_threads_unlock_platform_specific ();
return TRUE;
}
if (!_dbus_threads_init_platform_specific() ||
!init_global_locks ())
{
_dbus_threads_unlock_platform_specific ();
return FALSE;
}
thread_init_generation = _dbus_current_generation;
_dbus_threads_unlock_platform_specific ();
return TRUE;
}
/* Default thread implemenation */
dbus_bool_t
dbus_threads_init_default (void)
{
return dbus_threads_init (NULL);
}
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
#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:153
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
_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:790
_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:296
_dbus_threads_unlock_platform_specific
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific().
Definition dbus-sysdeps-pthread.c:302
_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:281
_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:56
_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:110
_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:184
_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:227
_dbus_rmutex_unlock
void _dbus_rmutex_unlock(DBusRMutex *mutex)
Unlocks a mutex.
Definition dbus-threads.c:153
_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:243
_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:202
_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:80
_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:281
_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:264
_dbus_cmutex_lock
void _dbus_cmutex_lock(DBusCMutex *mutex)
Locks a mutex.
Definition dbus-threads.c:139
_dbus_cmutex_unlock
void _dbus_cmutex_unlock(DBusCMutex *mutex)
Unlocks a mutex.
Definition dbus-threads.c:167
_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:97
_dbus_rmutex_lock
void _dbus_rmutex_lock(DBusRMutex *mutex)
Locks a mutex.
Definition dbus-threads.c:125
_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:215
dbus_threads_init_default
dbus_bool_t dbus_threads_init_default(void)
Initializes threads.
Definition dbus-threads.c:442
dbus_threads_init
dbus_bool_t dbus_threads_init(const DBusThreadFunctions *functions)
Initializes threads, like dbus_threads_init_default().
Definition dbus-threads.c:395
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusCMutex
Definition dbus-sysdeps-pthread.c:55
DBusCondVar
Definition dbus-sysdeps-pthread.c:59
DBusRMutex
Definition dbus-sysdeps-pthread.c:51
DBusThreadFunctions
Functions that must be implemented to make the D-Bus library thread-aware.
Definition dbus-threads.h:155