dispatcher.h
Go to the documentation of this file.
1/*
2 *
3 * D-Bus++ - C++ bindings for D-Bus
4 *
5 * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com>
6 *
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library 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 GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24
25#ifndef __DBUSXX_DISPATCHER_H
26#define __DBUSXX_DISPATCHER_H
27
28#include "api.h"
29#include "connection.h"
30#include "eventloop.h"
31
32namespace DBus
33{
34
36{
37public:
38
39 class Internal;
40
41 Timeout(Internal *i);
42
43 virtual ~Timeout() {}
44
57 int interval() const;
58
59 bool enabled() const;
60
73 bool handle();
74
75 virtual void toggle() = 0;
76
77private:
78
80
81private:
82
83 Internal *_int;
84};
85
87{
88public:
89
90 class Internal;
91
92 Watch(Internal *i);
93
94 virtual ~Watch() {}
95
104 int descriptor() const;
105
116 int flags() const;
117
118 bool enabled() const;
119
138 bool handle(int flags);
139
140 virtual void toggle() = 0;
141
142private:
143
145
146private:
147
148 Internal *_int;
149};
150
151class DXXAPI Dispatcher
152{
153public:
154
155 virtual ~Dispatcher()
156 {}
157
158 void queue_connection(Connection::Private *);
159
160 void dispatch_pending();
161 bool has_something_to_dispatch();
162
163 virtual void enter() = 0;
164
165 virtual void leave() = 0;
166
167 virtual Timeout *add_timeout(Timeout::Internal *) = 0;
168
169 virtual void rem_timeout(Timeout *) = 0;
170
171 virtual Watch *add_watch(Watch::Internal *) = 0;
172
173 virtual void rem_watch(Watch *) = 0;
174
175 struct Private;
176
177private:
178 void dispatch_pending(Connection::PrivatePList &pending_queue);
179
182
184};
185
187
188/* classes for multithreading support
189*/
190
192{
193public:
194
195 virtual ~Mutex() {}
196
197 virtual void lock() = 0;
198
199 virtual void unlock() = 0;
200
201 struct Internal;
202
203protected:
204
205 Internal *_int;
206};
207
209{
210public:
211
212 virtual ~CondVar() {}
213
214 virtual void wait(Mutex *) = 0;
215
216 virtual bool wait_timeout(Mutex *, int timeout) = 0;
217
218 virtual void wake_one() = 0;
219
220 virtual void wake_all() = 0;
221
222 struct Internal;
223
224protected:
225
226 Internal *_int;
227};
228
229typedef Mutex *(*MutexNewFn)();
230typedef void (*MutexUnlockFn)(Mutex *mx);
231
232#ifndef DBUS_HAS_RECURSIVE_MUTEX
233typedef bool (*MutexFreeFn)(Mutex *mx);
234typedef bool (*MutexLockFn)(Mutex *mx);
235#else
236typedef void (*MutexFreeFn)(Mutex *mx);
237typedef void (*MutexLockFn)(Mutex *mx);
238#endif//DBUS_HAS_RECURSIVE_MUTEX
239
240typedef CondVar *(*CondVarNewFn)();
241typedef void (*CondVarFreeFn)(CondVar *cv);
242typedef void (*CondVarWaitFn)(CondVar *cv, Mutex *mx);
243typedef bool (*CondVarWaitTimeoutFn)(CondVar *cv, Mutex *mx, int timeout);
244typedef void (*CondVarWakeOneFn)(CondVar *cv);
245typedef void (*CondVarWakeAllFn)(CondVar *cv);
246
248
252);
253
254template<class Mx, class Cv>
256{
257 static void init()
258 {
262 );
263 }
264
265 static Mutex *mutex_new()
266 {
267 return new Mx;
268 }
269
270#ifndef DBUS_HAS_RECURSIVE_MUTEX
271 static bool mutex_free(Mutex *mx)
272 {
273 delete mx;
274 return true;
275 }
276
277 static bool mutex_lock(Mutex *mx)
278 {
279 mx->lock();
280 return true;
281 }
282#else
283 static void mutex_free(Mutex *mx)
284 {
285 delete mx;
286 }
287
288 static void mutex_lock(Mutex *mx)
289 {
290 mx->lock();
291 }
292#endif
293
294 static void mutex_unlock(Mutex *mx)
295 {
296 mx->unlock();
297 }
298
300 {
301 return new Cv;
302 }
303
304 static void condvar_free(CondVar *cv)
305 {
306 delete cv;
307 }
308
309 static void condvar_wait(CondVar *cv, Mutex *mx)
310 {
311 cv->wait(mx);
312 }
313
314 static bool condvar_wait_timeout(CondVar *cv, Mutex *mx, int timeout)
315 {
316 return cv->wait_timeout(mx, timeout);
317 }
318
319 static void condvar_wake_one(CondVar *cv)
320 {
321 cv->wake_one();
322 }
323
324 static void condvar_wake_all(CondVar *cv)
325 {
326 cv->wake_all();
327 }
328};
329
330} /* namespace DBus */
331
332#endif//__DBUSXX_DISPATCHER_H
#define DXXAPILOCAL
Definition: api.h:32
#define DXXAPI
Definition: api.h:36
virtual bool wait_timeout(Mutex *, int timeout)=0
virtual void wake_all()=0
virtual void wake_one()=0
Internal * _int
Definition: dispatcher.h:226
virtual ~CondVar()
Definition: dispatcher.h:212
virtual void wait(Mutex *)=0
virtual void lock()=0
Internal * _int
Definition: dispatcher.h:205
virtual void unlock()=0
virtual ~Mutex()
Definition: dispatcher.h:195
virtual void toggle()=0
virtual ~Timeout()
Definition: dispatcher.h:43
Timeout(Internal *i)
Internal * _int
Definition: dispatcher.h:83
DXXAPILOCAL Timeout(const Timeout &)
virtual ~Watch()
Definition: dispatcher.h:94
Watch(Internal *i)
DXXAPILOCAL Watch(const Watch &)
virtual void toggle()=0
Internal * _int
Definition: dispatcher.h:148
void(* CondVarFreeFn)(CondVar *cv)
Definition: dispatcher.h:241
void DXXAPI _init_threading()
Definition: dispatcher.cpp:247
CondVar *(* CondVarNewFn)()
Definition: dispatcher.h:240
bool(* CondVarWaitTimeoutFn)(CondVar *cv, Mutex *mx, int timeout)
Definition: dispatcher.h:243
DXXAPI Dispatcher * default_dispatcher
Definition: dispatcher.cpp:36
void(* MutexUnlockFn)(Mutex *mx)
Definition: dispatcher.h:230
bool(* MutexLockFn)(Mutex *mx)
Definition: dispatcher.h:234
void(* CondVarWakeAllFn)(CondVar *cv)
Definition: dispatcher.h:245
Mutex *(* MutexNewFn)()
Definition: dispatcher.h:229
void(* CondVarWakeOneFn)(CondVar *cv)
Definition: dispatcher.h:244
void(* CondVarWaitFn)(CondVar *cv, Mutex *mx)
Definition: dispatcher.h:242
bool(* MutexFreeFn)(Mutex *mx)
Definition: dispatcher.h:233
Private(DBusConnection *, Server::Private *=NULL)
std::list< Private * > PrivatePList
Definition: connection.h:60
virtual Watch * add_watch(Watch::Internal *)=0
DefaultMutex _mutex_p_copy
Definition: dispatcher.h:181
virtual Timeout * add_timeout(Timeout::Internal *)=0
virtual void rem_timeout(Timeout *)=0
virtual ~Dispatcher()
Definition: dispatcher.h:155
DefaultMutex _mutex_p
Definition: dispatcher.h:180
virtual void rem_watch(Watch *)=0
virtual void leave()=0
virtual void enter()=0
Connection::PrivatePList _pending_queue
Definition: dispatcher.h:183
static void init()
Definition: dispatcher.h:257
static bool condvar_wait_timeout(CondVar *cv, Mutex *mx, int timeout)
Definition: dispatcher.h:314
static void condvar_wake_all(CondVar *cv)
Definition: dispatcher.h:324
static void condvar_free(CondVar *cv)
Definition: dispatcher.h:304
static void condvar_wait(CondVar *cv, Mutex *mx)
Definition: dispatcher.h:309
static Mutex * mutex_new()
Definition: dispatcher.h:265
static bool mutex_free(Mutex *mx)
Definition: dispatcher.h:271
static CondVar * condvar_new()
Definition: dispatcher.h:299
static void mutex_unlock(Mutex *mx)
Definition: dispatcher.h:294
static bool mutex_lock(Mutex *mx)
Definition: dispatcher.h:277
static void condvar_wake_one(CondVar *cv)
Definition: dispatcher.h:319