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

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

locking für enter method neu

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