Event::PollManager Class Reference

Event manager used in polling fashion. More...

Inheritance diagram for Event::PollManager:

Public Member Functions

virtual bool IsThreaded ()
 
void Run (bool)
 
bool Poll (const timespec *when, bool immediate, pthread_mutex_t *mutex, pthread_cond_t *cond)
 
virtual void Terminate (unsigned int millisecs)
 
virtual void Blocking ()
 
virtual void Unblocked ()
 
virtual bool LongLived ()
 
virtual void SetThreads (int, int)
 
virtual int GetActiveLimit ()
 
virtual void Queue (AsyncEvent *event, unsigned priority, bool nostart=false)
 
virtual void QueueAt (AsyncEvent *event, const struct timespec *abstime)
 
virtual void QueueAt (AsyncEvent *event, unsigned int millisec)
 
virtual void Dequeue (AsyncEvent *event)
 
virtual void QueueAtExit (AsyncEvent *event)
 
virtual const struct timespec * NextEventDue ()
 

Static Public Member Functions

static Manager * Create ()
 

Detailed Description

Event manager used in polling fashion.

Definition at line 25 of file PollManager.C.

Constructor & Destructor Documentation

◆ PollManager()

Event::PollManager::PollManager ( )

Definition at line 123 of file PollManager.C.

123 : _busy(false)
124 {
125 pthread_mutex_init (&_mutex, NULL);
126 pthread_cond_init (&_cond, NULL);
127
128 _mainQ = new AsyncEventPriorityQueue;
129 _timerQ = new AsyncEventTimerQueue;
130 _atExitQ = new AsyncEventPlainQueue;
131
132 LOG_DEBUG (("PollManager created"));
133 }

Member Function Documentation

◆ Create()

Manager * Event::PollManager::Create ( )
static

Definition at line 135 of file PollManager.C.

135 {
136 if ( threadinstance != NULL )
137 return threadinstance;
138
139 threadinstance = new PollManager ();
140
141 return threadinstance;
142 }

◆ IsThreaded()

virtual bool Event::PollManager::IsThreaded ( )
inlinevirtual

Definition at line 65 of file PollManager.C.

65{ return false; }

◆ Run()

void Event::PollManager::Run ( bool  )
inline

Definition at line 68 of file PollManager.C.

68{}

◆ Poll()

bool Event::PollManager::Poll ( const timespec *  when,
bool  immediate,
pthread_mutex_t *  mutex,
pthread_cond_t *  cond 
)

Definition at line 144 of file PollManager.C.

148 {
149 if ( pthread_mutex_lock (&_mutex) != 0 )
150 return false;
151
152 // Unlock the client mutex for the duration of this call
153 // So that stuff can be delivered to it via events
154 if ( clientmutex ) pthread_mutex_unlock (clientmutex);
155
156 // If the current thread is the busy thread, then recursing,
157 // which is allowed.
158 if ( _busy && !pthread_equal(_thread, pthread_self()) ) {
159 // Someone else is executing the manager
160 if ( !immediate ) {
161 if ( when != 0 )
162 pthread_cond_timedwait (&_cond, &_mutex, when);
163 else
164 pthread_cond_wait (&_cond, &_mutex);
165 }
166
167 } else {
168
169 _busy = true;
170 _thread = pthread_self();
171
172 struct timespec now;
173 getNow (now);
174
175 if ( _timerQ->HasEvents () ) {
176
177 AsyncEvent *tevent;
178
179 // Get any due events onto the main event queue
180 while ( (tevent = _timerQ->First (now)) != 0 ) {
181 LOG_DEBUG (("timerQ event %p due", tevent));
182 _mainQ->Insert (tevent, 0);
183 }
184 }
185
186 _duetime = 0;
187
188 const struct timespec *nextdue = _timerQ->NextPrio();
189
190 if ( nextdue != 0 )
191 _duetime = nextdue;
192
193 if ( when != 0 ) {
194 if ( _duetime == 0 || timespec_cmp (when, _duetime) )
195 _duetime = when;
196 }
197
198 LOG_DEBUG (("PollManager: events=%d",_mainQ->HasEvents()));
199
200 if ( _mainQ->HasEvents() ) {
201
202 AsyncEvent *event = _mainQ->First();
203
204 Receiver *rcvr = event ? event->GetReceiver() : 0;
205
206 // Client mutex is unlocked while events are delivered
207 if ( rcvr != 0 ) {
208 // Queue event to receiver object prior to processing
209 // This avoids the race condition where the event
210 // object is being handled by one thread, and so cannot
211 // be cancelled, but another thread wants to delete it.
212
213 LOG_DEBUG (("Manager: queue %p", event));
214 rcvr->Queue (event);
215
216 pthread_mutex_unlock (&_mutex);
217
218 rcvr->Process ();
219
220 } else {
221
222 pthread_mutex_unlock (&_mutex);
223
224 LOG_DEBUG (("Manager: deliver %p", event));
225
226 event->Deliver();
227 }
228
229 if ( pthread_mutex_lock(&_mutex) != 0 ) {
230 return false;
231 }
232
233 } else if ( _duetime != 0 ) {
234 // Nothing to process until duetime, so wait
235 (void) pthread_cond_timedwait (&_cond, &_mutex, _duetime);
236 }
237
238 _busy = false;
239 }
240
241 bool retval = ( _mainQ->HasEvents() &&
242 *(_mainQ->NextPrio()) < (unsigned) MAXPRIO );
243
244 // Tell any other threads that something has happened
245 pthread_cond_broadcast (&_cond);
246
247 pthread_mutex_unlock (&_mutex);
248
249 if ( clientmutex ) pthread_mutex_lock (clientmutex);
250
251 return retval;
252 }
bool timespec_cmp(const struct timespec *time1, const struct timespec *time2)
Function for comparing two times.
Definition timeutil.h:27
void getNow(struct timespec &now)
Return the current time.
Definition timeutil.h:60

