Syncmsgobj< C > Class Template Reference

Template class for protecting an object. More...

#include <Syncmsg.h>

Inheritance diagram for Syncmsgobj< C >:

Public Member Functions

virtual void Queue (Event::AsyncEvent *event)
 Queue event from Event Manager.
 
virtual void Extract (Event::AsyncEvent *event)
 Remove event from queue.
 
virtual void Process ()
 Process queued events for the object.
 
template<class M >
void msgDeliver (M *msg)
 template function for delivering message of given type to receiver
 

Protected Member Functions

void dieAux ()
 

Detailed Description

template<class C>
class Syncmsgobj< C >

Template class for protecting an object.

The key idea here is that when the object is busy, then the thread which delivers the event object does not wait. It stores the event on a queue, which is processed when the current operation has finished. A subclass of this class would be defined so that Deliver (M *msg) can be defined. We cannot have virtual template functions within this class.

Definition at line 168 of file Syncmsg.h.

Constructor & Destructor Documentation

◆ Syncmsgobj()

template<class C >
Syncmsgobj< C >::Syncmsgobj ( )
inline

Definition at line 194 of file Syncmsg.h.

194 : _busy(false), _die(false), _dying(false), _qcount(0) {
195 pthread_mutex_init (&_mutex, 0);
196 }

◆ ~Syncmsgobj()

template<class C >
virtual Syncmsgobj< C >::~Syncmsgobj ( )
inlinevirtual

Definition at line 198 of file Syncmsg.h.

198 {
199 Event::AsyncEvent *item;
200
201 while ( (item = _queue.First()) != 0 )
202 if ( item->ShouldDelete() )
203 delete item;
204
205 pthread_mutex_destroy (&_mutex);
206 }
Event::AsyncEvent * First()
Remove first element of queue.
Definition Syncmsg.h:147

Member Function Documentation

◆ dieAux()

template<class C >
void Syncmsgobj< C >::dieAux ( )
inlineprotected

Definition at line 178 of file Syncmsg.h.

178 {
179 int rc = pthread_mutex_lock (&_mutex);
180
181 _die = true;
182
183 bool killit = !_busy && !_dying && _qcount <= 0;
184
185 if ( killit )
186 _dying = true;
187
188 if ( rc == 0 ) pthread_mutex_unlock (&_mutex);
189
190 if ( killit ) delete this;
191 }

◆ Queue()

template<class C >
virtual void Syncmsgobj< C >::Queue ( Event::AsyncEvent *  event)
inlinevirtual

Queue event from Event Manager.

Definition at line 210 of file Syncmsg.h.

210 {
211 if ( pthread_mutex_lock (&_mutex) != 0 )
212 return;
213
214 _queue.Insert (event);
215
216 _qcount++;
217
218 pthread_mutex_unlock (&_mutex);
219 }
void Insert(Event::AsyncEvent *event)
Insert in queue.
Definition Syncmsg.h:121

References SyncEventQueue::Insert().

◆ Extract()

template<class C >
virtual void Syncmsgobj< C >::Extract ( Event::AsyncEvent *  event)
inlinevirtual

Remove event from queue.

Definition at line 222 of file Syncmsg.h.

222 {
223 if ( pthread_mutex_lock (&_mutex) != 0 )
224 return;
225
226 Event::AsyncEventRef *ref = event->Reference();
227
228 if ( ref != 0 )
229 ref->Extract();
230
231 pthread_mutex_unlock (&_mutex);
232 }

◆ Process()

template<class C >
virtual void Syncmsgobj< C >::Process ( )
inlinevirtual

Process queued events for the object.

Definition at line 236 of file Syncmsg.h.

236 {
237 if ( pthread_mutex_lock (&_mutex) != 0 )
238 return;
239
240 _qcount--;
241
242 if ( _busy ) {
243
244 pthread_mutex_unlock (&_mutex);
245
246 return;
247 }
248
249 _busy = true;
250
251 Event::AsyncEvent *item;
252
253 while ( (item = _queue.First ()) ) {
254
255 if ( _die ) {
256 if ( item->ShouldDelete() )
257 delete item;
258 continue;
259 }
260
261 pthread_mutex_unlock (&_mutex);
262
263 // As the delivery of the item may delete the underlying object,
264 // we need to find out whether this should delete it beforehand
265 bool shouldDelete = item->ShouldDelete();
266
267 item->Deliver ();
268
269 if ( shouldDelete )
270 delete item;
271
272 if ( pthread_mutex_lock (&_mutex) != 0 )
273 return;
274 }
275
276 _busy = false;
277
278 bool killit = _die && !_dying && _qcount <= 0;
279
280 if ( killit )
281 _dying = true;
282
283 pthread_mutex_unlock (&_mutex);
284
285 if ( killit )
286 delete this;
287 }

References SyncEventQueue::First().

◆ msgDeliver()

template<class C >
template<class M >
void Syncmsgobj< C >::msgDeliver ( M *  msg)
inline

template function for delivering message of given type to receiver

Definition at line 290 of file Syncmsg.h.

290 {
291
292 if ( pthread_mutex_lock (&_mutex) != 0 )
293 return;
294
295 if ( _die || _busy ) {
296
297 // Don't bother to queue this message if the entity is going away
298 if ( !_die )
299 _queue.Insert (new SyncEvent<M,C> (msg, this));
300
301 pthread_mutex_unlock (&_mutex);
302
303 return;
304 }
305
306 _busy = true;
307 pthread_mutex_unlock (&_mutex);
308
309 this->actualDeliver (msg);
310
311 if ( pthread_mutex_lock (&_mutex) != 0 )
312 return;
313
314 Event::AsyncEvent *item;
315
316 while ( (item = _queue.First ()) ) {
317
318 if ( _die ) {
319 if ( item->ShouldDelete() )
320 delete item;
321 continue;
322 }
323
324 pthread_mutex_unlock (&_mutex);
325
326 bool shouldDelete = item->ShouldDelete();
327
328 item->Deliver ();
329
330 if ( shouldDelete )
331 delete item;
332
333 if ( pthread_mutex_lock (&_mutex) != 0 )
334 return;
335 }
336
337 _busy = false;
338
339 bool killit = _die && !_dying && _qcount <= 0;
340
341 if ( killit )
342 _dying = true;
343
344 pthread_mutex_unlock (&_mutex);
345
346 if ( killit ) delete this;
347 }
Class used to wrap each event object type for a given receiver.
Definition Syncmsg.h:30

References SyncEventQueue::First(), and SyncEventQueue::Insert().


The documentation for this class was generated from the following file:

All rights reserved © 2002 - 2024 Isode Ltd.