| 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 |
|
---|
| 40 | namespace ariba {
|
---|
| 41 | namespace utility {
|
---|
| 42 |
|
---|
| 43 | use_logging_cpp(SystemQueue);
|
---|
| 44 |
|
---|
| 45 | SystemQueue::SystemQueue()
|
---|
| 46 | #ifndef UNDERLAY_OMNET
|
---|
| 47 | : delayScheduler( &directScheduler ), systemQueueRunning( false )
|
---|
| 48 | #endif
|
---|
| 49 | {
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | SystemQueue::~SystemQueue() {
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void SystemQueue::scheduleEvent( const SystemEvent& event, uint32_t delay ) {
|
---|
| 56 | #ifndef UNDERLAY_OMNET
|
---|
| 57 | if( delay == 0 ) directScheduler.insert( event, delay );
|
---|
| 58 | else delayScheduler.insert ( event, delay );
|
---|
| 59 | #else
|
---|
| 60 | Enter_Method_Silent();
|
---|
| 61 | cMessage* msg = new cMessage();
|
---|
| 62 | msg->setContextPointer( new SystemEvent(event) );
|
---|
| 63 |
|
---|
| 64 | if( delay == 0 )
|
---|
| 65 | cSimpleModule::scheduleAt( cSimpleModule::simTime(), msg );
|
---|
| 66 | else
|
---|
| 67 | cSimpleModule::scheduleAt( cSimpleModule::simTime()+((double)delay/1000.0), msg );
|
---|
| 68 | #endif
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | #ifdef UNDERLAY_OMNET
|
---|
| 72 | void SystemQueue::handleMessage(cMessage* msg){
|
---|
| 73 | SystemEvent* event = (SystemEvent*)msg->contextPointer();
|
---|
| 74 |
|
---|
| 75 | event->listener->handleSystemEvent( *event );
|
---|
| 76 |
|
---|
| 77 | delete event; delete msg;
|
---|
| 78 | }
|
---|
| 79 | #endif
|
---|
| 80 |
|
---|
| 81 | void SystemQueue::run() {
|
---|
| 82 | #ifndef UNDERLAY_OMNET
|
---|
| 83 | systemQueueRunning = true;
|
---|
| 84 | directScheduler.run();
|
---|
| 85 | delayScheduler.run();
|
---|
| 86 | #endif
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void SystemQueue::cancel() {
|
---|
| 90 | #ifndef UNDERLAY_OMNET
|
---|
| 91 | systemQueueRunning = false;
|
---|
| 92 | directScheduler.cancel();
|
---|
| 93 | delayScheduler.cancel();
|
---|
| 94 | #endif
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | bool SystemQueue::isEmpty() {
|
---|
| 98 | #ifndef UNDERLAY_OMNET
|
---|
| 99 | return directScheduler.isEmpty() || delayScheduler.isEmpty();
|
---|
| 100 | #else
|
---|
| 101 | return false;
|
---|
| 102 | #endif
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | bool SystemQueue::isRunning() {
|
---|
| 106 | #ifndef UNDERLAY_OMNET
|
---|
| 107 | return systemQueueRunning;
|
---|
| 108 | #else
|
---|
| 109 | return true;
|
---|
| 110 | #endif
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void SystemQueue::enterMethod(){
|
---|
| 114 | // TODO: omnet case and delay scheduler
|
---|
| 115 | directScheduler.enter();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void SystemQueue::leaveMethod(){
|
---|
| 119 | // TODO: omnet case and delay scheduler
|
---|
| 120 | directScheduler.leave();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | //***************************************************************
|
---|
| 124 | #ifndef UNDERLAY_OMNET
|
---|
| 125 |
|
---|
| 126 | SystemQueue::QueueThread::QueueThread(QueueThread* _transferQueue)
|
---|
| 127 | : running( false ), transferQueue( _transferQueue ) {
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | SystemQueue::QueueThread::~QueueThread(){
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | void SystemQueue::QueueThread::run(){
|
---|
| 134 | running = true;
|
---|
| 135 |
|
---|
| 136 | queueThread = new boost::thread(
|
---|
| 137 | boost::bind(&QueueThread::threadFunc, this) );
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void SystemQueue::QueueThread::cancel(){
|
---|
| 141 |
|
---|
| 142 | logging_debug("cancelling system queue");
|
---|
| 143 |
|
---|
| 144 | // cause the thread to exit
|
---|
| 145 | {
|
---|
| 146 | // get the lock, when we got the lock the
|
---|
| 147 | // queue thread must be in itemsAvailable.wait()
|
---|
| 148 | boost::mutex::scoped_lock lock(queueMutex);
|
---|
| 149 |
|
---|
| 150 | // set the running indicator and signal to run on
|
---|
| 151 | // this will run the thread and quit it
|
---|
| 152 | running = false;
|
---|
| 153 | itemsAvailable.notify_all();
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | // wait until the thread has exited
|
---|
| 157 | logging_debug("joining system queue thread");
|
---|
| 158 | queueThread->join();
|
---|
| 159 |
|
---|
| 160 | // delete pending events
|
---|
| 161 | logging_debug("deleting pending system queue events");
|
---|
| 162 | while( eventsQueue.size() > 0 ){
|
---|
| 163 | eventsQueue.erase( eventsQueue.begin() );
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | // delete the thread, so that a subsuquent run() can be called
|
---|
| 167 | delete queueThread;
|
---|
| 168 | queueThread = NULL;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | bool SystemQueue::QueueThread::isEmpty(){
|
---|
| 172 | boost::mutex::scoped_lock lock( queueMutex );
|
---|
| 173 | return eventsQueue.empty();
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void SystemQueue::QueueThread::insert( const SystemEvent& event, uint32_t delay ){
|
---|
| 177 |
|
---|
| 178 | // if this is called from a module that is currently handling
|
---|
| 179 | // a thread (called from SystemQueue::onNextQueueItem), the
|
---|
| 180 | // thread is the same anyway and the mutex will be already
|
---|
| 181 | // aquired, otherwise we aquire it now
|
---|
| 182 |
|
---|
| 183 | boost::mutex::scoped_lock lock( queueMutex );
|
---|
| 184 |
|
---|
| 185 | eventsQueue.push_back( event );
|
---|
| 186 | eventsQueue.back().scheduledTime = boost::posix_time::microsec_clock::local_time();
|
---|
| 187 | eventsQueue.back().delayTime = delay;
|
---|
| 188 | eventsQueue.back().remainingDelay = delay;
|
---|
| 189 |
|
---|
| 190 | onItemInserted( event );
|
---|
| 191 | itemsAvailable.notify_all();
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | void SystemQueue::QueueThread::threadFunc( QueueThread* obj ) {
|
---|
| 195 |
|
---|
| 196 | boost::mutex::scoped_lock lock( obj->queueMutex );
|
---|
| 197 |
|
---|
| 198 | while( obj->running ) {
|
---|
| 199 |
|
---|
| 200 | // wait until an item is in the queue or we are notified
|
---|
| 201 | // to quit the thread. in case the thread is about to
|
---|
| 202 | // quit, the queueThreadRunning variable will indicate
|
---|
| 203 | // this and cause the thread to exit
|
---|
| 204 |
|
---|
| 205 | while ( obj->running && obj->eventsQueue.empty() ){
|
---|
| 206 |
|
---|
| 207 | // const boost::system_time duration =
|
---|
| 208 | // boost::get_system_time() +
|
---|
| 209 | // boost::posix_time::milliseconds(100);
|
---|
| 210 | // obj->itemsAvailable.timed_wait( lock, duration );
|
---|
| 211 |
|
---|
| 212 | obj->itemsAvailable.wait( lock );
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | //
|
---|
| 216 | // work all the items that are currently in the queue
|
---|
| 217 | //
|
---|
| 218 |
|
---|
| 219 | while( obj->running && (!obj->eventsQueue.empty()) ) {
|
---|
| 220 |
|
---|
| 221 | // fetch the first item in the queue
|
---|
| 222 | // and deliver it to the queue handler
|
---|
| 223 | SystemEvent ev = obj->eventsQueue.front();
|
---|
| 224 | obj->eventsQueue.erase( obj->eventsQueue.begin() );
|
---|
| 225 |
|
---|
| 226 | // call the queue and this will
|
---|
| 227 | // call the actual event handler
|
---|
| 228 | obj->onNextQueueItem( ev );
|
---|
| 229 |
|
---|
| 230 | } // !obj->eventsQueue.empty() )
|
---|
| 231 | } // while (obj->running)
|
---|
| 232 |
|
---|
| 233 | logging_debug("system queue exited");
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | void SystemQueue::QueueThread::enter(){
|
---|
| 237 | queueMutex.lock();
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | void SystemQueue::QueueThread::leave(){
|
---|
| 241 | queueMutex.unlock();
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | //***************************************************************
|
---|
| 246 |
|
---|
| 247 | SystemQueue::QueueThreadDirect::QueueThreadDirect(){
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | SystemQueue::QueueThreadDirect::~QueueThreadDirect(){
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | void SystemQueue::QueueThreadDirect::onItemInserted( const SystemEvent& event ){
|
---|
| 254 | // do nothing here
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | void SystemQueue::QueueThreadDirect::onNextQueueItem( const SystemEvent& event ){
|
---|
| 258 | // directly deliver the item to the
|
---|
| 259 | event.getListener()->handleSystemEvent( event );
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | //***************************************************************
|
---|
| 263 |
|
---|
| 264 | SystemQueue::QueueThreadDelay::QueueThreadDelay(QueueThread* _transferQueue)
|
---|
| 265 | : QueueThread( _transferQueue ), isSleeping( false ) {
|
---|
| 266 |
|
---|
| 267 | assert( _transferQueue != NULL );
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | SystemQueue::QueueThreadDelay::~QueueThreadDelay(){
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | void SystemQueue::QueueThreadDelay::onItemInserted( const SystemEvent& event ){
|
---|
| 274 |
|
---|
| 275 | if( !isSleeping) return;
|
---|
| 276 |
|
---|
| 277 | // break an existing sleep and
|
---|
| 278 | // remember the time that was actually slept for
|
---|
| 279 | // and change it for every event in the queue
|
---|
| 280 |
|
---|
| 281 | assert( !eventsQueue.empty());
|
---|
| 282 | sleepCond.notify_all();
|
---|
| 283 |
|
---|
| 284 | ptime sleepEnd = boost::posix_time::microsec_clock::local_time();
|
---|
| 285 | boost::posix_time::time_duration duration = sleepEnd - sleepStart;
|
---|
| 286 | uint32_t sleptTime = duration.total_milliseconds();
|
---|
| 287 |
|
---|
| 288 | EventQueue::iterator i = eventsQueue.begin();
|
---|
| 289 | EventQueue::iterator iend = eventsQueue.end();
|
---|
| 290 |
|
---|
| 291 | for( ; i != iend; i++ ) {
|
---|
| 292 |
|
---|
| 293 | if( sleptTime >= i->remainingDelay)
|
---|
| 294 | i->remainingDelay = 0;
|
---|
| 295 | else
|
---|
| 296 | i->remainingDelay -= sleptTime;
|
---|
| 297 |
|
---|
| 298 | } // for( ; i != iend; i++ )
|
---|
| 299 |
|
---|
| 300 | // now we have to reorder the events
|
---|
| 301 | // in the queue with respect to their remaining delay
|
---|
| 302 | // the SystemQueue::operator< takes care of the
|
---|
| 303 | // ordering with respect to the remaining delay
|
---|
| 304 |
|
---|
| 305 | std::sort( eventsQueue.begin(), eventsQueue.end() );
|
---|
| 306 |
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | void SystemQueue::QueueThreadDelay::onNextQueueItem( const SystemEvent& event ){
|
---|
| 310 |
|
---|
| 311 | // sleeps will be cancelled in the
|
---|
| 312 | // onItemInserted function when a new
|
---|
| 313 | // event arrives during sleeping
|
---|
| 314 |
|
---|
| 315 | assert( !isSleeping );
|
---|
| 316 |
|
---|
| 317 | // the given item is the one with the least
|
---|
| 318 | // amount of sleep time left. because all
|
---|
| 319 | // items are reordered in onItemInserted
|
---|
| 320 |
|
---|
| 321 | if( event.remainingDelay > 0 ) {
|
---|
| 322 |
|
---|
| 323 | const boost::system_time duration =
|
---|
| 324 | boost::get_system_time() +
|
---|
| 325 | boost::posix_time::milliseconds(event.remainingDelay);
|
---|
| 326 |
|
---|
| 327 | {
|
---|
| 328 | boost::unique_lock<boost::mutex> lock( sleepMutex );
|
---|
| 329 |
|
---|
| 330 | sleepStart = boost::posix_time::microsec_clock::local_time();
|
---|
| 331 | isSleeping = true;
|
---|
| 332 |
|
---|
| 333 | sleepCond.timed_wait( lock, duration );
|
---|
| 334 |
|
---|
| 335 | isSleeping = false;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | } // if( event.remainingDelay > 0 )
|
---|
| 339 |
|
---|
| 340 | // if the sleep succeeded and was not
|
---|
| 341 | // interrupted by a new incoming item
|
---|
| 342 | // we can now deliver this event
|
---|
| 343 |
|
---|
| 344 | ptime sleepEnd = boost::posix_time::microsec_clock::local_time();
|
---|
| 345 | boost::posix_time::time_duration duration = sleepEnd - sleepStart;
|
---|
| 346 | uint32_t sleptTime = duration.total_milliseconds();
|
---|
| 347 |
|
---|
| 348 | if (event.remainingDelay <= sleptTime)
|
---|
| 349 | transferQueue->insert( event, 0 );
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | #endif // #ifndef UNDERLAY_OMNET
|
---|
| 353 |
|
---|
| 354 | //***************************************************************
|
---|
| 355 |
|
---|
| 356 | }} // spovnet, common
|
---|