1 | // [License]
|
---|
2 | // The Ariba-Underlay Copyright
|
---|
3 | //
|
---|
4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
5 | //
|
---|
6 | // Institute of Telematics
|
---|
7 | // UniversitÀt Karlsruhe (TH)
|
---|
8 | // Zirkel 2, 76128 Karlsruhe
|
---|
9 | // Germany
|
---|
10 | //
|
---|
11 | // Redistribution and use in source and binary forms, with or without
|
---|
12 | // modification, are permitted provided that the following conditions are
|
---|
13 | // met:
|
---|
14 | //
|
---|
15 | // 1. Redistributions of source code must retain the above copyright
|
---|
16 | // notice, this list of conditions and the following disclaimer.
|
---|
17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
18 | // notice, this list of conditions and the following disclaimer in the
|
---|
19 | // documentation and/or other materials provided with the distribution.
|
---|
20 | //
|
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | //
|
---|
33 | // The views and conclusions contained in the software and documentation
|
---|
34 | // are those of the authors and should not be interpreted as representing
|
---|
35 | // official policies, either expressed or implied, of the Institute of
|
---|
36 | // Telematics.
|
---|
37 |
|
---|
38 | #include "SystemQueue.h"
|
---|
39 | #include <ariba/utility/misc/sha1.h>
|
---|
40 | #include <stdexcept>
|
---|
41 |
|
---|
42 | namespace ariba {
|
---|
43 | namespace utility {
|
---|
44 |
|
---|
45 | typedef boost::mutex::scoped_lock scoped_lock;
|
---|
46 |
|
---|
47 | using boost::posix_time::microsec_clock;
|
---|
48 | using boost::posix_time::time_duration;
|
---|
49 | using boost::date_time::not_a_date_time;
|
---|
50 | using boost::scoped_ptr;
|
---|
51 |
|
---|
52 |
|
---|
53 | use_logging_cpp(SystemQueue);
|
---|
54 |
|
---|
55 | SystemQueue::SystemQueue() :
|
---|
56 | SysQ( new QueueThread() )
|
---|
57 | {
|
---|
58 | logging_debug("Creating SystemQueue at: " << this);
|
---|
59 | }
|
---|
60 |
|
---|
61 | SystemQueue::~SystemQueue()
|
---|
62 | {
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | SystemQueue& SystemQueue::instance()
|
---|
67 | {
|
---|
68 | static SystemQueue _inst;
|
---|
69 | return _inst;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | void SystemQueue::scheduleEvent( const SystemEvent& event, uint32_t delay )
|
---|
74 | {
|
---|
75 | // copy
|
---|
76 | SystemEvent ev(event);
|
---|
77 |
|
---|
78 | SysQ->insert(ev, delay);
|
---|
79 | }
|
---|
80 |
|
---|
81 | // maps to function call internally to the Event-system
|
---|
82 | void SystemQueue::scheduleCall( const boost::function0<void>& function, uint32_t delay)
|
---|
83 | {
|
---|
84 | // copy function object
|
---|
85 | boost::function0<void>* function_ptr = new boost::function0<void>();
|
---|
86 | (*function_ptr) = function;
|
---|
87 |
|
---|
88 | // schedule special call-event
|
---|
89 | scheduleEvent( SystemEvent(&internal_function_caller, SystemEventType::DEFAULT, function_ptr), delay );
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 | void SystemQueue::run()
|
---|
96 | {
|
---|
97 | assert ( ! SysQ->running );
|
---|
98 |
|
---|
99 | SysQ->running = true;
|
---|
100 |
|
---|
101 | // start thread
|
---|
102 | sysq_thread.reset( new boost::thread(boost::ref(*SysQ)) );
|
---|
103 | }
|
---|
104 |
|
---|
105 | void SystemQueue::cancel()
|
---|
106 | {
|
---|
107 | // signal SysQ to quit (and abort queued events)
|
---|
108 | SysQ->cancel();
|
---|
109 |
|
---|
110 | // wait till actually completes
|
---|
111 | // (should be fast, but the current event is allowed to finish)
|
---|
112 | if ( sysq_thread )
|
---|
113 | {
|
---|
114 | logging_debug("/// ... joining SysQ thread");
|
---|
115 | sysq_thread->join();
|
---|
116 | }
|
---|
117 |
|
---|
118 | // delete thread object
|
---|
119 | sysq_thread.reset();
|
---|
120 |
|
---|
121 | assert ( ! SysQ->isRunning() );
|
---|
122 |
|
---|
123 |
|
---|
124 | // clean up and respawn
|
---|
125 | logging_debug("/// respawning SysQ");
|
---|
126 | SysQ.reset( new QueueThread() );
|
---|
127 | }
|
---|
128 |
|
---|
129 | void SystemQueue::dropAll( const SystemEventListener* mlistener)
|
---|
130 | {
|
---|
131 | // XXX
|
---|
132 | // directScheduler.dropAll(mlistener);
|
---|
133 | // delayScheduler.dropAll(mlistener);
|
---|
134 | }
|
---|
135 |
|
---|
136 | bool SystemQueue::isEmpty()
|
---|
137 | {
|
---|
138 | return SysQ->isEmpty();
|
---|
139 | }
|
---|
140 |
|
---|
141 | bool SystemQueue::isRunning()
|
---|
142 | {
|
---|
143 | return SysQ->isRunning();
|
---|
144 | }
|
---|
145 |
|
---|
146 | // XXX
|
---|
147 | // void SystemQueue::enterMethod(){
|
---|
148 | // // TODO: omnet case and delay scheduler
|
---|
149 | // directScheduler.enter();
|
---|
150 | // }
|
---|
151 | //
|
---|
152 | // void SystemQueue::leaveMethod(){
|
---|
153 | // // TODO: omnet case and delay scheduler
|
---|
154 | // directScheduler.leave();
|
---|
155 | // }
|
---|
156 |
|
---|
157 |
|
---|
158 | SystemQueue::QueueThread::QueueThread() :
|
---|
159 | // now( not_a_date_time ),
|
---|
160 | // next_deadline( not_a_date_time ),
|
---|
161 | processing_event( false ),
|
---|
162 | running( false ),
|
---|
163 | aborted( false )
|
---|
164 | {
|
---|
165 | }
|
---|
166 |
|
---|
167 | SystemQueue::QueueThread::~QueueThread(){
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | void SystemQueue::QueueThread::operator()()
|
---|
172 | {
|
---|
173 | logging_debug( "/// SysQ thread is alive." );
|
---|
174 |
|
---|
175 | assert( running ); // this is set before the thread starts
|
---|
176 |
|
---|
177 | // main loop
|
---|
178 | while ( ! aborted )
|
---|
179 | {
|
---|
180 | // run next immediate event (only one)
|
---|
181 | run_immediate_event();
|
---|
182 |
|
---|
183 | // maintain timed events (move to immediateEventsQ, when deadline expired)
|
---|
184 | check_timed_queue();
|
---|
185 |
|
---|
186 | // wait for next deadline (if no immediate events pending)
|
---|
187 | wait_for_next_deadline();
|
---|
188 | }
|
---|
189 |
|
---|
190 | logging_debug( "/// SysQ thread is quitting." );
|
---|
191 |
|
---|
192 | running = false;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 |
|
---|
197 |
|
---|
198 |
|
---|
199 | /// main loop functions
|
---|
200 |
|
---|
201 | void SystemQueue::QueueThread::run_immediate_event()
|
---|
202 | {
|
---|
203 | // get next event and process it
|
---|
204 | if ( ! immediateEventsQ.empty() )
|
---|
205 | {
|
---|
206 | scoped_ptr<SystemEvent> currently_processed_event;
|
---|
207 |
|
---|
208 | /* dequeue event */
|
---|
209 | // SYNCHRONIZED
|
---|
210 | {
|
---|
211 | scoped_lock lock( queue_mutex );
|
---|
212 |
|
---|
213 | this->processing_event = true;
|
---|
214 |
|
---|
215 | // * dequeue first event *
|
---|
216 | currently_processed_event.reset( new SystemEvent(immediateEventsQ.front()) ); // copy
|
---|
217 | immediateEventsQ.pop_front();
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* dispatch event */
|
---|
221 | logging_debug("/// SysQ: dispatching event");
|
---|
222 |
|
---|
223 | // measure execution time (1/2)
|
---|
224 | ptime start_time = get_clock();
|
---|
225 |
|
---|
226 | // * dispatch event *
|
---|
227 | currently_processed_event->getListener()->handleSystemEvent( *currently_processed_event );
|
---|
228 |
|
---|
229 | // measure execution time (2/2)
|
---|
230 | time_duration execution_time = get_clock() - start_time;
|
---|
231 |
|
---|
232 | // DEBUG OUTPUT: warning when execution takes too much time
|
---|
233 | // [ TODO how long is "too much time"? ]
|
---|
234 | if ( execution_time.total_milliseconds() > 50 )
|
---|
235 | {
|
---|
236 | logging_info("WARNING: Last event took " << execution_time.total_milliseconds() << " to complete.");
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* [ TODO ]
|
---|
240 | *
|
---|
241 | * - we could also measure how long an event has been waiting in the queue before it's dispatched
|
---|
242 | * (in order to detect overload)
|
---|
243 | *
|
---|
244 | * - especially for timed events, the displacement could be calculated
|
---|
245 | * (and, e.g., put in relation with the actual intended sleep time)
|
---|
246 | */
|
---|
247 | }
|
---|
248 |
|
---|
249 | this->processing_event = false;
|
---|
250 | }
|
---|
251 |
|
---|
252 | void SystemQueue::QueueThread::check_timed_queue()
|
---|
253 | {
|
---|
254 | // this whole function is SYNCHRONIZED
|
---|
255 | scoped_lock lock( queue_mutex );
|
---|
256 |
|
---|
257 | ptime now = get_clock();
|
---|
258 | bool not_expired_events_reached = false;
|
---|
259 |
|
---|
260 | // move all expired events into the immediateEventsQ
|
---|
261 | while ( ! timedEventsQ.empty() && ! not_expired_events_reached )
|
---|
262 | {
|
---|
263 | const SystemEvent& ev = timedEventsQ.top();
|
---|
264 |
|
---|
265 | time_duration remaining_sleep_time = ev.deadline - now;
|
---|
266 |
|
---|
267 | // BRANCH: deadline reached
|
---|
268 | if ( remaining_sleep_time.is_negative() )
|
---|
269 | {
|
---|
270 | // move to immediateEventsQ
|
---|
271 | immediateEventsQ.push_back(ev);
|
---|
272 | timedEventsQ.pop();
|
---|
273 | }
|
---|
274 | // BRANCH: deadline not reached
|
---|
275 | else
|
---|
276 | {
|
---|
277 | // okay, that's all for now.
|
---|
278 | not_expired_events_reached = true;
|
---|
279 | }
|
---|
280 | } // while
|
---|
281 | }
|
---|
282 |
|
---|
283 | void SystemQueue::QueueThread::wait_for_next_deadline()
|
---|
284 | {
|
---|
285 | // SYNCHRONIZED
|
---|
286 | boost::mutex::scoped_lock lock(queue_mutex);
|
---|
287 |
|
---|
288 | if ( immediateEventsQ.empty() )
|
---|
289 | {
|
---|
290 | // don't sleep when the SystemQueue is not already canceled
|
---|
291 | if ( aborted )
|
---|
292 | return;
|
---|
293 |
|
---|
294 |
|
---|
295 | // BRANCH: no timed events: sleep "forever" (until new events are scheduled)
|
---|
296 | if ( timedEventsQ.empty() )
|
---|
297 | {
|
---|
298 | logging_debug("/// SysQ is going to sleep.");
|
---|
299 |
|
---|
300 | this->system_queue_idle.wait( lock );
|
---|
301 | }
|
---|
302 | // BRANCH: sleep till next timed event
|
---|
303 | else
|
---|
304 | {
|
---|
305 | logging_debug( "/// SysQ is going to sleep for "
|
---|
306 | << ( timedEventsQ.top().deadline - get_clock() ).total_milliseconds()
|
---|
307 | << "ms. Deadline: "
|
---|
308 | << timedEventsQ.top().deadline
|
---|
309 | << ", Clock: "
|
---|
310 | << get_clock() );
|
---|
311 |
|
---|
312 | this->system_queue_idle.timed_wait( lock, timedEventsQ.top().deadline );
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | /// uniform clock interface
|
---|
319 | ptime SystemQueue::QueueThread::get_clock()
|
---|
320 | {
|
---|
321 | // return microsec_clock::universal_time();
|
---|
322 | return microsec_clock::local_time();
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 |
|
---|
327 | /// external interface
|
---|
328 |
|
---|
329 | bool SystemQueue::QueueThread::isRunning()
|
---|
330 | {
|
---|
331 | return running;
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | void SystemQueue::QueueThread::cancel()
|
---|
336 | {
|
---|
337 | logging_debug("/// Cancelling system queue... ");
|
---|
338 |
|
---|
339 | // SYNCHRONIZED
|
---|
340 | {
|
---|
341 | scoped_lock lock(queue_mutex);
|
---|
342 | aborted = true;
|
---|
343 | }
|
---|
344 |
|
---|
345 | logging_debug("/// SysQ: " << immediateEventsQ.size() << " immediate event(s) + "
|
---|
346 | << timedEventsQ.size() << " timed event(s) left.");
|
---|
347 |
|
---|
348 | system_queue_idle.notify_all();
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | void SystemQueue::QueueThread::insert( SystemEvent& event, uint32_t delay )
|
---|
353 | {
|
---|
354 | event.scheduledTime = get_clock();
|
---|
355 |
|
---|
356 | // SYNCHRONIZED
|
---|
357 | {
|
---|
358 | scoped_lock lock( queue_mutex );
|
---|
359 |
|
---|
360 | // BRANCH: immediate event
|
---|
361 | if ( delay == 0 )
|
---|
362 | {
|
---|
363 | immediateEventsQ.push_back(event);
|
---|
364 | }
|
---|
365 | // BRANCH: timed event
|
---|
366 | else
|
---|
367 | {
|
---|
368 | event.deadline = event.scheduledTime + boost::posix_time::milliseconds(delay);
|
---|
369 | event.delayTime = delay; // XXX I think this is no longer needed..
|
---|
370 |
|
---|
371 | // XXX debug
|
---|
372 | logging_debug("/// inserting timed event, due at: " << event.deadline << " (in " << delay << " ms)");
|
---|
373 |
|
---|
374 | timedEventsQ.push(event);
|
---|
375 |
|
---|
376 | // TODO push sorted.. (use sorted queue..)
|
---|
377 | // timedEventsQ.pu
|
---|
378 | /*
|
---|
379 | * std::priority_queue
|
---|
380 | *
|
---|
381 | * but it orders high-to-low (must reverse order..)
|
---|
382 | *
|
---|
383 | * ... ah cool, da ist direkt eine anleitung fÃŒr reverse order:
|
---|
384 | * http://www.cplusplus.com/reference/queue/priority_queue/priority_queue/
|
---|
385 | */
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | // wake SysQ thread
|
---|
390 | system_queue_idle.notify_one(); // NOTE: there is only one thread
|
---|
391 | // (so it doesn't matter whether to call notify_one, or notify_all)
|
---|
392 | }
|
---|
393 |
|
---|
394 |
|
---|
395 | bool SystemQueue::QueueThread::isEmpty()
|
---|
396 | {
|
---|
397 | // SYNCHRONIZED
|
---|
398 | scoped_lock lock( queue_mutex );
|
---|
399 |
|
---|
400 | return immediateEventsQ.empty() && timedEventsQ.empty() && ! processing_event;
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 |
|
---|
405 | // FIXME
|
---|
406 | void SystemQueue::enterMethod()
|
---|
407 | {
|
---|
408 | assert( false );
|
---|
409 | }
|
---|
410 | void SystemQueue::leaveMethod()
|
---|
411 | {
|
---|
412 | assert( false );
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 |
|
---|
417 | //***************************************************************
|
---|
418 | //// XXX old SystemQueue subclasses
|
---|
419 | // (needed as reference during development of the replacement)
|
---|
420 |
|
---|
421 | // #ifndef UNDERLAY_OMNET
|
---|
422 | #define NOOLDSYSQ
|
---|
423 | #ifndef NOOLDSYSQ
|
---|
424 |
|
---|
425 | void SystemQueue::QueueThread::run(){
|
---|
426 | running = true;
|
---|
427 |
|
---|
428 | queueThread = new boost::thread(
|
---|
429 | boost::bind(&QueueThread::threadFunc, this) );
|
---|
430 | }
|
---|
431 |
|
---|
432 | void SystemQueue::QueueThread::cancel(){
|
---|
433 |
|
---|
434 | logging_debug("cancelling system queue");
|
---|
435 |
|
---|
436 | // cause the thread to exit
|
---|
437 | {
|
---|
438 | // get the lock, when we got the lock the
|
---|
439 | // queue thread must be in itemsAvailable.wait()
|
---|
440 | boost::mutex::scoped_lock lock(queueMutex);
|
---|
441 |
|
---|
442 | // set the running indicator and signal to run on
|
---|
443 | // this will run the thread and quit it
|
---|
444 | running = false;
|
---|
445 | itemsAvailable.notify_all();
|
---|
446 | }
|
---|
447 |
|
---|
448 | // wait until the thread has exited
|
---|
449 | logging_debug("joining system queue thread");
|
---|
450 | queueThread->join();
|
---|
451 |
|
---|
452 | // delete pending events
|
---|
453 | logging_debug("deleting pending system queue events");
|
---|
454 | while( eventsQueue.size() > 0 ){
|
---|
455 | eventsQueue.erase( eventsQueue.begin() );
|
---|
456 | }
|
---|
457 |
|
---|
458 | // delete the thread, so that a subsuquent run() can be called
|
---|
459 | delete queueThread;
|
---|
460 | queueThread = NULL;
|
---|
461 | }
|
---|
462 |
|
---|
463 | bool SystemQueue::QueueThread::isEmpty(){
|
---|
464 | boost::mutex::scoped_lock lock( queueMutex );
|
---|
465 | return eventsQueue.empty();
|
---|
466 | }
|
---|
467 |
|
---|
468 | void SystemQueue::QueueThread::insert( const SystemEvent& event, uint32_t delay ){
|
---|
469 |
|
---|
470 | // if this is called from a module that is currently handling
|
---|
471 | // a thread (called from SystemQueue::onNextQueueItem), the
|
---|
472 | // thread is the same anyway and the mutex will be already
|
---|
473 | // aquired, otherwise we aquire it now
|
---|
474 |
|
---|
475 | boost::mutex::scoped_lock lock( queueMutex );
|
---|
476 |
|
---|
477 | if ( delay > 0 )
|
---|
478 | {
|
---|
479 | logging_debug("SystemQueue(" << this << ") : Schedule event in: " << delay << " ms; Events in queue (before insert): " << eventsQueue.size() );
|
---|
480 | }
|
---|
481 |
|
---|
482 | eventsQueue.push_back( event );
|
---|
483 | eventsQueue.back().scheduledTime = boost::posix_time::microsec_clock::local_time();
|
---|
484 | eventsQueue.back().delayTime = delay;
|
---|
485 | eventsQueue.back().remainingDelay = delay;
|
---|
486 |
|
---|
487 | if ( delay > 0 )
|
---|
488 | {
|
---|
489 | logging_debug("SystemQueue(" << this << ") : Events in queue (after insert): " << eventsQueue.size() );
|
---|
490 | }
|
---|
491 |
|
---|
492 | onItemInserted( event );
|
---|
493 | itemsAvailable.notify_all();
|
---|
494 | }
|
---|
495 |
|
---|
496 | void SystemQueue::QueueThread::dropAll( const SystemEventListener* mlistener) {
|
---|
497 | boost::mutex::scoped_lock lock( queueMutex );
|
---|
498 |
|
---|
499 | bool deleted;
|
---|
500 | do{
|
---|
501 | deleted = false;
|
---|
502 | EventQueue::iterator i = eventsQueue.begin();
|
---|
503 | EventQueue::iterator iend = eventsQueue.end();
|
---|
504 |
|
---|
505 | for( ; i != iend; i++){
|
---|
506 | if((*i).getListener() == mlistener){
|
---|
507 | eventsQueue.erase(i);
|
---|
508 | deleted = true;
|
---|
509 | break;
|
---|
510 | }
|
---|
511 | }
|
---|
512 | }while(deleted);
|
---|
513 | }
|
---|
514 |
|
---|
515 | void SystemQueue::QueueThread::threadFunc( QueueThread* obj ) {
|
---|
516 |
|
---|
517 | boost::mutex::scoped_lock lock( obj->queueMutex );
|
---|
518 |
|
---|
519 | while( obj->running ) {
|
---|
520 |
|
---|
521 | // wait until an item is in the queue or we are notified
|
---|
522 | // to quit the thread. in case the thread is about to
|
---|
523 | // quit, the queueThreadRunning variable will indicate
|
---|
524 | // this and cause the thread to exit
|
---|
525 |
|
---|
526 | while ( obj->running && obj->eventsQueue.empty() ){
|
---|
527 |
|
---|
528 | // const boost::system_time duration =
|
---|
529 | // boost::get_system_time() +
|
---|
530 | // boost::posix_time::milliseconds(100);
|
---|
531 | // obj->itemsAvailable.timed_wait( lock, duration );
|
---|
532 |
|
---|
533 | obj->itemsAvailable.wait( lock );
|
---|
534 | }
|
---|
535 |
|
---|
536 | //
|
---|
537 | // work all the items that are currently in the queue
|
---|
538 | //
|
---|
539 |
|
---|
540 | while( obj->running && (!obj->eventsQueue.empty()) ) {
|
---|
541 |
|
---|
542 | // fetch the first item in the queue
|
---|
543 | // and deliver it to the queue handler
|
---|
544 | SystemEvent ev = obj->eventsQueue.front();
|
---|
545 |
|
---|
546 | // XXX debugging the delay-scheduler..
|
---|
547 | if ( ev.delayTime > 0 )
|
---|
548 | logging_debug("SystemQueue(" << obj << ") : Events in queue (before execution): " << obj->eventsQueue.size());
|
---|
549 |
|
---|
550 | obj->eventsQueue.erase( obj->eventsQueue.begin() );
|
---|
551 |
|
---|
552 | // call the queue and this will
|
---|
553 | // call the actual event handler
|
---|
554 | obj->queueMutex.unlock();
|
---|
555 | obj->onNextQueueItem( ev );
|
---|
556 | obj->queueMutex.lock();
|
---|
557 |
|
---|
558 | // XXX debugging the delay-scheduler..
|
---|
559 | if ( ev.delayTime > 0 )
|
---|
560 | logging_debug("SystemQueue(" << obj << ") : Remaining events in queue (after execution): " << obj->eventsQueue.size());
|
---|
561 |
|
---|
562 | } // !obj->eventsQueue.empty() )
|
---|
563 | } // while (obj->running)
|
---|
564 |
|
---|
565 | logging_debug("system queue exited");
|
---|
566 | }
|
---|
567 |
|
---|
568 | void SystemQueue::QueueThread::enter(){
|
---|
569 | queueMutex.lock();
|
---|
570 | }
|
---|
571 |
|
---|
572 | void SystemQueue::QueueThread::leave(){
|
---|
573 | queueMutex.unlock();
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | //***************************************************************
|
---|
578 |
|
---|
579 | SystemQueue::QueueThreadDirect::QueueThreadDirect(){
|
---|
580 | }
|
---|
581 |
|
---|
582 | SystemQueue::QueueThreadDirect::~QueueThreadDirect(){
|
---|
583 | }
|
---|
584 |
|
---|
585 | void SystemQueue::QueueThreadDirect::onItemInserted( const SystemEvent& event ){
|
---|
586 | // do nothing here
|
---|
587 | }
|
---|
588 |
|
---|
589 | void SystemQueue::QueueThreadDirect::onNextQueueItem( const SystemEvent& event ){
|
---|
590 | // directly deliver the item to the
|
---|
591 | event.getListener()->handleSystemEvent( event );
|
---|
592 | }
|
---|
593 |
|
---|
594 | //***************************************************************
|
---|
595 |
|
---|
596 | SystemQueue::QueueThreadDelay::QueueThreadDelay(QueueThread* _transferQueue)
|
---|
597 | : QueueThread( _transferQueue ), isSleeping( false ) {
|
---|
598 |
|
---|
599 | assert( _transferQueue != NULL );
|
---|
600 | }
|
---|
601 |
|
---|
602 | SystemQueue::QueueThreadDelay::~QueueThreadDelay(){
|
---|
603 | }
|
---|
604 |
|
---|
605 | void SystemQueue::QueueThreadDelay::onItemInserted( const SystemEvent& event ){
|
---|
606 |
|
---|
607 | if( !isSleeping)
|
---|
608 | {
|
---|
609 | logging_warn("SystemQueue(" << this << ") : No, I'm not asleep!! New item inserted.");
|
---|
610 | return; // TODO Mario: shouldn't we sort anyway..?
|
---|
611 | }
|
---|
612 |
|
---|
613 | // break an existing sleep and
|
---|
614 | // remember the time that was actually slept for
|
---|
615 | // and change it for every event in the queue
|
---|
616 |
|
---|
617 | assert( !eventsQueue.empty());
|
---|
618 | sleepCond.notify_all();
|
---|
619 |
|
---|
620 | ptime sleepEnd = boost::posix_time::microsec_clock::local_time();
|
---|
621 | boost::posix_time::time_duration duration = sleepEnd - sleepStart;
|
---|
622 | uint32_t sleptTime = duration.total_milliseconds();
|
---|
623 |
|
---|
624 | EventQueue::iterator i = eventsQueue.begin();
|
---|
625 | EventQueue::iterator iend = eventsQueue.end();
|
---|
626 |
|
---|
627 | logging_debug("SystemQueue(" << this << ") : Adjusting remaining delays:");
|
---|
628 |
|
---|
629 | // TODO Mario: What about the just inserted event..?
|
---|
630 | for( ; i != iend; i++ ) {
|
---|
631 |
|
---|
632 | if( sleptTime >= i->remainingDelay)
|
---|
633 | i->remainingDelay = 0;
|
---|
634 | else
|
---|
635 | {
|
---|
636 | i->remainingDelay -= sleptTime;
|
---|
637 |
|
---|
638 | // XXX Mario: Testcode, just to find a bug...
|
---|
639 | boost::posix_time::time_duration time_passed = sleepEnd - i->getScheduledTime();
|
---|
640 | logging_debug("SystemQueue(" << this << ") : Total: " <<
|
---|
641 | i->delayTime << ", remainingDelay: " << i->remainingDelay <<
|
---|
642 | ", time already passed: " << time_passed.total_milliseconds() );
|
---|
643 | }
|
---|
644 |
|
---|
645 | } // for( ; i != iend; i++ )
|
---|
646 |
|
---|
647 | // now we have to reorder the events
|
---|
648 | // in the queue with respect to their remaining delay
|
---|
649 | // the SystemQueue::operator< takes care of the
|
---|
650 | // ordering with respect to the remaining delay
|
---|
651 |
|
---|
652 | std::sort( eventsQueue.begin(), eventsQueue.end() );
|
---|
653 |
|
---|
654 | }
|
---|
655 |
|
---|
656 | void SystemQueue::QueueThreadDelay::onNextQueueItem( const SystemEvent& event ){
|
---|
657 |
|
---|
658 | // sleeps will be cancelled in the
|
---|
659 | // onItemInserted function when a new
|
---|
660 | // event arrives during sleeping
|
---|
661 |
|
---|
662 | assert( !isSleeping );
|
---|
663 |
|
---|
664 | // the given item is the one with the least
|
---|
665 | // amount of sleep time left. because all
|
---|
666 | // items are reordered in onItemInserted
|
---|
667 |
|
---|
668 | if( event.remainingDelay > 0 ) {
|
---|
669 |
|
---|
670 | const boost::system_time duration =
|
---|
671 | boost::get_system_time() +
|
---|
672 | boost::posix_time::milliseconds(event.remainingDelay);
|
---|
673 |
|
---|
674 | logging_debug("SystemQueue(" << this << ") : Sleeping for: " << event.remainingDelay << " ms");
|
---|
675 |
|
---|
676 | {
|
---|
677 | boost::unique_lock<boost::mutex> lock( sleepMutex );
|
---|
678 |
|
---|
679 | sleepStart = boost::posix_time::microsec_clock::local_time();
|
---|
680 | isSleeping = true;
|
---|
681 |
|
---|
682 | sleepCond.timed_wait( lock, duration );
|
---|
683 |
|
---|
684 | isSleeping = false;
|
---|
685 | }
|
---|
686 |
|
---|
687 | } // if( event.remainingDelay > 0 )
|
---|
688 |
|
---|
689 | // if the sleep succeeded and was not
|
---|
690 | // interrupted by a new incoming item
|
---|
691 | // we can now deliver this event
|
---|
692 |
|
---|
693 | ptime sleepEnd = boost::posix_time::microsec_clock::local_time();
|
---|
694 | boost::posix_time::time_duration duration = sleepEnd - sleepStart;
|
---|
695 | uint32_t sleptTime = duration.total_milliseconds();
|
---|
696 |
|
---|
697 | logging_debug("SystemQueue(" << this << ") : Slept for: " << sleptTime << " ms; until: " << sleepEnd);
|
---|
698 |
|
---|
699 | // TODO MARIO: find the bug that loses events...
|
---|
700 | if (event.remainingDelay <= sleptTime)
|
---|
701 | {
|
---|
702 | logging_debug("SystemQueue(" << this << ") : Transferring scheduled event into the direct queue. Scheduled time: " << event.getScheduledTime() );
|
---|
703 | transferQueue->insert( event, 0 );
|
---|
704 | }
|
---|
705 | else
|
---|
706 | {
|
---|
707 | logging_warn("SystemQueue(" << this << ") : Scheduled event lost!! :-( (Sleep should have been " << event.remainingDelay - sleptTime << " ms longer..)");
|
---|
708 | logging_debug("SystemQueue(" << this << ") : Total delay: " << event.delayTime << "; remaining delay: " << event.remainingDelay);
|
---|
709 |
|
---|
710 | // throw std::logic_error("Scheduled event lost!! :-(");
|
---|
711 | }
|
---|
712 | }
|
---|
713 |
|
---|
714 | #endif // #ifndef UNDERLAY_OMNET
|
---|
715 |
|
---|
716 | //***************************************************************
|
---|
717 |
|
---|
718 | }} // spovnet, common
|
---|