1 | /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ |
2 | /* dbus-threads.h D-Bus threads handling |
3 | * |
4 | * Copyright (C) 2002 Red Hat Inc. |
5 | * |
6 | * Licensed under the Academic Free License version 2.1 |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | * |
22 | */ |
23 | #if !defined (DBUS_INSIDE_DBUS_H) && !defined (DBUS_COMPILATION) |
24 | #error "Only <dbus/dbus.h> can be included directly, this file may disappear or change contents." |
25 | #endif |
26 | |
27 | #ifndef DBUS_THREADS_H |
28 | #define DBUS_THREADS_H |
29 | |
30 | #include <dbus/dbus-macros.h> |
31 | #include <dbus/dbus-types.h> |
32 | |
33 | DBUS_BEGIN_DECLS |
34 | |
35 | /** |
36 | * @addtogroup DBusThreads |
37 | * @{ |
38 | */ |
39 | |
40 | /** An opaque mutex type provided by the #DBusThreadFunctions implementation installed by dbus_threads_init(). */ |
41 | typedef struct DBusMutex DBusMutex; |
42 | /** An opaque condition variable type provided by the #DBusThreadFunctions implementation installed by dbus_threads_init(). */ |
43 | typedef struct DBusCondVar DBusCondVar; |
44 | |
45 | /** Deprecated, provide DBusRecursiveMutexNewFunction instead. */ |
46 | typedef DBusMutex* (* DBusMutexNewFunction) (void); |
47 | /** Deprecated, provide DBusRecursiveMutexFreeFunction instead. */ |
48 | typedef void (* DBusMutexFreeFunction) (DBusMutex *mutex); |
49 | /** Deprecated, provide DBusRecursiveMutexLockFunction instead. Return value is lock success, but gets ignored in practice. */ |
50 | typedef dbus_bool_t (* DBusMutexLockFunction) (DBusMutex *mutex); |
51 | /** Deprecated, provide DBusRecursiveMutexUnlockFunction instead. Return value is unlock success, but gets ignored in practice. */ |
52 | typedef dbus_bool_t (* DBusMutexUnlockFunction) (DBusMutex *mutex); |
53 | |
54 | /** Creates a new recursively-lockable mutex, or returns #NULL if not |
55 | * enough memory. Can only fail due to lack of memory. Found in |
56 | * #DBusThreadFunctions. Do not just use PTHREAD_MUTEX_RECURSIVE for |
57 | * this, because it does not save/restore the recursion count when |
58 | * waiting on a condition. libdbus requires the Java-style behavior |
59 | * where the mutex is fully unlocked to wait on a condition. |
60 | */ |
61 | typedef DBusMutex* (* DBusRecursiveMutexNewFunction) (void); |
62 | /** Frees a recursively-lockable mutex. Found in #DBusThreadFunctions. |
63 | */ |
64 | typedef void (* DBusRecursiveMutexFreeFunction) (DBusMutex *mutex); |
65 | /** Locks a recursively-lockable mutex. Found in #DBusThreadFunctions. |
66 | * Can only fail due to lack of memory. |
67 | */ |
68 | typedef void (* DBusRecursiveMutexLockFunction) (DBusMutex *mutex); |
69 | /** Unlocks a recursively-lockable mutex. Found in #DBusThreadFunctions. |
70 | * Can only fail due to lack of memory. |
71 | */ |
72 | typedef void (* DBusRecursiveMutexUnlockFunction) (DBusMutex *mutex); |
73 | |
74 | /** Creates a new condition variable. Found in #DBusThreadFunctions. |
75 | * Can only fail (returning #NULL) due to lack of memory. |
76 | */ |
77 | typedef DBusCondVar* (* DBusCondVarNewFunction) (void); |
78 | /** Frees a condition variable. Found in #DBusThreadFunctions. |
79 | */ |
80 | typedef void (* DBusCondVarFreeFunction) (DBusCondVar *cond); |
81 | |
82 | /** Waits on a condition variable. Found in |
83 | * #DBusThreadFunctions. Must work with either a recursive or |
84 | * nonrecursive mutex, whichever the thread implementation |
85 | * provides. Note that PTHREAD_MUTEX_RECURSIVE does not work with |
86 | * condition variables (does not save/restore the recursion count) so |
87 | * don't try using simply pthread_cond_wait() and a |
88 | * PTHREAD_MUTEX_RECURSIVE to implement this, it won't work right. |
89 | * |
90 | * Has no error conditions. Must succeed if it returns. |
91 | */ |
92 | typedef void (* DBusCondVarWaitFunction) (DBusCondVar *cond, |
93 | DBusMutex *mutex); |
94 | |
95 | /** Waits on a condition variable with a timeout. Found in |
96 | * #DBusThreadFunctions. Returns #TRUE if the wait did not |
97 | * time out, and #FALSE if it did. |
98 | * |
99 | * Has no error conditions. Must succeed if it returns. |
100 | */ |
101 | typedef dbus_bool_t (* DBusCondVarWaitTimeoutFunction) (DBusCondVar *cond, |
102 | DBusMutex *mutex, |
103 | int timeout_milliseconds); |
104 | /** Wakes one waiting thread on a condition variable. Found in #DBusThreadFunctions. |
105 | * |
106 | * Has no error conditions. Must succeed if it returns. |
107 | */ |
108 | typedef void (* DBusCondVarWakeOneFunction) (DBusCondVar *cond); |
109 | |
110 | /** Wakes all waiting threads on a condition variable. Found in #DBusThreadFunctions. |
111 | * |
112 | * Has no error conditions. Must succeed if it returns. |
113 | */ |
114 | typedef void (* DBusCondVarWakeAllFunction) (DBusCondVar *cond); |
115 | |
116 | /** |
117 | * Flags indicating which functions are present in #DBusThreadFunctions. Used to allow |
118 | * the library to detect older callers of dbus_threads_init() if new possible functions |
119 | * are added to #DBusThreadFunctions. |
120 | */ |
121 | typedef enum |
122 | { |
123 | DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK = 1 << 0, |
124 | DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK = 1 << 1, |
125 | DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK = 1 << 2, |
126 | DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK = 1 << 3, |
127 | DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK = 1 << 4, |
128 | DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK = 1 << 5, |
129 | DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK = 1 << 6, |
130 | DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK = 1 << 7, |
131 | DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK = 1 << 8, |
132 | DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK = 1 << 9, |
133 | DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK = 1 << 10, |
134 | DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK = 1 << 11, |
135 | DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK = 1 << 12, |
136 | DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK = 1 << 13, |
137 | DBUS_THREAD_FUNCTIONS_ALL_MASK = (1 << 14) - 1 |
138 | } DBusThreadFunctionsMask; |
139 | |
140 | /** |
141 | * Functions that must be implemented to make the D-Bus library |
142 | * thread-aware. |
143 | * |
144 | * If you supply both recursive and non-recursive mutexes, |
145 | * libdbus will use the non-recursive version for condition variables, |
146 | * and the recursive version in other contexts. |
147 | * |
148 | * The condition variable functions have to work with nonrecursive |
149 | * mutexes if you provide those, or with recursive mutexes if you |
150 | * don't. |
151 | */ |
152 | typedef struct |
153 | { |
154 | unsigned int mask; /**< Mask indicating which functions are present. */ |
155 | |
156 | DBusMutexNewFunction mutex_new; /**< Function to create a mutex; optional and deprecated. */ |
157 | DBusMutexFreeFunction mutex_free; /**< Function to free a mutex; optional and deprecated. */ |
158 | DBusMutexLockFunction mutex_lock; /**< Function to lock a mutex; optional and deprecated. */ |
159 | DBusMutexUnlockFunction mutex_unlock; /**< Function to unlock a mutex; optional and deprecated. */ |
160 | |
161 | DBusCondVarNewFunction condvar_new; /**< Function to create a condition variable */ |
162 | DBusCondVarFreeFunction condvar_free; /**< Function to free a condition variable */ |
163 | DBusCondVarWaitFunction condvar_wait; /**< Function to wait on a condition */ |
164 | DBusCondVarWaitTimeoutFunction condvar_wait_timeout; /**< Function to wait on a condition with a timeout */ |
165 | DBusCondVarWakeOneFunction condvar_wake_one; /**< Function to wake one thread waiting on the condition */ |
166 | DBusCondVarWakeAllFunction condvar_wake_all; /**< Function to wake all threads waiting on the condition */ |
167 | |
168 | DBusRecursiveMutexNewFunction recursive_mutex_new; /**< Function to create a recursive mutex */ |
169 | DBusRecursiveMutexFreeFunction recursive_mutex_free; /**< Function to free a recursive mutex */ |
170 | DBusRecursiveMutexLockFunction recursive_mutex_lock; /**< Function to lock a recursive mutex */ |
171 | DBusRecursiveMutexUnlockFunction recursive_mutex_unlock; /**< Function to unlock a recursive mutex */ |
172 | |
173 | void (* padding1) (void); /**< Reserved for future expansion */ |
174 | void (* padding2) (void); /**< Reserved for future expansion */ |
175 | void (* padding3) (void); /**< Reserved for future expansion */ |
176 | void (* padding4) (void); /**< Reserved for future expansion */ |
177 | |
178 | } DBusThreadFunctions; |
179 | |
180 | DBUS_EXPORT |
181 | dbus_bool_t dbus_threads_init (const DBusThreadFunctions *functions); |
182 | DBUS_EXPORT |
183 | dbus_bool_t dbus_threads_init_default (void); |
184 | |
185 | /** @} */ |
186 | |
187 | DBUS_END_DECLS |
188 | |
189 | #endif /* DBUS_THREADS_H */ |
190 | |