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