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

Last change on this file since 4712 was 4712, checked in by Christoph Mayer, 15 years ago

system queue fixing

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