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