#include "dbus-list.h"
static int thread_init_generation = 0;
-
-static DBusList *uninitialized_rmutex_list = NULL;
-static DBusList *uninitialized_cmutex_list = NULL;
-static DBusList *uninitialized_condvar_list = NULL;
-
-/** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
-#define _DBUS_DUMMY_MUTEX ((DBusMutex*)0xABCDEF)
-#define _DBUS_DUMMY_RMUTEX ((DBusRMutex *) _DBUS_DUMMY_MUTEX)
-#define _DBUS_DUMMY_CMUTEX ((DBusCMutex *) _DBUS_DUMMY_MUTEX)
-
-/** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
-#define _DBUS_DUMMY_CONDVAR ((DBusCondVar*)0xABCDEF2)
/**
* @defgroup DBusThreadsInternals Thread functions
* If possible, the mutex returned by this function is recursive, to
* avoid deadlocks. However, that cannot be relied on.
*
- * The extra level of indirection given by allocating a pointer
- * to point to the mutex location allows the threading
- * module to swap out dummy mutexes for a real mutex so libraries
- * can initialize threads even after the D-Bus API has been used.
- *
* @param location_p the location of the new mutex, can return #NULL on OOM
*/
void
{
_dbus_assert (location_p != NULL);
- if (thread_init_generation == _dbus_current_generation)
+ if (!dbus_threads_init_default ())
{
- *location_p = _dbus_platform_rmutex_new ();
+ *location_p = NULL;
+ return;
}
- else
- {
- *location_p = _DBUS_DUMMY_RMUTEX;
- if (!_dbus_list_append (&uninitialized_rmutex_list, location_p))
- *location_p = NULL;
- }
+ *location_p = _dbus_platform_rmutex_new ();
}
/**
*
* The returned mutex is suitable for use with condition variables.
*
- * The extra level of indirection given by allocating a pointer
- * to point to the mutex location allows the threading
- * module to swap out dummy mutexes for a real mutex so libraries
- * can initialize threads even after the D-Bus API has been used.
- *
* @param location_p the location of the new mutex, can return #NULL on OOM
*/
void
{
_dbus_assert (location_p != NULL);
- if (thread_init_generation == _dbus_current_generation)
+ if (!dbus_threads_init_default ())
{
- *location_p = _dbus_platform_cmutex_new ();
+ *location_p = NULL;
+ return;
}
- else
- {
- *location_p = _DBUS_DUMMY_CMUTEX;
- if (!_dbus_list_append (&uninitialized_cmutex_list, location_p))
- *location_p = NULL;
- }
+ *location_p = _dbus_platform_cmutex_new ();
}
/**
- * Frees a DBusRMutex or removes it from the uninitialized mutex list;
- * does nothing if passed a #NULL pointer.
+ * Frees a DBusRMutex; does nothing if passed a #NULL pointer.
*/
void
_dbus_rmutex_free_at_location (DBusRMutex **location_p)
if (location_p == NULL)
return;
- if (thread_init_generation == _dbus_current_generation)
- {
- if (*location_p != NULL)
- _dbus_platform_rmutex_free (*location_p);
- }
- else
- {
- _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_RMUTEX);
-
- _dbus_list_remove (&uninitialized_rmutex_list, location_p);
- }
+ if (*location_p != NULL)
+ _dbus_platform_rmutex_free (*location_p);
}
/**
- * Frees a DBusCMutex and removes it from the
- * uninitialized mutex list;
- * does nothing if passed a #NULL pointer.
+ * Frees a DBusCMutex; does nothing if passed a #NULL pointer.
*/
void
_dbus_cmutex_free_at_location (DBusCMutex **location_p)
if (location_p == NULL)
return;
- if (thread_init_generation == _dbus_current_generation)
- {
- if (*location_p != NULL)
- _dbus_platform_cmutex_free (*location_p);
- }
- else
- {
- _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_CMUTEX);
-
- _dbus_list_remove (&uninitialized_cmutex_list, location_p);
- }
+ if (*location_p != NULL)
+ _dbus_platform_cmutex_free (*location_p);
}
/**
void
_dbus_rmutex_lock (DBusRMutex *mutex)
{
- if (mutex && thread_init_generation == _dbus_current_generation)
- _dbus_platform_rmutex_lock (mutex);
+ if (mutex == NULL)
+ return;
+
+ _dbus_platform_rmutex_lock (mutex);
}
/**
void
_dbus_cmutex_lock (DBusCMutex *mutex)
{
- if (mutex && thread_init_generation == _dbus_current_generation)
- _dbus_platform_cmutex_lock (mutex);
+ if (mutex == NULL)
+ return;
+
+ _dbus_platform_cmutex_lock (mutex);
}
/**
void
_dbus_rmutex_unlock (DBusRMutex *mutex)
{
- if (mutex && thread_init_generation == _dbus_current_generation)
- _dbus_platform_rmutex_unlock (mutex);
+ if (mutex == NULL)
+ return;
+
+ _dbus_platform_rmutex_unlock (mutex);
}
/**
void
_dbus_cmutex_unlock (DBusCMutex *mutex)
{
- if (mutex && thread_init_generation == _dbus_current_generation)
- _dbus_platform_cmutex_unlock (mutex);
+ if (mutex == NULL)
+ return;
+
+ _dbus_platform_cmutex_unlock (mutex);
}
/**
DBusCondVar *
_dbus_condvar_new (void)
{
- if (thread_init_generation == _dbus_current_generation)
- return _dbus_platform_condvar_new ();
- else
- return _DBUS_DUMMY_CONDVAR;
+ if (!dbus_threads_init_default ())
+ return NULL;
+
+ return _dbus_platform_condvar_new ();
}
/**
* This does the same thing as _dbus_condvar_new. It however
* gives another level of indirection by allocating a pointer
- * to point to the condvar location. This allows the threading
- * module to swap out dummy condvars for a real condvar so libraries
- * can initialize threads even after the D-Bus API has been used.
+ * to point to the condvar location; this used to be useful.
*
* @returns the location of a new condvar or #NULL on OOM
*/
{
_dbus_assert (location_p != NULL);
- if (thread_init_generation == _dbus_current_generation)
- {
- *location_p = _dbus_condvar_new();
- }
- else
- {
- *location_p = _DBUS_DUMMY_CONDVAR;
-
- if (!_dbus_list_append (&uninitialized_condvar_list, location_p))
- *location_p = NULL;
- }
+ *location_p = _dbus_condvar_new();
}
void
_dbus_condvar_free (DBusCondVar *cond)
{
- if (cond && thread_init_generation == _dbus_current_generation)
- _dbus_platform_condvar_free (cond);
+ if (cond == NULL)
+ return;
+
+ _dbus_platform_condvar_free (cond);
}
/**
- * Frees a conditional variable and removes it from the
- * uninitialized_condvar_list;
- * does nothing if passed a #NULL pointer.
+ * Frees a condition variable; does nothing if passed a #NULL pointer.
*/
void
_dbus_condvar_free_at_location (DBusCondVar **location_p)
if (location_p == NULL)
return;
- if (thread_init_generation == _dbus_current_generation)
- {
- if (*location_p != NULL)
- _dbus_platform_condvar_free (*location_p);
- }
- else
- {
- _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_CONDVAR);
-
- _dbus_list_remove (&uninitialized_condvar_list, location_p);
- }
+ if (*location_p != NULL)
+ _dbus_platform_condvar_free (*location_p);
}
/**
_dbus_condvar_wait (DBusCondVar *cond,
DBusCMutex *mutex)
{
- if (cond && mutex && thread_init_generation == _dbus_current_generation)
- _dbus_platform_condvar_wait (cond, mutex);
+ if (cond == NULL || mutex == NULL)
+ return;
+
+ _dbus_platform_condvar_wait (cond, mutex);
}
/**
DBusCMutex *mutex,
int timeout_milliseconds)
{
- if (cond && mutex && thread_init_generation == _dbus_current_generation)
- return _dbus_platform_condvar_wait_timeout (cond, mutex,
- timeout_milliseconds);
- else
+ 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 && thread_init_generation == _dbus_current_generation)
- _dbus_platform_condvar_wake_one (cond);
-}
-
-static void
-shutdown_global_locks (void *data)
-{
- DBusRMutex ***locks = data;
- int i;
-
- i = 0;
- while (i < _DBUS_N_GLOBAL_LOCKS)
- {
- if (*(locks[i]) != NULL)
- _dbus_platform_rmutex_free (*(locks[i]));
+ if (cond == NULL)
+ return;
- *(locks[i]) = NULL;
- ++i;
- }
-
- dbus_free (locks);
+ _dbus_platform_condvar_wake_one (cond);
}
-static void
-shutdown_uninitialized_locks (void *data)
-{
- _dbus_list_clear (&uninitialized_rmutex_list);
- _dbus_list_clear (&uninitialized_cmutex_list);
- _dbus_list_clear (&uninitialized_condvar_list);
-}
+#ifdef DBUS_HAVE_STATIC_RECURSIVE_MUTEXES
static dbus_bool_t
-init_uninitialized_locks (void)
+init_global_locks (void)
{
- DBusList *link;
-
- _dbus_assert (thread_init_generation != _dbus_current_generation);
-
- link = uninitialized_rmutex_list;
- while (link != NULL)
- {
- DBusRMutex **mp;
-
- mp = link->data;
- _dbus_assert (*mp == _DBUS_DUMMY_RMUTEX);
-
- *mp = _dbus_platform_rmutex_new ();
- if (*mp == NULL)
- goto fail_mutex;
-
- link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
- }
+ return TRUE;
+}
- link = uninitialized_cmutex_list;
- while (link != NULL)
- {
- DBusCMutex **mp;
+/* implementations in dbus-sysdeps-pthread.c */
- mp = link->data;
- _dbus_assert (*mp == _DBUS_DUMMY_CMUTEX);
+#else /* !defined(DBUS_HAVE_STATIC_RECURSIVE_MUTEXES) */
- *mp = _dbus_platform_cmutex_new ();
- if (*mp == NULL)
- goto fail_mutex;
+static DBusRMutex *global_locks[_DBUS_N_GLOBAL_LOCKS] = { NULL };
- link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
- }
+static void
+shutdown_global_locks (void *nil)
+{
+ int i;
- link = uninitialized_condvar_list;
- while (link != NULL)
+ for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
{
- DBusCondVar **cp;
-
- cp = (DBusCondVar **)link->data;
- _dbus_assert (*cp == _DBUS_DUMMY_CONDVAR);
-
- *cp = _dbus_platform_condvar_new ();
- if (*cp == NULL)
- goto fail_condvar;
-
- link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
+ _dbus_assert (global_locks[i] != NULL);
+ _dbus_platform_rmutex_free (global_locks[i]);
+ global_locks[i] = NULL;
}
+}
- _dbus_list_clear (&uninitialized_rmutex_list);
- _dbus_list_clear (&uninitialized_cmutex_list);
- _dbus_list_clear (&uninitialized_condvar_list);
-
- if (!_dbus_register_shutdown_func (shutdown_uninitialized_locks,
- NULL))
- goto fail_condvar;
-
- return TRUE;
+static dbus_bool_t
+init_global_locks (void)
+{
+ int i;
+ dbus_bool_t ok;
- fail_condvar:
- link = uninitialized_condvar_list;
- while (link != NULL)
+ for (i = 0; i < _DBUS_N_GLOBAL_LOCKS; i++)
{
- DBusCondVar **cp;
-
- cp = link->data;
+ _dbus_assert (global_locks[i] == NULL);
- if (*cp != _DBUS_DUMMY_CONDVAR && *cp != NULL)
- _dbus_platform_condvar_free (*cp);
+ global_locks[i] = _dbus_platform_rmutex_new ();
- *cp = _DBUS_DUMMY_CONDVAR;
-
- link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
+ if (global_locks[i] == NULL)
+ goto failed;
}
- fail_mutex:
- link = uninitialized_rmutex_list;
- while (link != NULL)
- {
- DBusRMutex **mp;
-
- mp = link->data;
-
- if (*mp != _DBUS_DUMMY_RMUTEX && *mp != NULL)
- _dbus_platform_rmutex_free (*mp);
+ _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]);
- *mp = _DBUS_DUMMY_RMUTEX;
+ if (!ok)
+ goto failed;
- link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
- }
+ return TRUE;
- link = uninitialized_cmutex_list;
- while (link != NULL)
+ failed:
+ for (i = i - 1; i >= 0; i--)
{
- DBusCMutex **mp;
-
- mp = link->data;
-
- if (*mp != _DBUS_DUMMY_CMUTEX && *mp != NULL)
- _dbus_platform_cmutex_free (*mp);
-
- *mp = _DBUS_DUMMY_CMUTEX;
-
- link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
+ _dbus_platform_rmutex_free (global_locks[i]);
+ global_locks[i] = NULL;
}
return FALSE;
}
-static dbus_bool_t
-init_locks (void)
+dbus_bool_t
+_dbus_lock (DBusGlobalLock lock)
{
- int i;
- DBusRMutex ***dynamic_global_locks;
- DBusRMutex **global_locks[] = {
-#define LOCK_ADDR(name) (& _dbus_lock_##name)
- LOCK_ADDR (win_fds),
- LOCK_ADDR (sid_atom_cache),
- LOCK_ADDR (list),
- LOCK_ADDR (connection_slots),
- LOCK_ADDR (pending_call_slots),
- LOCK_ADDR (server_slots),
- LOCK_ADDR (message_slots),
- LOCK_ADDR (bus),
- LOCK_ADDR (bus_datas),
- LOCK_ADDR (shutdown_funcs),
- LOCK_ADDR (system_users),
- LOCK_ADDR (message_cache),
- LOCK_ADDR (shared_connections),
- LOCK_ADDR (machine_uuid)
-#undef LOCK_ADDR
- };
-
- _DBUS_STATIC_ASSERT (_DBUS_N_ELEMENTS (global_locks) == _DBUS_N_GLOBAL_LOCKS);
-
- i = 0;
-
- dynamic_global_locks = dbus_new (DBusRMutex**, _DBUS_N_GLOBAL_LOCKS);
- if (dynamic_global_locks == NULL)
- goto failed;
-
- while (i < _DBUS_N_ELEMENTS (global_locks))
- {
- *global_locks[i] = _dbus_platform_rmutex_new ();
-
- if (*global_locks[i] == NULL)
- goto failed;
-
- dynamic_global_locks[i] = global_locks[i];
+ _dbus_assert (lock >= 0);
+ _dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
- ++i;
- }
-
- if (!_dbus_register_shutdown_func (shutdown_global_locks,
- dynamic_global_locks))
- goto failed;
+ if (thread_init_generation != _dbus_current_generation &&
+ !dbus_threads_init_default ())
+ return FALSE;
- if (!init_uninitialized_locks ())
- goto failed;
-
+ _dbus_platform_rmutex_lock (global_locks[lock]);
return TRUE;
+}
- failed:
- dbus_free (dynamic_global_locks);
-
- for (i = i - 1; i >= 0; i--)
- {
- _dbus_platform_rmutex_free (*global_locks[i]);
- *global_locks[i] = NULL;
- }
- return FALSE;
+void
+_dbus_unlock (DBusGlobalLock lock)
+{
+ _dbus_assert (lock >= 0);
+ _dbus_assert (lock < _DBUS_N_GLOBAL_LOCKS);
+
+ _dbus_platform_rmutex_unlock (global_locks[lock]);
}
+#endif /* !defined(DBUS_HAVE_STATIC_RECURSIVE_MUTEXES) */
+
/** @} */ /* end of internals */
/**
}
if (!_dbus_threads_init_platform_specific() ||
- !init_locks ())
+ !init_global_locks ())
{
_dbus_threads_unlock_platform_specific ();
return FALSE;