30template<
class M,
class C>
class SyncEvent :
public Event::AsyncEvent {
42 _msg (*msg), _target(target), _onheap(true) { }
48 inline void Init (C *rcvr) {
55 _target->actualDeliver (&_msg);
62 inline void Send (M &data) {
64 ::Event::Manager::GetManager()->Queue (
this);
68 inline void Send (
const timespec *when, M &data ) {
71 ::Event::Manager::GetManager()->QueueAt (
this, when);
75 inline void Sendms (
unsigned millisecs, M &data) {
78 ::Event::Manager::GetManager()->QueueAt (
this, millisecs);
84 ::Event::Manager::GetManager()->QueueAtExit (
this);
99 SyncEventQueue (Event::AsyncEvent *event) : Event::AsyncEventRef (event) {}
103 _next = _prev =
this;
110 if ( _freelist == &_free ) {
129 newq->_freelist = _freelist;
140 _prev->_next = _next;
141 _next->_prev = _prev;
151 return _next->Extract();
168 pthread_mutex_t _mutex;
176 inline void dieAux () {
177 int rc = pthread_mutex_lock (&_mutex);
181 bool killit = !_busy && !_dying && _qcount <= 0;
186 if ( rc == 0 ) pthread_mutex_unlock (&_mutex);
188 if ( killit )
delete this;
192 inline Syncmsgobj () : _busy(
false), _die(
false), _dying(
false), _qcount(0) {
193 pthread_mutex_init (&_mutex, 0);
197 Event::AsyncEvent *item;
199 while ( (item = _queue.
First()) != 0 )
200 if ( item->ShouldDelete() )
203 pthread_mutex_destroy (&_mutex);
208 virtual void Queue (Event::AsyncEvent *event) {
209 if ( pthread_mutex_lock (&_mutex) != 0 )
216 pthread_mutex_unlock (&_mutex);
220 virtual void Extract (Event::AsyncEvent *event) {
221 if ( pthread_mutex_lock (&_mutex) != 0 )
224 Event::AsyncEventRef *ref =
event->Reference();
229 pthread_mutex_unlock (&_mutex);
235 if ( pthread_mutex_lock (&_mutex) != 0 )
242 pthread_mutex_unlock (&_mutex);
249 Event::AsyncEvent *item;
251 while ( (item = _queue.
First ()) ) {
254 if ( item->ShouldDelete() )
259 pthread_mutex_unlock (&_mutex);
263 bool shouldDelete = item->ShouldDelete();
270 if ( pthread_mutex_lock (&_mutex) != 0 )
276 bool killit = _die && !_dying && _qcount <= 0;
281 pthread_mutex_unlock (&_mutex);
290 if ( pthread_mutex_lock (&_mutex) != 0 )
293 if ( _die || _busy ) {
299 pthread_mutex_unlock (&_mutex);
305 pthread_mutex_unlock (&_mutex);
307 this->actualDeliver (msg);
309 if ( pthread_mutex_lock (&_mutex) != 0 )
312 Event::AsyncEvent *item;
314 while ( (item = _queue.
First ()) ) {
317 if ( item->ShouldDelete() )
322 pthread_mutex_unlock (&_mutex);
324 bool shouldDelete = item->ShouldDelete();
331 if ( pthread_mutex_lock (&_mutex) != 0 )
337 bool killit = _die && !_dying && _qcount <= 0;
342 pthread_mutex_unlock (&_mutex);
344 if ( killit )
delete this;
A simple FIFO queue for events.
virtual void extract()
Subordinate extract mechanism.
Event::AsyncEvent * First()
Remove first element of queue.
void Insert(Event::AsyncEvent *event)
Insert in queue.
Class used to wrap each event object type for a given receiver.
virtual bool ShouldDelete() const
Can delete this object.
void Send(M &data)
Send data now.
virtual ~SyncEvent()
Destructor must be virtual.
void Init(C *rcvr)
Set the target.
void Sendms(unsigned millisecs, M &data)
Send in interval (milliseconds)
SyncEvent()
Constructor within another object.
SyncEvent(M *msg, C *target)
Constructor takes message object to be wrapped.
virtual void Deliver()
Deliver this message to the receiver.
void Send(const timespec *when, M &data)
Send using timespec.
void SendAtExit(M &data)
Send when terminating.
Template class for protecting an object.
void msgDeliver(M *msg)
template function for delivering message of given type to receiver
virtual void Process()
Process queued events for the object.
virtual void Queue(Event::AsyncEvent *event)
Queue event from Event Manager.
virtual void Extract(Event::AsyncEvent *event)
Remove event from queue.
Various utility functions for handling times.