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 166 of file Syncmsg.h.

Constructor & Destructor Documentation

◆ Syncmsgobj()

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

Definition at line 192 of file Syncmsg.h.

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

◆ ~Syncmsgobj()

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

Definition at line 196 of file Syncmsg.h.

196 {
197 Event::AsyncEvent *item;
198
199 while ( (item = _queue.First()) != 0 )
200 if ( item->ShouldDelete() )
201 delete item;
202
203 pthread_mutex_destroy (&_mutex);
204 }
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 176 of file Syncmsg.h.

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

◆ Queue()

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

Queue event from Event Manager.

Definition at line 208 of file Syncmsg.h.

208 {
209 if ( pthread_mutex_lock (&_mutex) != 0 )
210 return;
211
212 _queue.Insert (event);
213
214 _qcount++;
215
216 pthread_mutex_unlock (&_mutex);
217 }
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 220 of file Syncmsg.h.

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

◆ Process()

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

Process queued events for the object.

Definition at line 234 of file Syncmsg.h.

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

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 288 of file Syncmsg.h.

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