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 |
|
---|
46 | using boost::posix_time::microsec_clock;
|
---|
47 | using boost::posix_time::time_duration;
|
---|
48 | // using boost::mutex::scoped_lock;
|
---|
49 | typedef boost::mutex::scoped_lock scoped_lock;
|
---|
50 | using boost::date_time::not_a_date_time;
|
---|
51 | using boost::scoped_ptr;
|
---|
52 |
|
---|
53 |
|
---|
54 | use_logging_cpp(SystemQueue);
|
---|
55 |
|
---|
56 | SystemQueue::SystemQueue()
|
---|
57 | {
|
---|
58 | logging_info("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 | logging_info("/// joining system queue thread");
|
---|
113 | sysq_thread->join();
|
---|
114 |
|
---|
115 | // delete thread object
|
---|
116 | sysq_thread.reset();
|
---|
117 |
|
---|
118 | assert ( ! SysQ.isRunning() );
|
---|
119 |
|
---|
120 |
|
---|
121 | // TODO FIXME gtest reuses the singleton... :-/
|
---|
122 | // maybe delete the SysQ object here..? (scoped_ptr...)
|
---|
123 | SysQ.aborted = false; // XXX hotfix..
|
---|
124 | }
|
---|
125 |
|
---|
126 | void SystemQueue::dropAll( const SystemEventListener* mlistener)
|
---|
127 | {
|
---|
128 | // XXX
|
---|
129 | // directScheduler.dropAll(mlistener);
|
---|
130 | // delayScheduler.dropAll(mlistener);
|
---|
131 | }
|
---|
132 |
|
---|
133 | bool SystemQueue::isEmpty()
|
---|
134 | {
|
---|
135 | // XXX
|
---|
136 | // return directScheduler.isEmpty() || delayScheduler.isEmpty();
|
---|
137 |
|
---|
138 | return true;
|
---|
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 | running( false ),
|
---|
162 | aborted( false )
|
---|
163 | {
|
---|
164 | }
|
---|
165 |
|
---|
166 | SystemQueue::QueueThread::~QueueThread(){
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | void SystemQueue::QueueThread::operator()()
|
---|
171 | {
|
---|
172 | // XXX debug
|
---|
173 | logging_info( "/// 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 | // XXX debug
|
---|
191 | logging_info( "/// SysQ thread is quitting." );
|
---|
192 |
|
---|
193 | running = false;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 | /// main loop functions
|
---|
201 |
|
---|
202 | void SystemQueue::QueueThread::run_immediate_event()
|
---|
203 | {
|
---|
204 | // measure execution time
|
---|
205 | ptime start_time = get_clock();
|
---|
206 |
|
---|
207 | // get next event and process it
|
---|
208 | if ( ! immediateEventsQ.empty() )
|
---|
209 | {
|
---|
210 | scoped_ptr<SystemEvent> currently_processed_event;
|
---|
211 |
|
---|
212 | // SYNCHRONIZED
|
---|
213 | {
|
---|
214 | scoped_lock lock( queue_mutex );
|
---|
215 |
|
---|
216 | // dequeue first event
|
---|
217 | currently_processed_event.reset( new SystemEvent(immediateEventsQ.front()) ); // copy
|
---|
218 | immediateEventsQ.pop_front();
|
---|
219 | }
|
---|
220 |
|
---|
221 | // XXX debug
|
---|
222 | logging_info("/// SysQ: dispatching event");
|
---|
223 |
|
---|
224 | currently_processed_event->getListener()->handleSystemEvent( *currently_processed_event );
|
---|
225 | }
|
---|
226 |
|
---|
227 | // measure execution time
|
---|
228 | now = get_clock(); // NOTE: this is reused in check_timed_queue();
|
---|
229 | time_duration execution_time = now - start_time;
|
---|
230 |
|
---|
231 | // DEBUG OUTPUT: warning when execution takes too much time
|
---|
232 | // [ TODO how long is "too much time"? ]
|
---|
233 | if ( execution_time.total_milliseconds() > 50 )
|
---|
234 | {
|
---|
235 | logging_debug("WARNING: Last event took " << execution_time.total_milliseconds() << " to complete.");
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | void SystemQueue::QueueThread::check_timed_queue()
|
---|
240 | {
|
---|
241 | // this whole function is SYNCHRONIZED
|
---|
242 | scoped_lock lock( queue_mutex );
|
---|
243 |
|
---|
244 |
|
---|
245 | // NOTE: this->now was just set by this->run_immediate_event()
|
---|
246 |
|
---|
247 | // reset next deadline
|
---|
248 | this->next_deadline = boost::date_time::not_a_date_time;
|
---|
249 |
|
---|
250 | bool not_expired_events_reached = false;
|
---|
251 |
|
---|
252 | // move all expired events into the immediateEventsQ
|
---|
253 | while ( ! timedEventsQ.empty() && ! not_expired_events_reached )
|
---|
254 | {
|
---|
255 | SystemEvent& ev = timedEventsQ.front();
|
---|
256 |
|
---|
257 | time_duration remaining_sleep_time = ev.deadline - this->now;
|
---|
258 |
|
---|
259 | // BRANCH: deadline reached
|
---|
260 | if ( remaining_sleep_time.is_negative() )
|
---|
261 | {
|
---|
262 | // move to immediateEventsQ
|
---|
263 | immediateEventsQ.push_back(ev);
|
---|
264 | timedEventsQ.pop_front();
|
---|
265 | }
|
---|
266 | // BRANCH: deadline not reached
|
---|
267 | else
|
---|
268 | {
|
---|
269 | // store time for next sleep
|
---|
270 | this->next_deadline = ev.deadline;
|
---|
271 |
|
---|
272 | // okay, that's all for now.
|
---|
273 | not_expired_events_reached = true;
|
---|
274 | }
|
---|
275 | } // while
|
---|
276 | }
|
---|
277 |
|
---|
278 | void SystemQueue::QueueThread::wait_for_next_deadline()
|
---|
279 | {
|
---|
280 | if ( immediateEventsQ.empty() )
|
---|
281 | {
|
---|
282 | boost::mutex::scoped_lock lock(queue_mutex);
|
---|
283 |
|
---|
284 | // don't sleep when the SystemQueue is not already canceled
|
---|
285 | if ( aborted )
|
---|
286 | return;
|
---|
287 |
|
---|
288 |
|
---|
289 | // BRANCH: no timed events: sleep "forever" (until new events are scheduled)
|
---|
290 | if ( this->next_deadline.is_not_a_date_time() )
|
---|
291 | {
|
---|
292 | // XXX debug output
|
---|
293 | logging_info("/// SysQ is going to sleep (forever..).");
|
---|
294 |
|
---|
295 | this->system_queue_idle.wait( lock );
|
---|
296 | }
|
---|
297 | // BRANCH: sleep till next timed event
|
---|
298 | else
|
---|
299 | {
|
---|
300 | // XXX debug output
|
---|
301 | time_duration sleep_time = next_deadline - get_clock();
|
---|
302 | logging_info("/// SysQ is going to sleep for " << sleep_time.total_milliseconds() << "ms.");
|
---|
303 |
|
---|
304 | this->system_queue_idle.timed_wait( lock, next_deadline );
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | /// uniform clock interface
|
---|
311 | ptime SystemQueue::QueueThread::get_clock()
|
---|
312 | {
|
---|
313 | return microsec_clock::universal_time();
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 |
|
---|
318 | /// external interface
|
---|
319 |
|
---|
320 | bool SystemQueue::QueueThread::isRunning()
|
---|
321 | {
|
---|
322 | return running;
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | void SystemQueue::QueueThread::cancel()
|
---|
327 | {
|
---|
328 | logging_debug("/// cancelling system queue");
|
---|
329 |
|
---|
330 | // SYNCHRONIZED
|
---|
331 | {
|
---|
332 | scoped_lock lock(queue_mutex);
|
---|
333 | aborted = true;
|
---|
334 | }
|
---|
335 |
|
---|
336 | // XXX debug output
|
---|
337 | logging_info("/// SysQ: " << immediateEventsQ.size() << " immediate event(s) + "
|
---|
338 | << timedEventsQ.size() << " timed event(s) left.");
|
---|
339 |
|
---|
340 | system_queue_idle.notify_all();
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | void SystemQueue::QueueThread::insert( SystemEvent& event, uint32_t delay )
|
---|
345 | {
|
---|
346 | event.scheduledTime = get_clock();
|
---|
347 |
|
---|
348 | // SYNCHRONIZED
|
---|
349 | {
|
---|
350 | scoped_lock lock( queue_mutex );
|
---|
351 |
|
---|
352 | // BRANCH: immediate event
|
---|
353 | if ( delay == 0 )
|
---|
354 | {
|
---|
355 | immediateEventsQ.push_back(event);
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | // wake SysQ thread
|
---|
360 | system_queue_idle.notify_one(); // NOTE: there is only one thread
|
---|
361 | // (so it doesn't matter whether to call notify_one, or notify_all)
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 |
|
---|
366 | // FIXME
|
---|
367 | void SystemQueue::enterMethod()
|
---|
368 | {
|
---|
369 | assert( false );
|
---|
370 | }
|
---|
371 | void SystemQueue::leaveMethod()
|
---|
372 | {
|
---|
373 | assert( false );
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 |
|
---|
378 | //***************************************************************
|
---|
379 | //// XXX old SystemQueue subclasses
|
---|
380 | // (needed as reference during development of the replacement)
|
---|
381 |
|
---|
382 | // #ifndef UNDERLAY_OMNET
|
---|
383 | #define NOOLDSYSQ
|
---|
384 | #ifndef NOOLDSYSQ
|
---|
385 |
|
---|
386 | void SystemQueue::QueueThread::run(){
|
---|
387 | running = true;
|
---|
388 |
|
---|
389 | queueThread = new boost::thread(
|
---|
390 | boost::bind(&QueueThread::threadFunc, this) );
|
---|
391 | }
|
---|
392 |
|
---|
393 | void SystemQueue::QueueThread::cancel(){
|
---|
394 |
|
---|
395 | logging_debug("cancelling system queue");
|
---|
396 |
|
---|
397 | // cause the thread to exit
|
---|
398 | {
|
---|
399 | // get the lock, when we got the lock the
|
---|
400 | // queue thread must be in itemsAvailable.wait()
|
---|
401 | boost::mutex::scoped_lock lock(queueMutex);
|
---|
402 |
|
---|
403 | // set the running indicator and signal to run on
|
---|
404 | // this will run the thread and quit it
|
---|
405 | running = false;
|
---|
406 | itemsAvailable.notify_all();
|
---|
407 | }
|
---|
408 |
|
---|
409 | // wait until the thread has exited
|
---|
410 | logging_debug("joining system queue thread");
|
---|
411 | queueThread->join();
|
---|
412 |
|
---|
413 | // delete pending events
|
---|
414 | logging_debug("deleting pending system queue events");
|
---|
415 | while( eventsQueue.size() > 0 ){
|
---|
416 | eventsQueue.erase( eventsQueue.begin() );
|
---|
417 | }
|
---|
418 |
|
---|
419 | // delete the thread, so that a subsuquent run() can be called
|
---|
420 | delete queueThread;
|
---|
421 | queueThread = NULL;
|
---|
422 | }
|
---|
423 |
|
---|
424 | bool SystemQueue::QueueThread::isEmpty(){
|
---|
425 | boost::mutex::scoped_lock lock( queueMutex );
|
---|
426 | return eventsQueue.empty();
|
---|
427 | }
|
---|
428 |
|
---|
429 | void SystemQueue::QueueThread::insert( const SystemEvent& event, uint32_t delay ){
|
---|
430 |
|
---|
431 | // if this is called from a module that is currently handling
|
---|
432 | // a thread (called from SystemQueue::onNextQueueItem), the
|
---|
433 | // thread is the same anyway and the mutex will be already
|
---|
434 | // aquired, otherwise we aquire it now
|
---|
435 |
|
---|
436 | boost::mutex::scoped_lock lock( queueMutex );
|
---|
437 |
|
---|
438 | if ( delay > 0 )
|
---|
439 | {
|
---|
440 | logging_debug("SystemQueue(" << this << ") : Schedule event in: " << delay << " ms; Events in queue (before insert): " << eventsQueue.size() );
|
---|
441 | }
|
---|
442 |
|
---|
443 | eventsQueue.push_back( event );
|
---|
444 | eventsQueue.back().scheduledTime = boost::posix_time::microsec_clock::local_time();
|
---|
445 | eventsQueue.back().delayTime = delay;
|
---|
446 | eventsQueue.back().remainingDelay = delay;
|
---|
447 |
|
---|
448 | if ( delay > 0 )
|
---|
449 | {
|
---|
450 | logging_debug("SystemQueue(" << this << ") : Events in queue (after insert): " << eventsQueue.size() );
|
---|
451 | }
|
---|
452 |
|
---|
453 | onItemInserted( event );
|
---|
454 | itemsAvailable.notify_all();
|
---|
455 | }
|
---|
456 |
|
---|
457 | void SystemQueue::QueueThread::dropAll( const SystemEventListener* mlistener) {
|
---|
458 | boost::mutex::scoped_lock lock( queueMutex );
|
---|
459 |
|
---|
460 | bool deleted;
|
---|
461 | do{
|
---|
462 | deleted = false;
|
---|
463 | EventQueue::iterator i = eventsQueue.begin();
|
---|
464 | EventQueue::iterator iend = eventsQueue.end();
|
---|
465 |
|
---|
466 | for( ; i != iend; i++){
|
---|
467 | if((*i).getListener() == mlistener){
|
---|
468 | eventsQueue.erase(i);
|
---|
469 | deleted = true;
|
---|
470 | break;
|
---|
471 | }
|
---|
472 | }
|
---|
473 | }while(deleted);
|
---|
474 | }
|
---|
475 |
|
---|
476 | void SystemQueue::QueueThread::threadFunc( QueueThread* obj ) {
|
---|
477 |
|
---|
478 | boost::mutex::scoped_lock lock( obj->queueMutex );
|
---|
479 |
|
---|
480 | while( obj->running ) {
|
---|
481 |
|
---|
482 | // wait until an item is in the queue or we are notified
|
---|
483 | // to quit the thread. in case the thread is about to
|
---|
484 | // quit, the queueThreadRunning variable will indicate
|
---|
485 | // this and cause the thread to exit
|
---|
486 |
|
---|
487 | while ( obj->running && obj->eventsQueue.empty() ){
|
---|
488 |
|
---|
489 | // const boost::system_time duration =
|
---|
490 | // boost::get_system_time() +
|
---|
491 | // boost::posix_time::milliseconds(100);
|
---|
492 | // obj->itemsAvailable.timed_wait( lock, duration );
|
---|
493 |
|
---|
494 | obj->itemsAvailable.wait( lock );
|
---|
495 | }
|
---|
496 |
|
---|
497 | //
|
---|
498 | // work all the items that are currently in the queue
|
---|
499 | //
|
---|
500 |
|
---|
501 | while( obj->running && (!obj->eventsQueue.empty()) ) {
|
---|
502 |
|
---|
503 | // fetch the first item in the queue
|
---|
504 | // and deliver it to the queue handler
|
---|
505 | SystemEvent ev = obj->eventsQueue.front();
|
---|
506 |
|
---|
507 | // XXX debugging the delay-scheduler..
|
---|
508 | if ( ev.delayTime > 0 )
|
---|
509 | logging_debug("SystemQueue(" << obj << ") : Events in queue (before execution): " << obj->eventsQueue.size());
|
---|
510 |
|
---|
511 | obj->eventsQueue.erase( obj->eventsQueue.begin() );
|
---|
512 |
|
---|
513 | // call the queue and this will
|
---|
514 | // call the actual event handler
|
---|
515 | obj->queueMutex.unlock();
|
---|
516 | obj->onNextQueueItem( ev );
|
---|
517 | obj->queueMutex.lock();
|
---|
518 |
|
---|
519 | // XXX debugging the delay-scheduler..
|
---|
520 | if ( ev.delayTime > 0 )
|
---|
521 | logging_debug("SystemQueue(" << obj << ") : Remaining events in queue (after execution): " << obj->eventsQueue.size());
|
---|
522 |
|
---|
523 | } // !obj->eventsQueue.empty() )
|
---|
524 | } // while (obj->running)
|
---|
525 |
|
---|
526 | logging_debug("system queue exited");
|
---|
527 | }
|
---|
528 |
|
---|
529 | void SystemQueue::QueueThread::enter(){
|
---|
530 | queueMutex.lock();
|
---|
531 | }
|
---|
532 |
|
---|
533 | void SystemQueue::QueueThread::leave(){
|
---|
534 | queueMutex.unlock();
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | //***************************************************************
|
---|
539 |
|
---|
540 | SystemQueue::QueueThreadDirect::QueueThreadDirect(){
|
---|
541 | }
|
---|
542 |
|
---|
543 | SystemQueue::QueueThreadDirect::~QueueThreadDirect(){
|
---|
544 | }
|
---|
545 |
|
---|
546 | void SystemQueue::QueueThreadDirect::onItemInserted( const SystemEvent& event ){
|
---|
547 | // do nothing here
|
---|
548 | }
|
---|
549 |
|
---|
550 | void SystemQueue::QueueThreadDirect::onNextQueueItem( const SystemEvent& event ){
|
---|
551 | // directly deliver the item to the
|
---|
552 | event.getListener()->handleSystemEvent( event );
|
---|
553 | }
|
---|
554 |
|
---|
555 | //***************************************************************
|
---|
556 |
|
---|
557 | SystemQueue::QueueThreadDelay::QueueThreadDelay(QueueThread* _transferQueue)
|
---|
558 | : QueueThread( _transferQueue ), isSleeping( false ) {
|
---|
559 |
|
---|
560 | assert( _transferQueue != NULL );
|
---|
561 | }
|
---|
562 |
|
---|
563 | SystemQueue::QueueThreadDelay::~QueueThreadDelay(){
|
---|
564 | }
|
---|
565 |
|
---|
566 | void SystemQueue::QueueThreadDelay::onItemInserted( const SystemEvent& event ){
|
---|
567 |
|
---|
568 | if( !isSleeping)
|
---|
569 | {
|
---|
570 | logging_warn("SystemQueue(" << this << ") : No, I'm not asleep!! New item inserted.");
|
---|
571 | return; // TODO Mario: shouldn't we sort anyway..?
|
---|
572 | }
|
---|
573 |
|
---|
574 | // break an existing sleep and
|
---|
575 | // remember the time that was actually slept for
|
---|
576 | // and change it for every event in the queue
|
---|
577 |
|
---|
578 | assert( !eventsQueue.empty());
|
---|
579 | sleepCond.notify_all();
|
---|
580 |
|
---|
581 | ptime sleepEnd = boost::posix_time::microsec_clock::local_time();
|
---|
582 | boost::posix_time::time_duration duration = sleepEnd - sleepStart;
|
---|
583 | uint32_t sleptTime = duration.total_milliseconds();
|
---|
584 |
|
---|
585 | EventQueue::iterator i = eventsQueue.begin();
|
---|
586 | EventQueue::iterator iend = eventsQueue.end();
|
---|
587 |
|
---|
588 | logging_debug("SystemQueue(" << this << ") : Adjusting remaining delays:");
|
---|
589 |
|
---|
590 | // TODO Mario: What about the just inserted event..?
|
---|
591 | for( ; i != iend; i++ ) {
|
---|
592 |
|
---|
593 | if( sleptTime >= i->remainingDelay)
|
---|
594 | i->remainingDelay = 0;
|
---|
595 | else
|
---|
596 | {
|
---|
597 | i->remainingDelay -= sleptTime;
|
---|
598 |
|
---|
599 | // XXX Mario: Testcode, just to find a bug...
|
---|
600 | boost::posix_time::time_duration time_passed = sleepEnd - i->getScheduledTime();
|
---|
601 | logging_debug("SystemQueue(" << this << ") : Total: " <<
|
---|
602 | i->delayTime << ", remainingDelay: " << i->remainingDelay <<
|
---|
603 | ", time already passed: " << time_passed.total_milliseconds() );
|
---|
604 | }
|
---|
605 |
|
---|
606 | } // for( ; i != iend; i++ )
|
---|
607 |
|
---|
608 | // now we have to reorder the events
|
---|
609 | // in the queue with respect to their remaining delay
|
---|
610 | // the SystemQueue::operator< takes care of the
|
---|
611 | // ordering with respect to the remaining delay
|
---|
612 |
|
---|
613 | std::sort( eventsQueue.begin(), eventsQueue.end() );
|
---|
614 |
|
---|
615 | }
|
---|
616 |
|
---|
617 | void SystemQueue::QueueThreadDelay::onNextQueueItem( const SystemEvent& event ){
|
---|
618 |
|
---|
619 | // sleeps will be cancelled in the
|
---|
620 | // onItemInserted function when a new
|
---|
621 | // event arrives during sleeping
|
---|
622 |
|
---|
623 | assert( !isSleeping );
|
---|
624 |
|
---|
625 | // the given item is the one with the least
|
---|
626 | // amount of sleep time left. because all
|
---|
627 | // items are reordered in onItemInserted
|
---|
628 |
|
---|
629 | if( event.remainingDelay > 0 ) {
|
---|
630 |
|
---|
631 | const boost::system_time duration =
|
---|
632 | boost::get_system_time() +
|
---|
633 | boost::posix_time::milliseconds(event.remainingDelay);
|
---|
634 |
|
---|
635 | logging_debug("SystemQueue(" << this << ") : Sleeping for: " << event.remainingDelay << " ms");
|
---|
636 |
|
---|
637 | {
|
---|
638 | boost::unique_lock<boost::mutex> lock( sleepMutex );
|
---|
639 |
|
---|
640 | sleepStart = boost::posix_time::microsec_clock::local_time();
|
---|
641 | isSleeping = true;
|
---|
642 |
|
---|
643 | sleepCond.timed_wait( lock, duration );
|
---|
644 |
|
---|
645 | isSleeping = false;
|
---|
646 | }
|
---|
647 |
|
---|
648 | } // if( event.remainingDelay > 0 )
|
---|
649 |
|
---|
650 | // if the sleep succeeded and was not
|
---|
651 | // interrupted by a new incoming item
|
---|
652 | // we can now deliver this event
|
---|
653 |
|
---|
654 | ptime sleepEnd = boost::posix_time::microsec_clock::local_time();
|
---|
655 | boost::posix_time::time_duration duration = sleepEnd - sleepStart;
|
---|
656 | uint32_t sleptTime = duration.total_milliseconds();
|
---|
657 |
|
---|
658 | logging_debug("SystemQueue(" << this << ") : Slept for: " << sleptTime << " ms; until: " << sleepEnd);
|
---|
659 |
|
---|
660 | // TODO MARIO: find the bug that loses events...
|
---|
661 | if (event.remainingDelay <= sleptTime)
|
---|
662 | {
|
---|
663 | logging_debug("SystemQueue(" << this << ") : Transferring scheduled event into the direct queue. Scheduled time: " << event.getScheduledTime() );
|
---|
664 | transferQueue->insert( event, 0 );
|
---|
665 | }
|
---|
666 | else
|
---|
667 | {
|
---|
668 | logging_warn("SystemQueue(" << this << ") : Scheduled event lost!! :-( (Sleep should have been " << event.remainingDelay - sleptTime << " ms longer..)");
|
---|
669 | logging_debug("SystemQueue(" << this << ") : Total delay: " << event.delayTime << "; remaining delay: " << event.remainingDelay);
|
---|
670 |
|
---|
671 | // throw std::logic_error("Scheduled event lost!! :-(");
|
---|
672 | }
|
---|
673 | }
|
---|
674 |
|
---|
675 | #endif // #ifndef UNDERLAY_OMNET
|
---|
676 |
|
---|
677 | //***************************************************************
|
---|
678 |
|
---|
679 | }} // spovnet, common
|
---|