An Overlay-based
Virtual Network Substrate
SpoVNet

source: source/ariba/utility/system/SystemQueue.cpp @ 5316

Last change on this file since 5316 was 5316, checked in by Christoph Mayer, 14 years ago

merge from bootstrap branch

File size: 9.5 KB
Line 
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
40namespace ariba {
41namespace utility {
42
43use_logging_cpp(SystemQueue);
44
45SystemQueue::SystemQueue()
46#ifndef UNDERLAY_OMNET
47 : delayScheduler( &directScheduler ), systemQueueRunning( false )
48#endif
49{
50}
51
52SystemQueue::~SystemQueue() {
53}
54
55void 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
72void 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
81void SystemQueue::run() {
82#ifndef UNDERLAY_OMNET
83        systemQueueRunning = true;
84        directScheduler.run();
85        delayScheduler.run();
86#endif
87}
88
89void SystemQueue::cancel() {
90#ifndef UNDERLAY_OMNET
91        systemQueueRunning = false;
92        directScheduler.cancel();
93        delayScheduler.cancel();
94#endif
95}
96
97bool SystemQueue::isEmpty() {
98#ifndef UNDERLAY_OMNET
99        return directScheduler.isEmpty() || delayScheduler.isEmpty();
100#else
101        return false;
102#endif
103}
104
105bool SystemQueue::isRunning() {
106#ifndef UNDERLAY_OMNET
107        return systemQueueRunning;
108#else
109        return true;
110#endif
111}
112
113void SystemQueue::enterMethod(){
114        // TODO: omnet case and delay scheduler
115        directScheduler.enter();
116}
117
118void SystemQueue::leaveMethod(){
119        // TODO: omnet case and delay scheduler
120        directScheduler.leave();
121}
122
123//***************************************************************
124#ifndef UNDERLAY_OMNET
125
126SystemQueue::QueueThread::QueueThread(QueueThread* _transferQueue)
127        : running( false ), transferQueue( _transferQueue ) {
128}
129
130SystemQueue::QueueThread::~QueueThread(){
131}
132
133void SystemQueue::QueueThread::run(){
134        running = true;
135
136        queueThread = new boost::thread(
137                boost::bind(&QueueThread::threadFunc, this) );
138}
139
140void 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
171bool SystemQueue::QueueThread::isEmpty(){
172        boost::mutex::scoped_lock lock( queueMutex );
173        return eventsQueue.empty();
174}
175
176void 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
194void 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->queueMutex.unlock();
229                        obj->onNextQueueItem( ev );
230                        obj->queueMutex.lock();
231
232                } // !obj->eventsQueue.empty() )
233        } // while (obj->running)
234
235        logging_debug("system queue exited");
236}
237
238void SystemQueue::QueueThread::enter(){
239        queueMutex.lock();
240}
241
242void SystemQueue::QueueThread::leave(){
243        queueMutex.unlock();
244}
245
246
247//***************************************************************
248
249SystemQueue::QueueThreadDirect::QueueThreadDirect(){
250}
251
252SystemQueue::QueueThreadDirect::~QueueThreadDirect(){
253}
254
255void SystemQueue::QueueThreadDirect::onItemInserted( const SystemEvent& event ){
256        // do nothing here
257}
258
259void SystemQueue::QueueThreadDirect::onNextQueueItem( const SystemEvent& event ){
260        // directly deliver the item to the
261        event.getListener()->handleSystemEvent( event );
262}
263
264//***************************************************************
265
266SystemQueue::QueueThreadDelay::QueueThreadDelay(QueueThread* _transferQueue)
267        : QueueThread( _transferQueue ), isSleeping( false ) {
268
269        assert( _transferQueue != NULL );
270}
271
272SystemQueue::QueueThreadDelay::~QueueThreadDelay(){
273}
274
275void SystemQueue::QueueThreadDelay::onItemInserted( const SystemEvent& event ){
276
277        if( !isSleeping) return;
278
279        // break an existing sleep and
280        // remember the time that was actually slept for
281        // and change it for every event in the queue
282
283        assert( !eventsQueue.empty());
284        sleepCond.notify_all();
285
286        ptime sleepEnd = boost::posix_time::microsec_clock::local_time();
287        boost::posix_time::time_duration duration = sleepEnd - sleepStart;
288        uint32_t sleptTime = duration.total_milliseconds();
289
290        EventQueue::iterator i = eventsQueue.begin();
291        EventQueue::iterator iend = eventsQueue.end();
292
293        for( ; i != iend; i++ ) {
294
295                if( sleptTime >= i->remainingDelay)
296                        i->remainingDelay = 0;
297                else
298                        i->remainingDelay -= sleptTime;
299
300        } // for( ; i != iend; i++ )
301
302        // now we have to reorder the events
303        // in the queue with respect to their remaining delay
304        // the SystemQueue::operator< takes care of the
305        // ordering with respect to the remaining delay
306
307        std::sort( eventsQueue.begin(), eventsQueue.end() );
308
309}
310
311void SystemQueue::QueueThreadDelay::onNextQueueItem( const SystemEvent& event ){
312
313        // sleeps will be cancelled in the
314        // onItemInserted function when a new
315        // event arrives during sleeping
316
317        assert( !isSleeping );
318
319        // the given item is the one with the least
320        // amount of sleep time left. because all
321        // items are reordered in onItemInserted
322
323        if( event.remainingDelay > 0 ) {
324
325                const boost::system_time duration =
326                        boost::get_system_time() +
327                        boost::posix_time::milliseconds(event.remainingDelay);
328
329                {
330                        boost::unique_lock<boost::mutex> lock( sleepMutex );
331
332                        sleepStart = boost::posix_time::microsec_clock::local_time();
333                        isSleeping = true;
334
335                        sleepCond.timed_wait( lock, duration );
336
337                        isSleeping = false;
338                }
339
340        } // if( event.remainingDelay > 0 )
341
342        // if the sleep succeeded and was not
343        // interrupted by a new incoming item
344        // we can now deliver this event
345
346        ptime sleepEnd = boost::posix_time::microsec_clock::local_time();
347        boost::posix_time::time_duration duration = sleepEnd - sleepStart;
348        uint32_t sleptTime = duration.total_milliseconds();
349
350        if (event.remainingDelay <= sleptTime)
351                transferQueue->insert( event, 0 );
352}
353
354#endif // #ifndef UNDERLAY_OMNET
355
356//***************************************************************
357
358}} // spovnet, common
Note: See TracBrowser for help on using the repository browser.