[12761] | 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 |
|
---|
| 60 | namespace ariba {
|
---|
| 61 | namespace utility {
|
---|
| 62 |
|
---|
| 63 | using std::list;
|
---|
| 64 | using 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 |
|
---|
| 78 | class SystemQueue : private boost::noncopyable
|
---|
| 79 | {
|
---|
| 80 | use_logging_h(SystemQueue);
|
---|
| 81 | friend class EnterMethod;
|
---|
| 82 | public:
|
---|
| 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 |
|
---|
| 139 | protected:
|
---|
| 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 | */
|
---|
| 168 | private:
|
---|
| 169 |
|
---|
| 170 | typedef 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 | // boost::mutex queueMutex;
|
---|
| 209 |
|
---|
| 210 | // SystemEvent& currently_processed_event;
|
---|
| 211 |
|
---|
| 212 | ptime now;
|
---|
| 213 | ptime next_deadline;
|
---|
| 214 |
|
---|
| 215 | boost::condition_variable system_queue_idle;
|
---|
| 216 | boost::mutex queue_mutex;
|
---|
| 217 |
|
---|
| 218 | // boost::condition_variable itemsAvailable; DEPRECATED
|
---|
| 219 | volatile bool running;
|
---|
| 220 | volatile bool aborted;
|
---|
| 221 | }; // class QueueThread
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | /// inner class of class SystemQueue
|
---|
| 225 | private:
|
---|
| 226 | /**
|
---|
| 227 | * This inner class handles the function-call events.
|
---|
| 228 | * @see SystemQueue::scheduleCall
|
---|
| 229 | */
|
---|
| 230 | class FunctionCaller : public SystemEventListener
|
---|
| 231 | {
|
---|
| 232 | void handleSystemEvent(const SystemEvent& event)
|
---|
| 233 | {
|
---|
| 234 | boost::function0<void>* function_ptr = event.getData< boost::function0<void> >();
|
---|
| 235 | (*function_ptr)();
|
---|
| 236 | delete function_ptr;
|
---|
| 237 | }
|
---|
| 238 | };
|
---|
| 239 |
|
---|
| 240 | FunctionCaller internal_function_caller;
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | /// member variables of class SystemQueue
|
---|
| 245 | private:
|
---|
| 246 | QueueThread SysQ;
|
---|
| 247 | boost::scoped_ptr<boost::thread> sysq_thread;
|
---|
| 248 |
|
---|
| 249 | // volatile bool systemQueueRunning;
|
---|
| 250 |
|
---|
| 251 | }; // class SystemQueue
|
---|
| 252 |
|
---|
| 253 | }} // spovnet, common
|
---|
| 254 |
|
---|
| 255 | #endif /* SYSTEMQUEUE_H_ */
|
---|