00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __DBUSXX_DISPATCHER_H
00026 #define __DBUSXX_DISPATCHER_H
00027
00028 #include "api.h"
00029 #include "connection.h"
00030 #include "eventloop.h"
00031
00032 namespace DBus {
00033
00034 class DXXAPI Timeout
00035 {
00036 public:
00037
00038 class Internal;
00039
00040 Timeout(Internal *i);
00041
00042 virtual ~Timeout(){}
00043
00056 int interval() const;
00057
00058 bool enabled() const;
00059
00072 bool handle();
00073
00074 virtual void toggle() = 0;
00075
00076 private:
00077
00078 DXXAPILOCAL Timeout(const Timeout &);
00079
00080 private:
00081
00082 Internal *_int;
00083 };
00084
00085 class DXXAPI Watch
00086 {
00087 public:
00088
00089 class Internal;
00090
00091 Watch(Internal *i);
00092
00093 virtual ~Watch(){}
00094
00103 int descriptor() const;
00104
00115 int flags() const;
00116
00117 bool enabled() const;
00118
00137 bool handle(int flags);
00138
00139 virtual void toggle() = 0;
00140
00141 private:
00142
00143 DXXAPILOCAL Watch(const Watch &);
00144
00145 private:
00146
00147 Internal *_int;
00148 };
00149
00150 class DXXAPI Dispatcher
00151 {
00152 public:
00153
00154 virtual ~Dispatcher()
00155 {}
00156
00157 void queue_connection(Connection::Private *);
00158
00159 void dispatch_pending();
00160 bool has_something_to_dispatch();
00161
00162 virtual void enter() = 0;
00163
00164 virtual void leave() = 0;
00165
00166 virtual Timeout *add_timeout(Timeout::Internal *) = 0;
00167
00168 virtual void rem_timeout(Timeout *) = 0;
00169
00170 virtual Watch *add_watch(Watch::Internal *) = 0;
00171
00172 virtual void rem_watch(Watch *) = 0;
00173
00174 struct Private;
00175
00176 private:
00177
00178 DefaultMutex _mutex_p;
00179 Connection::PrivatePList _pending_queue;
00180 };
00181
00182 extern DXXAPI Dispatcher *default_dispatcher;
00183
00184
00185
00186
00187 class DXXAPI Mutex
00188 {
00189 public:
00190
00191 virtual ~Mutex() {}
00192
00193 virtual void lock() = 0;
00194
00195 virtual void unlock() = 0;
00196
00197 struct Internal;
00198
00199 protected:
00200
00201 Internal *_int;
00202 };
00203
00204 class DXXAPI CondVar
00205 {
00206 public:
00207
00208 virtual ~CondVar() {}
00209
00210 virtual void wait(Mutex *) = 0;
00211
00212 virtual bool wait_timeout(Mutex *, int timeout) = 0;
00213
00214 virtual void wake_one() = 0;
00215
00216 virtual void wake_all() = 0;
00217
00218 struct Internal;
00219
00220 protected:
00221
00222 Internal *_int;
00223 };
00224
00225 typedef Mutex *(*MutexNewFn)();
00226 typedef void (*MutexUnlockFn)(Mutex *mx);
00227
00228 #ifndef DBUS_HAS_RECURSIVE_MUTEX
00229 typedef bool (*MutexFreeFn)(Mutex *mx);
00230 typedef bool (*MutexLockFn)(Mutex *mx);
00231 #else
00232 typedef void (*MutexFreeFn)(Mutex *mx);
00233 typedef void (*MutexLockFn)(Mutex *mx);
00234 #endif//DBUS_HAS_RECURSIVE_MUTEX
00235
00236 typedef CondVar *(*CondVarNewFn)();
00237 typedef void (*CondVarFreeFn)(CondVar *cv);
00238 typedef void (*CondVarWaitFn)(CondVar *cv, Mutex *mx);
00239 typedef bool (*CondVarWaitTimeoutFn)(CondVar *cv, Mutex *mx, int timeout);
00240 typedef void (*CondVarWakeOneFn)(CondVar *cv);
00241 typedef void (*CondVarWakeAllFn)(CondVar *cv);
00242
00243 void DXXAPI _init_threading();
00244
00245 void DXXAPI _init_threading(
00246 MutexNewFn, MutexFreeFn, MutexLockFn, MutexUnlockFn,
00247 CondVarNewFn, CondVarFreeFn, CondVarWaitFn, CondVarWaitTimeoutFn, CondVarWakeOneFn, CondVarWakeAllFn
00248 );
00249
00250 template<class Mx, class Cv>
00251 struct Threading
00252 {
00253 static void init()
00254 {
00255 _init_threading(
00256 mutex_new, mutex_free, mutex_lock, mutex_unlock,
00257 condvar_new, condvar_free, condvar_wait, condvar_wait_timeout, condvar_wake_one, condvar_wake_all
00258 );
00259 }
00260
00261 static Mutex *mutex_new()
00262 {
00263 return new Mx;
00264 }
00265
00266 static void mutex_free(Mutex *mx)
00267 {
00268 delete mx;
00269 }
00270
00271 static void mutex_lock(Mutex *mx)
00272 {
00273 mx->lock();
00274 }
00275
00276 static void mutex_unlock(Mutex *mx)
00277 {
00278 mx->unlock();
00279 }
00280
00281 static CondVar *condvar_new()
00282 {
00283 return new Cv;
00284 }
00285
00286 static void condvar_free(CondVar *cv)
00287 {
00288 delete cv;
00289 }
00290
00291 static void condvar_wait(CondVar *cv, Mutex *mx)
00292 {
00293 cv->wait(mx);
00294 }
00295
00296 static bool condvar_wait_timeout(CondVar *cv, Mutex *mx, int timeout)
00297 {
00298 return cv->wait_timeout(mx, timeout);
00299 }
00300
00301 static void condvar_wake_one(CondVar *cv)
00302 {
00303 cv->wake_one();
00304 }
00305
00306 static void condvar_wake_all(CondVar *cv)
00307 {
00308 cv->wake_all();
00309 }
00310 };
00311
00312 }
00313
00314 #endif//__DBUSXX_DISPATCHER_H