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 
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 
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 
182 
184 };
185 
187 
188 /* classes for multithreading support
189 */
190 
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 
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 
252 );
253 
254 template<class Mx, class Cv>
255 struct Threading
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
void(* CondVarWaitFn)(CondVar *cv, Mutex *mx)
Definition: dispatcher.h:242
static bool mutex_lock(Mutex *mx)
Definition: dispatcher.h:277
Internal * _int
Definition: dispatcher.h:201
std::list< Private * > PrivatePList
Definition: connection.h:58
virtual ~CondVar()
Definition: dispatcher.h:212
virtual ~Watch()
Definition: dispatcher.h:94
void(* CondVarWakeOneFn)(CondVar *cv)
Definition: dispatcher.h:244
static void init()
Definition: dispatcher.h:257
virtual void wake_one()=0
static void condvar_free(CondVar *cv)
Definition: dispatcher.h:304
Connection::PrivatePList _pending_queue
Definition: dispatcher.h:183
void(* CondVarWakeAllFn)(CondVar *cv)
Definition: dispatcher.h:245
static Mutex * mutex_new()
Definition: dispatcher.h:265
virtual bool wait_timeout(Mutex *, int timeout)=0
DefaultMutex _mutex_p
Definition: dispatcher.h:180
void(* CondVarFreeFn)(CondVar *cv)
Definition: dispatcher.h:241
static void condvar_wake_one(CondVar *cv)
Definition: dispatcher.h:319
bool(* MutexFreeFn)(Mutex *mx)
Definition: dispatcher.h:233
static CondVar * condvar_new()
Definition: dispatcher.h:299
#define DXXAPI
Definition: api.h:36
#define DXXAPILOCAL
Definition: api.h:32
static bool mutex_free(Mutex *mx)
Definition: dispatcher.h:271
Internal * _int
Definition: dispatcher.h:83
virtual ~Mutex()
Definition: dispatcher.h:195
Internal * _int
Definition: dispatcher.h:148
virtual void unlock()=0
Mutex *(* MutexNewFn)()
Definition: dispatcher.h:229
void(* MutexUnlockFn)(Mutex *mx)
Definition: dispatcher.h:230
virtual void lock()=0
DefaultMutex _mutex_p_copy
Definition: dispatcher.h:181
static void condvar_wake_all(CondVar *cv)
Definition: dispatcher.h:324
Internal * _int
Definition: dispatcher.h:222
virtual void wake_all()=0
CondVar *(* CondVarNewFn)()
Definition: dispatcher.h:240
DXXAPI Dispatcher * default_dispatcher
Definition: dispatcher.cpp:36
static void mutex_unlock(Mutex *mx)
Definition: dispatcher.h:294
virtual ~Timeout()
Definition: dispatcher.h:43
virtual ~Dispatcher()
Definition: dispatcher.h:155
Private(DBusConnection *, Server::Private *=NULL)
bool(* MutexLockFn)(Mutex *mx)
Definition: dispatcher.h:234
void DXXAPI _init_threading()
Definition: dispatcher.cpp:247
static bool condvar_wait_timeout(CondVar *cv, Mutex *mx, int timeout)
Definition: dispatcher.h:314
virtual void wait(Mutex *)=0
static void condvar_wait(CondVar *cv, Mutex *mx)
Definition: dispatcher.h:309
bool(* CondVarWaitTimeoutFn)(CondVar *cv, Mutex *mx, int timeout)
Definition: dispatcher.h:243