◆ Terminate()

void Event::PollManager::Terminate ( unsigned int  millisecs)
virtual

Definition at line 323 of file PollManager.C.

324 {
325 LOG_DEBUG (("Manager: Terminate in %dms", millisecs));
326
327 struct timespec now;
328
329 getNow (now);
330
331 addMillisecs (now, millisecs);
332
333 if ( pthread_mutex_lock (&_mutex) != 0 ) {
334 pthread_exit(0);
335 }
336
337 // Cancel all queued events
338 AsyncEvent *event;
339
340 while ( (event = _mainQ->First()) != 0 )
341 _deQueue (event);
342
343 while ( (event = _timerQ->First()) != 0 )
344 _deQueue (event);
345
346 // Deliver all at exit events
347 // in the reverse order to their being added
348 while ( (event = _atExitQ->Last()) != 0 ) {
349 // Need to unlock during delivery
350 pthread_mutex_unlock (&_mutex);
351
352 event->Deliver ();
353
354 if ( pthread_mutex_lock (&_mutex) != 0 ) {
355 pthread_exit(0);
356 }
357 }
358
359 pthread_mutex_unlock (&_mutex);
360 }
AsyncEvent * Last()
Remove last element of queue.
void addMillisecs(struct timespec &now, unsigned millisecs)
Add millisecs to a time.
Definition timeutil.h:70

◆ Blocking()

virtual void Event::PollManager::Blocking ( )
inlinevirtual

Definition at line 80 of file PollManager.C.

80{}

◆ Unblocked()

virtual void Event::PollManager::Unblocked ( )
inlinevirtual

Definition at line 83 of file PollManager.C.

83{}

◆ LongLived()

virtual bool Event::PollManager::LongLived ( )
inlinevirtual

Definition at line 87 of file PollManager.C.

87{ return false; }

◆ SetThreads()

virtual void Event::PollManager::SetThreads ( int  ,
int   
)
inlinevirtual

Definition at line 92 of file PollManager.C.

92{}

◆ GetActiveLimit()

virtual int Event::PollManager::GetActiveLimit ( )
inlinevirtual

Definition at line 95 of file PollManager.C.

95{ return 0; }

◆ Queue()

void Event::PollManager::Queue ( AsyncEvent *  event,
unsigned  priority,
bool  nostart = false 
)
virtual

Definition at line 254 of file PollManager.C.

257 {
258 LOG_DEBUG (("Queue: %p prio=%u", event, priority));
259
260 if ( pthread_mutex_lock (&_mutex) != 0 )
261 return;
262
263 _deQueue (event);
264
265 _mainQ->Insert (event, priority);
266
267 pthread_mutex_unlock (&_mutex);
268
269 }

◆ QueueAt()

void Event::PollManager::QueueAt ( AsyncEvent *  event,
const struct timespec *  abstime 
)
virtual

Definition at line 285 of file PollManager.C.

286 {
287 LOG_DEBUG (("Queue: %p @ %d.%9.9ld",
288 event, (int)duetime->tv_sec, duetime->tv_nsec));
289
290 if ( pthread_mutex_lock (&_mutex) != 0 )
291 return;
292
293 _deQueue (event);
294
295 _timerQ->Insert (event, *duetime);
296
297 pthread_mutex_unlock (&_mutex);
298 }

◆ Dequeue()

void Event::PollManager::Dequeue ( AsyncEvent *  event)
virtual

Definition at line 313 of file PollManager.C.

314 {
315 if ( pthread_mutex_lock (&_mutex) != 0 )
316 return;
317
318 _deQueue (event);
319
320 pthread_mutex_unlock (&_mutex);
321 }

◆ QueueAtExit()

void Event::PollManager::QueueAtExit ( AsyncEvent *  event)
virtual

Definition at line 271 of file PollManager.C.

272 {
273 LOG_DEBUG (("QueueAtExit: %p", event));
274
275 if ( pthread_mutex_lock (&_mutex) != 0 )
276 return;
277
278 _deQueue (event);
279
280 _atExitQ->Insert (event);
281
282 pthread_mutex_unlock (&_mutex);
283 }
void Insert(AsyncEvent *event)
Insert in queue.

◆ NextEventDue()

virtual const struct timespec * Event::PollManager::NextEventDue ( )
inlinevirtual

Definition at line 118 of file PollManager.C.

118{ return _duetime; }

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

All rights reserved © 2002 - 2024 Isode Ltd.