source: source/ariba/utility/system/SystemQueue.h@ 12762

Last change on this file since 12762 was 12762, checked in by hock@…, 10 years ago

New System Queue

Threading test (aka "isEmpty() test") is now passed, too.

File size: 6.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// [License]
38
39#ifndef SYSTEMQUEUE_H_
40#define SYSTEMQUEUE_H_
41
42// #include <vector>
43#include <list>
44#include <cassert>
45#include "SystemEvent.h"
46#include "SystemEventListener.h"
47#include "ariba/utility/logging/Logging.h"
48#include <boost/date_time.hpp>
49#include <boost/cstdint.hpp>
50#include <boost/scoped_ptr.hpp>
51
52#include <boost/thread/mutex.hpp>
53#include <boost/thread/thread.hpp>
54#include <boost/thread/condition_variable.hpp>
55#include <boost/utility.hpp>
56#include <boost/bind.hpp>
57
58#include <boost/function.hpp>
59
60namespace ariba {
61namespace utility {
62
63using std::list;
64using boost::posix_time::ptime;
65
66
67/**
68 * This class implements a simple system event queue to allow
69 * a simulation of cooperative multitasking. It also allows
70 * events to be scheduled from other tasks. This allows
71 * dispatching asynchronous tasks.
72 *
73 * @author Christoph Mayer, Sebastian Mies
74 */
75/** XXX Mario Hock -- reworking the entire module **/
76
77
78class SystemQueue : private boost::noncopyable
79{
80 use_logging_h(SystemQueue);
81 friend class EnterMethod;
82public:
83 /**
84 * Get the SystemQueue singleton instance.
85 */
86 static SystemQueue& instance();
87
88 /**
89 * This methods schedules a given event.
90 *
91 * @param The event to be scheduled
92 * @param The delay in milli-seconds
93 */
94 void scheduleEvent( const SystemEvent& event, uint32_t delay = 0 );
95
96 /**
97 * This method schedules a function call in the SystemQueue.
98 * (Like scheduleEvent, but to be used with boost::bind.)
99 *
100 * @param function: The function to be called [void function()]
101 * @param The delay in milli-seconds
102 */
103 void scheduleCall( const boost::function0<void>& function, uint32_t delay = 0 );
104
105 /**
106 * Starts the processing and waiting for events.
107 * Use <code>cancel()</code> to end system queue processing and
108 * <code>isEmpty()</code> to check wheter the queue is empty.
109 */
110 void run();
111
112 /**
113 * Cancels the system queue and ends the processing after the
114 * currently processed event is processed.
115 *
116 * This method is thread-safe.
117 */
118 void cancel();
119
120 /**
121 * Drop all queued events for that listener
122 */
123 void dropAll( const SystemEventListener* mlistener);
124
125 /**
126 * Check wheter this queue has items or not.
127 *
128 * @return True, if this queue is empty.
129 */
130 bool isEmpty();
131
132 /**
133 * Is the system queue already started and running?
134 *
135 * @return True, if the system queue is running.
136 */
137 bool isRunning();
138
139protected:
140
141 /**
142 * Aqcuire the mutex
143 */
144 void enterMethod();
145
146 /**
147 * Leave the mutex
148 */
149 void leaveMethod();
150
151 /**
152 * Constructs a system queue.
153 */
154 SystemQueue();
155
156 /**
157 * Destroys the system queue. Beware that all events
158 * are canceled
159 */
160 ~SystemQueue();
161
162
163/**
164 * inner class of class SystemQueue:
165 *
166 * QueueThread -- the class the does the actual work
167 */
168private:
169
170typedef list<SystemEvent> EventQueue;
171
172 //********************************************************
173
174 class QueueThread
175 {
176 friend class SystemQueue;
177
178 public:
179 QueueThread();
180 virtual ~QueueThread();
181
182 /// main loop -- called from boost::thread
183 void operator()();
184
185// void run();
186 void cancel();
187 bool isEmpty();
188 void insert( SystemEvent& event, uint32_t delay );
189 void enter();
190 void leave();
191 void dropAll( const SystemEventListener* mlistener);
192 bool isRunning();
193
194 private:
195
196 /// main loop functions
197 void run_immediate_event();
198 void check_timed_queue();
199 void wait_for_next_deadline();
200
201 /// makes sure that always the same clock is used
202 ptime get_clock();
203
204
205 private:
206 EventQueue immediateEventsQ;
207 EventQueue timedEventsQ;
208
209 ptime now;
210 ptime next_deadline;
211
212 boost::condition_variable system_queue_idle;
213 boost::mutex queue_mutex;
214
215 bool processing_event;
216
217 volatile bool running;
218 volatile bool aborted;
219 }; // class QueueThread
220
221
222/// inner class of class SystemQueue
223private:
224 /**
225 * This inner class handles the function-call events.
226 * @see SystemQueue::scheduleCall
227 */
228 class FunctionCaller : public SystemEventListener
229 {
230 void handleSystemEvent(const SystemEvent& event)
231 {
232 boost::function0<void>* function_ptr = event.getData< boost::function0<void> >();
233 (*function_ptr)();
234 delete function_ptr;
235 }
236 };
237
238 FunctionCaller internal_function_caller;
239
240
241
242 /// member variables of class SystemQueue
243private:
244 boost::scoped_ptr<QueueThread> SysQ;
245 boost::scoped_ptr<boost::thread> sysq_thread;
246
247// volatile bool systemQueueRunning;
248
249}; // class SystemQueue
250
251}} // spovnet, common
252
253#endif /* SYSTEMQUEUE_H_ */
Note: See TracBrowser for help on using the repository browser.