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

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

-timer delete fix (noch auskommentiert), -interface cleanup

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