Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:32

0001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
0002 /* dbus-threads.h  D-Bus threads handling
0003  *
0004  * Copyright (C) 2002  Red Hat Inc.
0005  *
0006  * SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
0007  *
0008  * Licensed under the Academic Free License version 2.1
0009  *
0010  * This program is free software; you can redistribute it and/or modify
0011  * it under the terms of the GNU General Public License as published by
0012  * the Free Software Foundation; either version 2 of the License, or
0013  * (at your option) any later version.
0014  *
0015  * This program is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  * GNU General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU General Public License
0021  * along with this program; if not, write to the Free Software
0022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0023  *
0024  */
0025 #if !defined (DBUS_INSIDE_DBUS_H) && !defined (DBUS_COMPILATION)
0026 #error "Only <dbus/dbus.h> can be included directly, this file may disappear or change contents."
0027 #endif
0028 
0029 #ifndef DBUS_THREADS_H
0030 #define DBUS_THREADS_H
0031 
0032 #include <dbus/dbus-macros.h>
0033 #include <dbus/dbus-types.h>
0034 
0035 DBUS_BEGIN_DECLS
0036 
0037 /**
0038  * @addtogroup DBusThreads
0039  * @{
0040  */
0041 
0042 /** An opaque mutex type provided by the #DBusThreadFunctions implementation installed by dbus_threads_init(). */
0043 typedef struct DBusMutex DBusMutex;
0044 /** An opaque condition variable type provided by the #DBusThreadFunctions implementation installed by dbus_threads_init(). */
0045 typedef struct DBusCondVar DBusCondVar;
0046 
0047 /** Deprecated, provide DBusRecursiveMutexNewFunction instead. */
0048 typedef DBusMutex*  (* DBusMutexNewFunction)    (void);
0049 /** Deprecated, provide DBusRecursiveMutexFreeFunction instead. */
0050 typedef void        (* DBusMutexFreeFunction)   (DBusMutex *mutex);
0051 /** Deprecated, provide DBusRecursiveMutexLockFunction instead. Return value is lock success, but gets ignored in practice. */
0052 typedef dbus_bool_t (* DBusMutexLockFunction)   (DBusMutex *mutex);
0053 /** Deprecated, provide DBusRecursiveMutexUnlockFunction instead. Return value is unlock success, but gets ignored in practice. */
0054 typedef dbus_bool_t (* DBusMutexUnlockFunction) (DBusMutex *mutex);
0055 
0056 /** Creates a new recursively-lockable mutex, or returns #NULL if not
0057  * enough memory.  Can only fail due to lack of memory.  Found in
0058  * #DBusThreadFunctions. Do not just use PTHREAD_MUTEX_RECURSIVE for
0059  * this, because it does not save/restore the recursion count when
0060  * waiting on a condition. libdbus requires the Java-style behavior
0061  * where the mutex is fully unlocked to wait on a condition.
0062  */
0063 typedef DBusMutex*  (* DBusRecursiveMutexNewFunction)    (void);
0064 /** Frees a recursively-lockable mutex.  Found in #DBusThreadFunctions.
0065  */
0066 typedef void        (* DBusRecursiveMutexFreeFunction)   (DBusMutex *mutex);
0067 /** Locks a recursively-lockable mutex.  Found in #DBusThreadFunctions.
0068  * Can only fail due to lack of memory.
0069  */
0070 typedef void        (* DBusRecursiveMutexLockFunction)   (DBusMutex *mutex);
0071 /** Unlocks a recursively-lockable mutex.  Found in #DBusThreadFunctions.
0072  * Can only fail due to lack of memory.
0073  */
0074 typedef void        (* DBusRecursiveMutexUnlockFunction) (DBusMutex *mutex);
0075 
0076 /** Creates a new condition variable.  Found in #DBusThreadFunctions.
0077  * Can only fail (returning #NULL) due to lack of memory.
0078  */
0079 typedef DBusCondVar*  (* DBusCondVarNewFunction)         (void);
0080 /** Frees a condition variable.  Found in #DBusThreadFunctions.
0081  */
0082 typedef void          (* DBusCondVarFreeFunction)        (DBusCondVar *cond);
0083 
0084 /** Waits on a condition variable.  Found in
0085  * #DBusThreadFunctions. Must work with either a recursive or
0086  * nonrecursive mutex, whichever the thread implementation
0087  * provides. Note that PTHREAD_MUTEX_RECURSIVE does not work with
0088  * condition variables (does not save/restore the recursion count) so
0089  * don't try using simply pthread_cond_wait() and a
0090  * PTHREAD_MUTEX_RECURSIVE to implement this, it won't work right.
0091  *
0092  * Has no error conditions. Must succeed if it returns.
0093  */
0094 typedef void          (* DBusCondVarWaitFunction)        (DBusCondVar *cond,
0095                               DBusMutex   *mutex);
0096 
0097 /** Waits on a condition variable with a timeout.  Found in
0098  *  #DBusThreadFunctions. Returns #TRUE if the wait did not
0099  *  time out, and #FALSE if it did.
0100  *
0101  * Has no error conditions. Must succeed if it returns. 
0102  */
0103 typedef dbus_bool_t   (* DBusCondVarWaitTimeoutFunction) (DBusCondVar *cond,
0104                               DBusMutex   *mutex,
0105                               int          timeout_milliseconds);
0106 /** Wakes one waiting thread on a condition variable.  Found in #DBusThreadFunctions.
0107  *
0108  * Has no error conditions. Must succeed if it returns.
0109  */
0110 typedef void          (* DBusCondVarWakeOneFunction) (DBusCondVar *cond);
0111 
0112 /** Wakes all waiting threads on a condition variable.  Found in #DBusThreadFunctions.
0113  *
0114  * Has no error conditions. Must succeed if it returns.
0115  */
0116 typedef void          (* DBusCondVarWakeAllFunction) (DBusCondVar *cond);
0117 
0118 /**
0119  * Flags indicating which functions are present in #DBusThreadFunctions. Used to allow
0120  * the library to detect older callers of dbus_threads_init() if new possible functions
0121  * are added to #DBusThreadFunctions.
0122  */
0123 typedef enum 
0124 {
0125   DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK      = 1 << 0,
0126   DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK     = 1 << 1,
0127   DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK     = 1 << 2,
0128   DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK   = 1 << 3,
0129   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK    = 1 << 4,
0130   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK   = 1 << 5,
0131   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK   = 1 << 6,
0132   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK   = 1 << 7,
0133   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK = 1 << 8,
0134   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK = 1 << 9,
0135   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK    = 1 << 10,
0136   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK   = 1 << 11,
0137   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK   = 1 << 12,
0138   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK = 1 << 13,
0139   DBUS_THREAD_FUNCTIONS_ALL_MASK     = (1 << 14) - 1
0140 } DBusThreadFunctionsMask;
0141 
0142 /**
0143  * Functions that must be implemented to make the D-Bus library
0144  * thread-aware.
0145  *
0146  * If you supply both recursive and non-recursive mutexes,
0147  * libdbus will use the non-recursive version for condition variables,
0148  * and the recursive version in other contexts.
0149  *
0150  * The condition variable functions have to work with nonrecursive
0151  * mutexes if you provide those, or with recursive mutexes if you
0152  * don't.
0153  */
0154 typedef struct
0155 {
0156   unsigned int mask; /**< Mask indicating which functions are present. */
0157 
0158   DBusMutexNewFunction mutex_new; /**< Function to create a mutex; optional and deprecated. */
0159   DBusMutexFreeFunction mutex_free; /**< Function to free a mutex; optional and deprecated. */
0160   DBusMutexLockFunction mutex_lock; /**< Function to lock a mutex; optional and deprecated. */
0161   DBusMutexUnlockFunction mutex_unlock; /**< Function to unlock a mutex; optional and deprecated. */
0162 
0163   DBusCondVarNewFunction condvar_new; /**< Function to create a condition variable */
0164   DBusCondVarFreeFunction condvar_free; /**< Function to free a condition variable */
0165   DBusCondVarWaitFunction condvar_wait; /**< Function to wait on a condition */
0166   DBusCondVarWaitTimeoutFunction condvar_wait_timeout; /**< Function to wait on a condition with a timeout */
0167   DBusCondVarWakeOneFunction condvar_wake_one; /**< Function to wake one thread waiting on the condition */
0168   DBusCondVarWakeAllFunction condvar_wake_all; /**< Function to wake all threads waiting on the condition */
0169  
0170   DBusRecursiveMutexNewFunction recursive_mutex_new; /**< Function to create a recursive mutex */
0171   DBusRecursiveMutexFreeFunction recursive_mutex_free; /**< Function to free a recursive mutex */
0172   DBusRecursiveMutexLockFunction recursive_mutex_lock; /**< Function to lock a recursive mutex */
0173   DBusRecursiveMutexUnlockFunction recursive_mutex_unlock; /**< Function to unlock a recursive mutex */
0174 
0175   void (* padding1) (void); /**< Reserved for future expansion */
0176   void (* padding2) (void); /**< Reserved for future expansion */
0177   void (* padding3) (void); /**< Reserved for future expansion */
0178   void (* padding4) (void); /**< Reserved for future expansion */
0179   
0180 } DBusThreadFunctions;
0181 
0182 DBUS_EXPORT
0183 dbus_bool_t  dbus_threads_init         (const DBusThreadFunctions *functions);
0184 DBUS_EXPORT
0185 dbus_bool_t  dbus_threads_init_default (void);
0186 
0187 /** @} */
0188 
0189 DBUS_END_DECLS
0190 
0191 #endif /* DBUS_THREADS_H */