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  static void mutex_free(Mutex *mx)
271  {
272  delete mx;
273  }
274 
275  static void mutex_lock(Mutex *mx)
276  {
277  mx->lock();
278  }
279 
280  static void mutex_unlock(Mutex *mx)
281  {
282  mx->unlock();
283  }
284 
286  {
287  return new Cv;
288  }
289 
290  static void condvar_free(CondVar *cv)
291  {
292  delete cv;
293  }
294 
295  static void condvar_wait(CondVar *cv, Mutex *mx)
296  {
297  cv->wait(mx);
298  }
299 
300  static bool condvar_wait_timeout(CondVar *cv, Mutex *mx, int timeout)
301  {
302  return cv->wait_timeout(mx, timeout);
303  }
304 
305  static void condvar_wake_one(CondVar *cv)
306  {
307  cv->wake_one();
308  }
309 
310  static void condvar_wake_all(CondVar *cv)
311  {
312  cv->wake_all();
313  }
314 };
315 
316 } /* namespace DBus */
317 
318 #endif//__DBUSXX_DISPATCHER_H
Mutex *(* MutexNewFn)()
Definition: dispatcher.h:229
bool(* MutexFreeFn)(Mutex *mx)
Definition: dispatcher.h:233
virtual void unlock()=0
static CondVar * condvar_new()
Definition: dispatcher.h:285
static void condvar_free(CondVar *cv)
Definition: dispatcher.h:290
static void condvar_wake_all(CondVar *cv)
Definition: dispatcher.h:310
virtual void wait(Mutex *)=0
static void mutex_lock(Mutex *mx)
Definition: dispatcher.h:275
void(* MutexUnlockFn)(Mutex *mx)
Definition: dispatcher.h:230
CondVar *(* CondVarNewFn)()
Definition: dispatcher.h:240
DXXAPI Dispatcher * default_dispatcher
Definition: dispatcher.cpp:36
#define DXXAPILOCAL
Definition: api.h:32
Connection::PrivatePList _pending_queue
Definition: dispatcher.h:183
bool(* MutexLockFn)(Mutex *mx)
Definition: dispatcher.h:234
virtual bool wait_timeout(Mutex *, int timeout)=0
virtual void lock()=0
static void condvar_wake_one(CondVar *cv)
Definition: dispatcher.h:305
virtual ~Watch()
Definition: dispatcher.h:94
virtual void wake_all()=0
virtual ~CondVar()
Definition: dispatcher.h:212
virtual ~Dispatcher()
Definition: dispatcher.h:155
static void mutex_unlock(Mutex *mx)
Definition: dispatcher.h:280
void(* CondVarWaitFn)(CondVar *cv, Mutex *mx)
Definition: dispatcher.h:242
Private(DBusConnection *, Server::Private *=NULL)
static void mutex_free(Mutex *mx)
Definition: dispatcher.h:270
static void init()
Definition: dispatcher.h:257
Internal * _int
Definition: dispatcher.h:148
void(* CondVarWakeAllFn)(CondVar *cv)
Definition: dispatcher.h:245
Internal * _int
Definition: dispatcher.h:201
virtual void wake_one()=0
bool(* CondVarWaitTimeoutFn)(CondVar *cv, Mutex *mx, int timeout)
Definition: dispatcher.h:243
DefaultMutex _mutex_p
Definition: dispatcher.h:180
static Mutex * mutex_new()
Definition: dispatcher.h:265
static void condvar_wait(CondVar *cv, Mutex *mx)
Definition: dispatcher.h:295
std::list< Private * > PrivatePList
Definition: connection.h:58
virtual ~Timeout()
Definition: dispatcher.h:43
void(* CondVarFreeFn)(CondVar *cv)
Definition: dispatcher.h:241
#define DXXAPI
Definition: api.h:36
virtual ~Mutex()
Definition: dispatcher.h:195
DefaultMutex _mutex_p_copy
Definition: dispatcher.h:181
void(* CondVarWakeOneFn)(CondVar *cv)
Definition: dispatcher.h:244
Internal * _int
Definition: dispatcher.h:222
Internal * _int
Definition: dispatcher.h:83
static bool condvar_wait_timeout(CondVar *cv, Mutex *mx, int timeout)
Definition: dispatcher.h:300
void DXXAPI _init_threading()
Definition: dispatcher.cpp:247