sl@0: /* -*- mode: C; c-file-style: "gnu" -*- */ sl@0: /* dbus-threads.h D-Bus threads handling sl@0: * sl@0: * Copyright (C) 2002 Red Hat Inc. sl@0: * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * Licensed under the Academic Free License version 2.1 sl@0: * sl@0: * This program is free software; you can redistribute it and/or modify sl@0: * it under the terms of the GNU General Public License as published by sl@0: * the Free Software Foundation; either version 2 of the License, or sl@0: * (at your option) any later version. sl@0: * sl@0: * This program is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the sl@0: * GNU General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU General Public License sl@0: * along with this program; if not, write to the Free Software sl@0: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA sl@0: * sl@0: */ sl@0: #if !defined (DBUS_INSIDE_DBUS_H) && !defined (DBUS_COMPILATION) sl@0: #error "Only can be included directly, this file may disappear or change contents." sl@0: #endif sl@0: sl@0: #ifndef DBUS_THREADS_H sl@0: #define DBUS_THREADS_H sl@0: sl@0: #include sl@0: #include sl@0: sl@0: DBUS_BEGIN_DECLS sl@0: sl@0: /** sl@0: * @addtogroup DBusThreads sl@0: * @{ sl@0: */ sl@0: sl@0: /** An opaque mutex type provided by the #DBusThreadFunctions implementation installed by dbus_threads_init(). */ sl@0: typedef struct DBusMutex DBusMutex; sl@0: /** An opaque condition variable type provided by the #DBusThreadFunctions implementation installed by dbus_threads_init(). */ sl@0: typedef struct DBusCondVar DBusCondVar; sl@0: sl@0: /** Deprecated, provide DBusRecursiveMutexNewFunction instead. */ sl@0: typedef DBusMutex* (* DBusMutexNewFunction) (void); sl@0: /** Deprecated, provide DBusRecursiveMutexFreeFunction instead. */ sl@0: typedef void (* DBusMutexFreeFunction) (DBusMutex *mutex); sl@0: /** Deprecated, provide DBusRecursiveMutexLockFunction instead. Return value is lock success, but gets ignored in practice. */ sl@0: typedef dbus_bool_t (* DBusMutexLockFunction) (DBusMutex *mutex); sl@0: /** Deprecated, provide DBusRecursiveMutexUnlockFunction instead. Return value is unlock success, but gets ignored in practice. */ sl@0: typedef dbus_bool_t (* DBusMutexUnlockFunction) (DBusMutex *mutex); sl@0: sl@0: /** Creates a new recursively-lockable mutex, or returns #NULL if not sl@0: * enough memory. Can only fail due to lack of memory. Found in sl@0: * #DBusThreadFunctions. Do not just use PTHREAD_MUTEX_RECURSIVE for sl@0: * this, because it does not save/restore the recursion count when sl@0: * waiting on a condition. libdbus requires the Java-style behavior sl@0: * where the mutex is fully unlocked to wait on a condition. sl@0: */ sl@0: typedef DBusMutex* (* DBusRecursiveMutexNewFunction) (void); sl@0: /** Frees a recursively-lockable mutex. Found in #DBusThreadFunctions. sl@0: */ sl@0: typedef void (* DBusRecursiveMutexFreeFunction) (DBusMutex *mutex); sl@0: /** Locks a recursively-lockable mutex. Found in #DBusThreadFunctions. sl@0: * Can only fail due to lack of memory. sl@0: */ sl@0: typedef void (* DBusRecursiveMutexLockFunction) (DBusMutex *mutex); sl@0: /** Unlocks a recursively-lockable mutex. Found in #DBusThreadFunctions. sl@0: * Can only fail due to lack of memory. sl@0: */ sl@0: typedef void (* DBusRecursiveMutexUnlockFunction) (DBusMutex *mutex); sl@0: sl@0: /** Creates a new condition variable. Found in #DBusThreadFunctions. sl@0: * Can only fail (returning #NULL) due to lack of memory. sl@0: */ sl@0: typedef DBusCondVar* (* DBusCondVarNewFunction) (void); sl@0: /** Frees a condition variable. Found in #DBusThreadFunctions. sl@0: */ sl@0: typedef void (* DBusCondVarFreeFunction) (DBusCondVar *cond); sl@0: sl@0: /** Waits on a condition variable. Found in sl@0: * #DBusThreadFunctions. Must work with either a recursive or sl@0: * nonrecursive mutex, whichever the thread implementation sl@0: * provides. Note that PTHREAD_MUTEX_RECURSIVE does not work with sl@0: * condition variables (does not save/restore the recursion count) so sl@0: * don't try using simply pthread_cond_wait() and a sl@0: * PTHREAD_MUTEX_RECURSIVE to implement this, it won't work right. sl@0: * sl@0: * Has no error conditions. Must succeed if it returns. sl@0: */ sl@0: typedef void (* DBusCondVarWaitFunction) (DBusCondVar *cond, sl@0: DBusMutex *mutex); sl@0: sl@0: /** Waits on a condition variable with a timeout. Found in sl@0: * #DBusThreadFunctions. Returns #TRUE if the wait did not sl@0: * time out, and #FALSE if it did. sl@0: * sl@0: * Has no error conditions. Must succeed if it returns. sl@0: */ sl@0: typedef dbus_bool_t (* DBusCondVarWaitTimeoutFunction) (DBusCondVar *cond, sl@0: DBusMutex *mutex, sl@0: int timeout_milliseconds); sl@0: /** Wakes one waiting thread on a condition variable. Found in #DBusThreadFunctions. sl@0: * sl@0: * Has no error conditions. Must succeed if it returns. sl@0: */ sl@0: typedef void (* DBusCondVarWakeOneFunction) (DBusCondVar *cond); sl@0: sl@0: /** Wakes all waiting threads on a condition variable. Found in #DBusThreadFunctions. sl@0: * sl@0: * Has no error conditions. Must succeed if it returns. sl@0: */ sl@0: typedef void (* DBusCondVarWakeAllFunction) (DBusCondVar *cond); sl@0: sl@0: /** sl@0: * Flags indicating which functions are present in #DBusThreadFunctions. Used to allow sl@0: * the library to detect older callers of dbus_threads_init() if new possible functions sl@0: * are added to #DBusThreadFunctions. sl@0: */ sl@0: typedef enum sl@0: { sl@0: DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK = 1 << 0, sl@0: DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK = 1 << 1, sl@0: DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK = 1 << 2, sl@0: DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK = 1 << 3, sl@0: DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK = 1 << 4, sl@0: DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK = 1 << 5, sl@0: DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK = 1 << 6, sl@0: DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK = 1 << 7, sl@0: DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK = 1 << 8, sl@0: DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK = 1 << 9, sl@0: DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK = 1 << 10, sl@0: DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK = 1 << 11, sl@0: DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK = 1 << 12, sl@0: DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK = 1 << 13, sl@0: DBUS_THREAD_FUNCTIONS_ALL_MASK = (1 << 14) - 1 sl@0: } DBusThreadFunctionsMask; sl@0: sl@0: /** sl@0: * Functions that must be implemented to make the D-Bus library sl@0: * thread-aware. The recursive mutex functions should be specified sl@0: * rather than the old, deprecated nonrecursive ones. sl@0: * sl@0: * The condition variable functions have to work with recursive sl@0: * mutexes if you provide those, or with nonrecursive mutexes if you sl@0: * provide those. sl@0: * sl@0: * If implementing threads using pthreads, be aware that sl@0: * PTHREAD_MUTEX_RECURSIVE is broken in combination with condition sl@0: * variables. libdbus relies on the Java-style behavior that when sl@0: * waiting on a condition, the recursion count is saved and restored, sl@0: * and the mutex is completely unlocked, not just decremented one sl@0: * level of recursion. sl@0: * sl@0: * Thus with pthreads you probably have to roll your own emulated sl@0: * recursive mutexes, you can't use PTHREAD_MUTEX_RECURSIVE. This is sl@0: * what dbus_threads_init_default() does on platforms that use sl@0: * pthreads. sl@0: */ sl@0: typedef struct sl@0: { sl@0: unsigned int mask; /**< Mask indicating which functions are present. */ sl@0: sl@0: DBusMutexNewFunction mutex_new; /**< Function to create a mutex; optional and deprecated. */ sl@0: DBusMutexFreeFunction mutex_free; /**< Function to free a mutex; optional and deprecated. */ sl@0: DBusMutexLockFunction mutex_lock; /**< Function to lock a mutex; optional and deprecated. */ sl@0: DBusMutexUnlockFunction mutex_unlock; /**< Function to unlock a mutex; optional and deprecated. */ sl@0: sl@0: DBusCondVarNewFunction condvar_new; /**< Function to create a condition variable */ sl@0: DBusCondVarFreeFunction condvar_free; /**< Function to free a condition variable */ sl@0: DBusCondVarWaitFunction condvar_wait; /**< Function to wait on a condition */ sl@0: DBusCondVarWaitTimeoutFunction condvar_wait_timeout; /**< Function to wait on a condition with a timeout */ sl@0: DBusCondVarWakeOneFunction condvar_wake_one; /**< Function to wake one thread waiting on the condition */ sl@0: DBusCondVarWakeAllFunction condvar_wake_all; /**< Function to wake all threads waiting on the condition */ sl@0: sl@0: DBusRecursiveMutexNewFunction recursive_mutex_new; /**< Function to create a recursive mutex */ sl@0: DBusRecursiveMutexFreeFunction recursive_mutex_free; /**< Function to free a recursive mutex */ sl@0: DBusRecursiveMutexLockFunction recursive_mutex_lock; /**< Function to lock a recursive mutex */ sl@0: DBusRecursiveMutexUnlockFunction recursive_mutex_unlock; /**< Function to unlock a recursive mutex */ sl@0: sl@0: void (* padding1) (void); /**< Reserved for future expansion */ sl@0: void (* padding2) (void); /**< Reserved for future expansion */ sl@0: void (* padding3) (void); /**< Reserved for future expansion */ sl@0: void (* padding4) (void); /**< Reserved for future expansion */ sl@0: sl@0: } DBusThreadFunctions; sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: IMPORT_C sl@0: #endif sl@0: dbus_bool_t dbus_threads_init (const DBusThreadFunctions *functions); sl@0: #ifdef __SYMBIAN32__ sl@0: IMPORT_C sl@0: #endif sl@0: dbus_bool_t dbus_threads_init_default (void); sl@0: sl@0: /** @} */ sl@0: sl@0: DBUS_END_DECLS sl@0: sl@0: #endif /* DBUS_THREADS_H */