24#include <isode/base/messages.h>
42 AsyncEventRef *_queued;
47 friend class AsyncEventRef;
51 inline void SetReceiver (Receiver *rcvr) { _rcvr = rcvr; }
55 AsyncEvent () : _queued (0), _rcvr (0) { }
58 virtual ~AsyncEvent () {
if ( _queued ) Cancel(); }
61 inline AsyncEventRef *Reference ()
const {
return _queued; }
64 Receiver *GetReceiver ()
const {
return _rcvr; }
67 virtual void Deliver() = 0;
70 virtual bool ShouldDelete ()
const {
return false; }
73 inline void Cancel ();
83 virtual ~Receiver() {}
86 virtual void Queue (AsyncEvent *event) = 0;
89 virtual void Extract (AsyncEvent *event) = 0;
92 virtual void Process() = 0;
105 friend class Manager;
108 virtual void extract() = 0;
112 AsyncEventRef(AsyncEvent *event) : _event(event) {
113 event->_queued =
this;
116 AsyncEventRef() : _event(0) {}
118 virtual ~AsyncEventRef() {}
120 void Set (AsyncEvent *event) {
122 event->_queued =
this;
126 inline AsyncEvent *Extract () {
127 AsyncEvent *ep = _event;
144 static EVENTSVC_DLL Manager *_instance;
148 virtual ~Manager() {}
151 static inline Manager *GetManager() {
156 virtual bool IsThreaded () = 0;
159 virtual void Dequeue (AsyncEvent *event) = 0;
169 virtual void Run (
bool isthread) = 0;
175 const timespec *when,
177 pthread_mutex_t *mutex,
178 pthread_cond_t *cond) = 0;
181 virtual void Terminate(
182 unsigned int millisecs
186 virtual void Blocking() = 0;
189 virtual void Unblocked() = 0;
192 static inline void about2Block() {
193 if ( _instance ) _instance->Blocking();
197 static inline void nowUnblocked() {
198 if ( _instance ) _instance->Unblocked();
203 virtual bool LongLived() = 0;
207 static inline bool Yielding() {
208 if ( _instance )
return _instance->LongLived();
214 virtual void SetThreads (
220 virtual int GetActiveLimit () = 0;
222 static const int MAXPRIO = 100;
229 unsigned priority = 0,
234 virtual void QueueAt (
236 const struct timespec *abstime
240 virtual void QueueAt (
246 virtual void QueueAtExit (AsyncEvent *event) = 0;
249 virtual const struct timespec *NextEventDue () = 0;
254 static int ManagerFactory (
260 inline void AsyncEvent::Cancel () {
264 Manager::GetManager()->Dequeue (
this);
272 template<
class U,
class D>
class AsyncEventAux :
public AsyncEvent {
279 AsyncEventAux () : _user(0) {}
282 void SetUser (U *u) { _user = u; }
285 void SetUserData (U *u, D &d) { _user = u; _data = d; }
288 virtual void Deliver () {
289 _user->Deliver (&_data);
293 virtual void Deliver (D *dp) {
298 void Send (Manager *mgr, D *dp) {
301 mgr->Queue (
this, 0);
310IC_CDECL EVENTSVC_DLL
int EventServiceInit (
311 const char *managertype,
312 const char *pollertype,
318#define EVENTSVC_THREADMANAGER "thread"
319#define EVENTSVC_POLLMANAGER "poll"
322IC_CDECL EVENTSVC_DLL
void EventServiceRun (
void);
325IC_CDECL EVENTSVC_DLL
void EventServiceLongLived (
void);
328IC_CDECL EVENTSVC_DLL
void EventServiceTerminate (