# DBus-C++

## Desktop Communication Bus System

libdbus-c-- Documentation 0.9.0

- Files
- Data Structures
- Globals
- Main Page

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
32 namespace DBus
33{
34
35 class DXXAPI Timeout
36{
37 public:
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
77 private:
78
79 DXXAPILOCAL Timeout(const Timeout &);
80
81 private:
82
83 Internal *_int;
84};
85
86 class DXXAPI Watch
87{
88 public:
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
142 private:
143
144 DXXAPILOCAL Watch(const Watch &);
145
146 private:
147
148 Internal *_int;
149};
150
151 class DXXAPI Dispatcher
152{
153 public:
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
177 private:
178 void dispatch_pending(Connection::PrivatePList &pending_queue);
179
180 DefaultMutex _mutex_p;
181 DefaultMutex _mutex_p_copy;
182
183 Connection::PrivatePList _pending_queue;
184};
185
186 extern DXXAPI Dispatcher *default_dispatcher;
187
188/* classes for multithreading support
189*/
190
191 class DXXAPI Mutex
192{
193 public:
194
195 virtual ~Mutex() {}
196
197 virtual void lock() = 0;
198
199 virtual void unlock() = 0;
200
201 struct Internal;
202
203 protected:
204
205 Internal *_int;
206};
207
208 class DXXAPI CondVar
209{
210 public:
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
224 protected:
225
226 Internal *_int;
227};
228
229 typedef Mutex *(*MutexNewFn)();
230 typedef void (*MutexUnlockFn)(Mutex *mx);
231
232#ifndef DBUS_HAS_RECURSIVE_MUTEX
233 typedef bool (*MutexFreeFn)(Mutex *mx);
234 typedef bool (*MutexLockFn)(Mutex *mx);
235#else
236 typedef void (*MutexFreeFn)(Mutex *mx);
237 typedef void (*MutexLockFn)(Mutex *mx);
238#endif//DBUS_HAS_RECURSIVE_MUTEX
239
240 typedef CondVar *(*CondVarNewFn)();
241 typedef void (*CondVarFreeFn)(CondVar *cv);
242 typedef void (*CondVarWaitFn)(CondVar *cv, Mutex *mx);
243 typedef bool (*CondVarWaitTimeoutFn)(CondVar *cv, Mutex *mx, int timeout);
244 typedef void (*CondVarWakeOneFn)(CondVar *cv);
245 typedef void (*CondVarWakeAllFn)(CondVar *cv);
246
247 void DXXAPI _init_threading();
248
249 void DXXAPI _init_threading(
250 MutexNewFn, MutexFreeFn, MutexLockFn, MutexUnlockFn,
251 CondVarNewFn, CondVarFreeFn, CondVarWaitFn, CondVarWaitTimeoutFn, CondVarWakeOneFn, CondVarWakeAllFn
252);
253
254 template<class Mx, class Cv>
255 struct Threading
256{
257 static void init()
258 {
259 _init_threading(
260 mutex_new, mutex_free, mutex_lock, mutex_unlock,
261 condvar_new, condvar_free, condvar_wait, condvar_wait_timeout, condvar_wake_one, condvar_wake_all
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
299 static CondVar *condvar_new()
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
api.h
DXXAPILOCAL
#define DXXAPILOCAL
Definition api.h:32
DXXAPI
#define DXXAPI
Definition api.h:36
DBus::CondVar
Definition dispatcher.h:209
DBus::CondVar::wait_timeout
virtual bool wait_timeout(Mutex *, int timeout)=0
DBus::CondVar::wake_all
virtual void wake_all()=0
DBus::CondVar::wake_one
virtual void wake_one()=0
DBus::CondVar::_int
Internal * _int
Definition dispatcher.h:226
DBus::CondVar::~CondVar
virtual ~CondVar()
Definition dispatcher.h:212
DBus::CondVar::wait
virtual void wait(Mutex *)=0
DBus::Connection::PrivatePList
std::list< Private * > PrivatePList
Definition connection.h:60
DBus::DefaultMutex
Definition eventloop.h:175
DBus::Dispatcher
Definition dispatcher.h:152
DBus::Dispatcher::add_watch
virtual Watch * add_watch(Watch::Internal *)=0
DBus::Dispatcher::_mutex_p_copy
DefaultMutex _mutex_p_copy
Definition dispatcher.h:181
DBus::Dispatcher::add_timeout
virtual Timeout * add_timeout(Timeout::Internal *)=0
DBus::Dispatcher::rem_timeout
virtual void rem_timeout(Timeout *)=0
DBus::Dispatcher::~Dispatcher
virtual ~Dispatcher()
Definition dispatcher.h:155
DBus::Dispatcher::_mutex_p
DefaultMutex _mutex_p
Definition dispatcher.h:180
DBus::Dispatcher::rem_watch
virtual void rem_watch(Watch *)=0
DBus::Dispatcher::leave
virtual void leave()=0
DBus::Dispatcher::enter
virtual void enter()=0
DBus::Dispatcher::dispatch_pending
void dispatch_pending()
Definition dispatcher.cpp:182
DBus::Dispatcher::_pending_queue
Connection::PrivatePList _pending_queue
Definition dispatcher.h:183
DBus::Mutex
Definition dispatcher.h:192
DBus::Mutex::lock
virtual void lock()=0
DBus::Mutex::_int
Internal * _int
Definition dispatcher.h:205
DBus::Mutex::unlock
virtual void unlock()=0
DBus::Mutex::~Mutex
virtual ~Mutex()
Definition dispatcher.h:195
DBus::Timeout
Definition dispatcher.h:36
DBus::Timeout::toggle
virtual void toggle()=0
DBus::Timeout::~Timeout
virtual ~Timeout()
Definition dispatcher.h:43
DBus::Timeout::Timeout
Timeout(Internal *i)
DBus::Timeout::_int
Internal * _int
Definition dispatcher.h:83
DBus::Timeout::Timeout
DXXAPILOCAL Timeout(const Timeout &)
DBus::Watch
Definition dispatcher.h:87
DBus::Watch::~Watch
virtual ~Watch()
Definition dispatcher.h:94
DBus::Watch::Watch
Watch(Internal *i)
DBus::Watch::Watch
DXXAPILOCAL Watch(const Watch &)
DBus::Watch::toggle
virtual void toggle()=0
DBus::Watch::_int
Internal * _int
Definition dispatcher.h:148
connection.h
eventloop.h
DBus
Definition connection_p.h:42
DBus::CondVarFreeFn
void(* CondVarFreeFn)(CondVar *cv)
Definition dispatcher.h:241
DBus::_init_threading
void DXXAPI _init_threading()
Definition dispatcher.cpp:247
DBus::CondVarNewFn
CondVar *(* CondVarNewFn)()
Definition dispatcher.h:240
DBus::CondVarWaitTimeoutFn
bool(* CondVarWaitTimeoutFn)(CondVar *cv, Mutex *mx, int timeout)
Definition dispatcher.h:243
DBus::default_dispatcher
DXXAPI Dispatcher * default_dispatcher
Definition dispatcher.cpp:36
DBus::MutexUnlockFn
void(* MutexUnlockFn)(Mutex *mx)
Definition dispatcher.h:230
DBus::MutexLockFn
bool(* MutexLockFn)(Mutex *mx)
Definition dispatcher.h:234
DBus::CondVarWakeAllFn
void(* CondVarWakeAllFn)(CondVar *cv)
Definition dispatcher.h:245
DBus::MutexNewFn
Mutex *(* MutexNewFn)()
Definition dispatcher.h:229
DBus::CondVarWakeOneFn
void(* CondVarWakeOneFn)(CondVar *cv)
Definition dispatcher.h:244
DBus::CondVarWaitFn
void(* CondVarWaitFn)(CondVar *cv, Mutex *mx)
Definition dispatcher.h:242
DBus::MutexFreeFn
bool(* MutexFreeFn)(Mutex *mx)
Definition dispatcher.h:233
DBus::Connection::Private
Definition connection_p.h:45
DBus::Dispatcher::Private
Definition dispatcher_p.h:42
DBus::Threading
Definition dispatcher.h:256
DBus::Threading::init
static void init()
Definition dispatcher.h:257
DBus::Threading::condvar_wait_timeout
static bool condvar_wait_timeout(CondVar *cv, Mutex *mx, int timeout)
Definition dispatcher.h:314
DBus::Threading::condvar_wake_all
static void condvar_wake_all(CondVar *cv)
Definition dispatcher.h:324
DBus::Threading::condvar_free
static void condvar_free(CondVar *cv)
Definition dispatcher.h:304
DBus::Threading::condvar_wait
static void condvar_wait(CondVar *cv, Mutex *mx)
Definition dispatcher.h:309
DBus::Threading::mutex_new
static Mutex * mutex_new()
Definition dispatcher.h:265
DBus::Threading::mutex_free
static bool mutex_free(Mutex *mx)
Definition dispatcher.h:271
DBus::Threading::condvar_new
static CondVar * condvar_new()
Definition dispatcher.h:299
DBus::Threading::mutex_unlock
static void mutex_unlock(Mutex *mx)
Definition dispatcher.h:294
DBus::Threading::mutex_lock
static bool mutex_lock(Mutex *mx)
Definition dispatcher.h:277
DBus::Threading::condvar_wake_one
static void condvar_wake_one(CondVar *cv)
Definition dispatcher.h:319