static DBusThreadFunctions thread_functions =
{
0,
- NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
-
+ NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
+
NULL, NULL, NULL, NULL
};
DBusMutex*
_dbus_mutex_new (void)
{
- if (thread_functions.mutex_new)
+ if (thread_functions.recursive_mutex_new)
+ return (* thread_functions.recursive_mutex_new) ();
+ else if (thread_functions.mutex_new)
return (* thread_functions.mutex_new) ();
else
return _DBUS_DUMMY_MUTEX;
void
_dbus_mutex_free (DBusMutex *mutex)
{
- if (mutex && thread_functions.mutex_free)
- (* thread_functions.mutex_free) (mutex);
+ if (mutex)
+ {
+ if (mutex && thread_functions.recursive_mutex_free)
+ (* thread_functions.recursive_mutex_free) (mutex);
+ else if (mutex && thread_functions.mutex_free)
+ (* thread_functions.mutex_free) (mutex);
+ }
}
/**
/**
* Locks a mutex. Does nothing if passed a #NULL pointer.
- * Locks are not recursive.
- *
- * @returns #TRUE on success
+ * Locks may be recursive if threading implementation initialized
+ * recursive locks.
*/
-dbus_bool_t
+void
_dbus_mutex_lock (DBusMutex *mutex)
{
- if (mutex && thread_functions.mutex_lock)
- return (* thread_functions.mutex_lock) (mutex);
- else
- return TRUE;
+ if (mutex)
+ {
+ if (thread_functions.recursive_mutex_lock)
+ (* thread_functions.recursive_mutex_lock) (mutex);
+ else if (thread_functions.mutex_lock)
+ (* thread_functions.mutex_lock) (mutex);
+ }
}
/**
*
* @returns #TRUE on success
*/
-dbus_bool_t
+void
_dbus_mutex_unlock (DBusMutex *mutex)
{
- if (mutex && thread_functions.mutex_unlock)
- return (* thread_functions.mutex_unlock) (mutex);
- else
- return TRUE;
+ if (mutex)
+ {
+ if (thread_functions.recursive_mutex_unlock)
+ (* thread_functions.recursive_mutex_unlock) (mutex);
+ else if (thread_functions.mutex_unlock)
+ (* thread_functions.mutex_unlock) (mutex);
+ }
}
/**
dbus_bool_t
dbus_threads_init (const DBusThreadFunctions *functions)
{
+ dbus_bool_t mutex_set;
+ dbus_bool_t recursive_mutex_set;
+
_dbus_assert (functions != NULL);
/* these base functions are required. Future additions to
* DBusThreadFunctions may be optional.
*/
- _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK);
- _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK);
- _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK);
- _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK);
_dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK);
_dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK);
_dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK);
_dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK);
_dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK);
_dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK);
- _dbus_assert (functions->mutex_new != NULL);
- _dbus_assert (functions->mutex_free != NULL);
- _dbus_assert (functions->mutex_lock != NULL);
- _dbus_assert (functions->mutex_unlock != NULL);
_dbus_assert (functions->condvar_new != NULL);
_dbus_assert (functions->condvar_free != NULL);
_dbus_assert (functions->condvar_wait != NULL);
_dbus_assert (functions->condvar_wake_one != NULL);
_dbus_assert (functions->condvar_wake_all != NULL);
+ /* Either the mutex function set or recursive mutex set needs
+ * to be available but not both
+ */
+ mutex_set = (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK) &&
+ (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK) &&
+ (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK) &&
+ (functions->mask & DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK) &&
+ functions->mutex_new &&
+ functions->mutex_free &&
+ functions->mutex_lock &&
+ functions->mutex_unlock;
+
+ recursive_mutex_set =
+ (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK) &&
+ (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK) &&
+ (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK) &&
+ (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK) &&
+ functions->recursive_mutex_new &&
+ functions->recursive_mutex_free &&
+ functions->recursive_mutex_lock &&
+ functions->recursive_mutex_unlock;
+
+ if (!(mutex_set || recursive_mutex_set))
+ _dbus_assert_not_reached ("Either the nonrecusrive or recursive mutex "
+ "functions sets should be passed into "
+ "dbus_threads_init. Neither sets were passed.");
+
+ if (mutex_set && recursive_mutex_set)
+ _dbus_assert_not_reached ("Either the nonrecusrive or recursive mutex "
+ "functions sets should be passed into "
+ "dbus_threads_init. Both sets were passed. "
+ "You most likely just want to set the recursive "
+ "mutex functions to avoid deadlocks in D-Bus.");
+
/* Check that all bits in the mask actually are valid mask bits.
* ensures people won't write code that breaks when we add
* new bits.
thread_functions.condvar_wait_timeout = functions->condvar_wait_timeout;
thread_functions.condvar_wake_one = functions->condvar_wake_one;
thread_functions.condvar_wake_all = functions->condvar_wake_all;
+
+ if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK)
+ thread_functions.recursive_mutex_new = functions->recursive_mutex_new;
+ if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK)
+ thread_functions.recursive_mutex_free = functions->recursive_mutex_free;
+
+ if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK)
+ thread_functions.recursive_mutex_lock = functions->recursive_mutex_lock;
+
+ if (functions->mask & DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK)
+ thread_functions.recursive_mutex_unlock = functions->recursive_mutex_unlock;
+
thread_functions.mask = functions->mask;
if (!init_locks ())
typedef dbus_bool_t (* DBusMutexLockFunction) (DBusMutex *mutex);
typedef dbus_bool_t (* DBusMutexUnlockFunction) (DBusMutex *mutex);
+typedef DBusMutex* (* DBusRecursiveMutexNewFunction) (void);
+typedef void (* DBusRecursiveMutexFreeFunction) (DBusMutex *mutex);
+typedef void (* DBusRecursiveMutexLockFunction) (DBusMutex *mutex);
+typedef void (* DBusRecursiveMutexUnlockFunction) (DBusMutex *mutex);
+
typedef DBusCondVar* (* DBusCondVarNewFunction) (void);
typedef void (* DBusCondVarFreeFunction) (DBusCondVar *cond);
typedef void (* DBusCondVarWaitFunction) (DBusCondVar *cond,
DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK = 1 << 7,
DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK = 1 << 8,
DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK = 1 << 9,
-
- DBUS_THREAD_FUNCTIONS_ALL_MASK = (1 << 10) - 1
+ DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK = 1 << 10,
+ DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK = 1 << 11,
+ DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK = 1 << 12,
+ DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK = 1 << 13,
+ DBUS_THREAD_FUNCTIONS_ALL_MASK = (1 << 13) - 1
} DBusThreadFunctionsMask;
/**
DBusCondVarWaitTimeoutFunction condvar_wait_timeout; /**< Function to wait on a condition with a timeout */
DBusCondVarWakeOneFunction condvar_wake_one; /**< Function to wake one thread waiting on the condition */
DBusCondVarWakeAllFunction condvar_wake_all; /**< Function to wake all threads waiting on the condition */
-
+
+ DBusRecursiveMutexNewFunction recursive_mutex_new; /**< Function to create a recursive mutex */
+ DBusRecursiveMutexFreeFunction recursive_mutex_free; /**< Function to free a recursive mutex */
+ DBusRecursiveMutexLockFunction recursive_mutex_lock; /**< Function to lock a recursive mutex */
+ DBusRecursiveMutexUnlockFunction recursive_mutex_unlock; /**< Function to unlock a recursive mutex */
+
void (* padding1) (void); /**< Reserved for future expansion */
void (* padding2) (void); /**< Reserved for future expansion */
void (* padding3) (void); /**< Reserved for future expansion */
void (* padding4) (void); /**< Reserved for future expansion */
- void (* padding5) (void); /**< Reserved for future expansion */
- void (* padding6) (void); /**< Reserved for future expansion */
- void (* padding7) (void); /**< Reserved for future expansion */
- void (* padding8) (void); /**< Reserved for future expansion */
} DBusThreadFunctions;