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();
165#pragma GCC diagnostic push
166#pragma GCC diagnostic ignored "-Wsuggest-override"
170 pthread_mutex_t _mutex;
178 inline void dieAux () {
179 int rc = pthread_mutex_lock (&_mutex);
183 bool killit = !_busy && !_dying && _qcount <= 0;
188 if ( rc == 0 ) pthread_mutex_unlock (&_mutex);
190 if ( killit )
delete this;
194 inline Syncmsgobj () : _busy(
false), _die(
false), _dying(
false), _qcount(0) {
195 pthread_mutex_init (&_mutex, 0);
199 Event::AsyncEvent *item;
201 while ( (item = _queue.
First()) != 0 )
202 if ( item->ShouldDelete() )
205 pthread_mutex_destroy (&_mutex);
210 virtual void Queue (Event::AsyncEvent *event) {
211 if ( pthread_mutex_lock (&_mutex) != 0 )
218 pthread_mutex_unlock (&_mutex);
222 virtual void Extract (Event::AsyncEvent *event) {
223 if ( pthread_mutex_lock (&_mutex) != 0 )
226 Event::AsyncEventRef *ref =
event->Reference();
231 pthread_mutex_unlock (&_mutex);
237 if ( pthread_mutex_lock (&_mutex) != 0 )
244 pthread_mutex_unlock (&_mutex);
251 Event::AsyncEvent *item;
253 while ( (item = _queue.
First ()) ) {
256 if ( item->ShouldDelete() )
261 pthread_mutex_unlock (&_mutex);
265 bool shouldDelete = item->ShouldDelete();
272 if ( pthread_mutex_lock (&_mutex) != 0 )
278 bool killit = _die && !_dying && _qcount <= 0;
283 pthread_mutex_unlock (&_mutex);
292 if ( pthread_mutex_lock (&_mutex) != 0 )
295 if ( _die || _busy ) {
301 pthread_mutex_unlock (&_mutex);
307 pthread_mutex_unlock (&_mutex);
309 this->actualDeliver (msg);
311 if ( pthread_mutex_lock (&_mutex) != 0 )
314 Event::AsyncEvent *item;
316 while ( (item = _queue.
First ()) ) {
319 if ( item->ShouldDelete() )
324 pthread_mutex_unlock (&_mutex);
326 bool shouldDelete = item->ShouldDelete();
333 if ( pthread_mutex_lock (&_mutex) != 0 )
339 bool killit = _die && !_dying && _qcount <= 0;
344 pthread_mutex_unlock (&_mutex);
346 if ( killit )
delete this;
348 #pragma GCC diagnostic pop
A simple FIFO queue for events.
void extract() override
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.
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.
bool ShouldDelete() const override
Can delete this object.
SyncEvent(M *msg, C *target)
Constructor takes message object to be wrapped.
void Deliver() override
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.