Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 1 | #ifndef EVENT_SCHEDULER_H |
| 2 | #define EVENT_SCHEDULER_H |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 3 | |
| 4 | // use pthread |
| 5 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 6 | #include <event2/event.h> |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 7 | #include <event2/thread.h> |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 8 | #include <event2/event-config.h> |
| 9 | #include <event2/util.h> |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 10 | |
| 11 | #include <boost/function.hpp> |
| 12 | #include <boost/shared_ptr.hpp> |
| 13 | #include <boost/random/mersenne_twister.hpp> |
| 14 | #include <boost/random/uniform_real.hpp> |
| 15 | #include <boost/random/variate_generator.hpp> |
| 16 | #include <boost/exception/all.hpp> |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 17 | #include <boost/thread/shared_mutex.hpp> |
| 18 | #include <boost/thread/thread.hpp> |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 19 | #include <math.h> |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <sys/time.h> |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 22 | |
| 23 | #define _OVERRIDE |
| 24 | #ifdef __GNUC__ |
| 25 | #if __GNUC_MAJOR >= 4 && __GNUC_MINOR__ >= 7 |
| 26 | #undef _OVERRIDE |
| 27 | #define _OVERRIDE override |
| 28 | #endif // __GNUC__ version |
| 29 | #endif // __GNUC__ |
| 30 | |
| 31 | using namespace std; |
| 32 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 33 | // callback used by libevent |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 34 | static void |
| 35 | eventCallback(evutil_socket_t fd, short what, void *arg); |
| 36 | |
| 37 | class Scheduler; |
| 38 | typedef boost::shared_ptr<Scheduler> SchedulerPtr; |
| 39 | class IntervalGenerator; |
| 40 | typedef boost::shared_ptr<IntervalGenerator> IntervalGeneratorPtr; |
| 41 | class Task; |
| 42 | typedef boost::shared_ptr<Task> TaskPtr; |
| 43 | |
| 44 | class IntervalGenerator |
| 45 | { |
| 46 | public: |
| 47 | virtual double |
| 48 | nextInterval() = 0; |
| 49 | static IntervalGeneratorPtr Null; |
| 50 | }; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 51 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 52 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 53 | class Task |
| 54 | { |
| 55 | public: |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 56 | // callback of this task |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 57 | typedef boost::function<void ()> Callback; |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 58 | // tag identifies this task, should be unique |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 59 | typedef string Tag; |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 60 | // used to match tasks |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 61 | typedef boost::function<bool (const TaskPtr &task)> TaskMatcher; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 62 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 63 | |
| 64 | // Task is associated with Schedulers due to the requirement that libevent event is associated with an libevent event_base |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 65 | Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler); |
| 66 | virtual ~Task(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 67 | |
| 68 | virtual void |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 69 | run() = 0; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 70 | |
| 71 | Tag |
| 72 | tag() { return m_tag; } |
| 73 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 74 | event * |
| 75 | ev() { return m_event; } |
| 76 | |
| 77 | timeval * |
| 78 | tv() { return m_tv; } |
| 79 | |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 80 | // Task needs to be resetted after the callback is invoked if it is to be schedule again; just for safety |
| 81 | // it's called by scheduler automatically when addTask or rescheduleTask is called; |
| 82 | // Tasks should do preparation work here (e.g. set up new delay, etc. ) |
| 83 | virtual void |
| 84 | reset() = 0; |
| 85 | |
| 86 | // set delay |
| 87 | // This overrides whatever delay kept in m_tv |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 88 | void |
| 89 | setTv(double delay); |
| 90 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 91 | protected: |
| 92 | Callback m_callback; |
| 93 | Tag m_tag; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 94 | SchedulerPtr m_scheduler; |
| 95 | bool m_invoked; |
| 96 | event *m_event; |
| 97 | timeval *m_tv; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 100 | class OneTimeTask : public Task |
| 101 | { |
| 102 | public: |
| 103 | OneTimeTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, double delay); |
| 104 | virtual ~OneTimeTask(){} |
| 105 | |
| 106 | // invoke callback and mark self as invoked and deregister self from scheduler |
| 107 | virtual void |
| 108 | run() _OVERRIDE; |
| 109 | |
| 110 | // after reset, the task is marked as un-invoked and can be add to scheduler again, with same delay |
| 111 | // if not invoked yet, no effect |
| 112 | virtual void |
| 113 | reset() _OVERRIDE; |
| 114 | |
| 115 | private: |
| 116 | // this is to deregister itself from scheduler automatically after invoke |
| 117 | void |
| 118 | deregisterSelf(); |
| 119 | }; |
| 120 | |
| 121 | class PeriodicTask : public Task |
| 122 | { |
| 123 | public: |
| 124 | // generator is needed only when this is a periodic task |
| 125 | // two simple generators implementation (SimpleIntervalGenerator and RandomIntervalGenerator) are provided; |
| 126 | // if user needs more complex pattern in the intervals between calls, extend class IntervalGenerator |
Zhenkai Zhu | 06b8e95 | 2013-01-15 12:21:15 -0800 | [diff] [blame] | 127 | PeriodicTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator); |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 128 | virtual ~PeriodicTask(){} |
| 129 | |
| 130 | // invoke callback, reset self and ask scheduler to schedule self with the next delay interval |
| 131 | virtual void |
| 132 | run() _OVERRIDE; |
| 133 | |
| 134 | // set the next delay and mark as un-invoke |
| 135 | virtual void |
| 136 | reset() _OVERRIDE; |
| 137 | |
| 138 | private: |
| 139 | IntervalGeneratorPtr m_generator; |
| 140 | }; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 141 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 142 | struct SchedulerException : virtual boost::exception, virtual exception { }; |
| 143 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 144 | class Scheduler |
| 145 | { |
| 146 | public: |
| 147 | Scheduler(); |
| 148 | virtual ~Scheduler(); |
| 149 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 150 | // start event scheduling |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 151 | virtual void |
| 152 | start(); |
| 153 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 154 | // stop event scheduling |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 155 | virtual void |
| 156 | shutdown(); |
| 157 | |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 158 | // if task with the same tag exists, the task is not added and return false |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 159 | virtual bool |
| 160 | addTask(const TaskPtr &task); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 161 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 162 | // delete task by tag, regardless of whether it's invoked or not |
| 163 | // if no task is found, no effect |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 164 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 165 | deleteTask(const Task::Tag &tag); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 166 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 167 | // delete tasks by matcher, regardless of whether it's invoked or not |
| 168 | // this is flexiable in that you can use any form of criteria in finding tasks to delete |
| 169 | // but keep in mind this is a linear scan |
| 170 | |
| 171 | // if no task is found, no effect |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 172 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 173 | deleteTask(const Task::TaskMatcher &matcher); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 174 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 175 | // task must already have been added to the scheduler, otherwise this is no effect |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 176 | // this is usually used by PeriodicTask |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 177 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 178 | rescheduleTask(const Task::Tag &tag); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 179 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 180 | void |
| 181 | eventLoop(); |
| 182 | |
| 183 | event_base * |
| 184 | base() { return m_base; } |
| 185 | |
| 186 | // used in test |
| 187 | int |
| 188 | size(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 189 | |
| 190 | protected: |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 191 | bool |
| 192 | addToMap(const TaskPtr &task); |
| 193 | |
| 194 | protected: |
| 195 | typedef map<Task::Tag, TaskPtr> TaskMap; |
| 196 | typedef map<Task::Tag, TaskPtr>::iterator TaskMapIt; |
| 197 | typedef boost::shared_mutex Mutex; |
| 198 | typedef boost::unique_lock<Mutex> WriteLock; |
| 199 | typedef boost::shared_lock<Mutex> ReadLock; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 200 | TaskMap m_taskMap; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 201 | Mutex m_mutex; |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 202 | bool m_running; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 203 | event_base *m_base; |
| 204 | boost::thread m_thread; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 205 | }; |
| 206 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 207 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 208 | class SimpleIntervalGenerator : public IntervalGenerator |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 209 | { |
| 210 | public: |
| 211 | SimpleIntervalGenerator(double interval) : m_interval(interval) {} |
| 212 | ~SimpleIntervalGenerator(){} |
| 213 | virtual double |
| 214 | nextInterval() _OVERRIDE { return m_interval; } |
| 215 | private: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 216 | double m_interval; |
| 217 | }; |
| 218 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 219 | // generates intervals with uniform distribution |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 220 | class RandomIntervalGenerator : public IntervalGenerator |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 221 | { |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 222 | public: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 223 | typedef enum |
| 224 | { |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 225 | UP = 1, |
| 226 | DOWN = 2, |
| 227 | EVEN = 3 |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 228 | } Direction; |
| 229 | |
| 230 | public: |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame] | 231 | // percent is random-range/interval; e.g. if interval is 10 and you wish the random-range to be 2 |
| 232 | // e.g. 9 ~ 11, percent = 0.2 |
| 233 | // direction shifts the random range; e.g. in the above example, UP would produce a range of |
| 234 | // 10 ~ 12, DOWN of 8 ~ 10, and EVEN of 9 ~ 11 |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 235 | RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN); |
| 236 | ~RandomIntervalGenerator(){} |
| 237 | virtual double |
| 238 | nextInterval() _OVERRIDE; |
| 239 | |
| 240 | private: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 241 | inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); } |
| 242 | |
| 243 | private: |
| 244 | typedef boost::mt19937 RNG_TYPE; |
| 245 | RNG_TYPE m_rng; |
| 246 | boost::uniform_real<> m_dist; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 247 | boost::variate_generator<RNG_TYPE &, boost::uniform_real<> > m_random; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 248 | Direction m_direction; |
| 249 | double m_interval; |
| 250 | double m_percent; |
| 251 | |
| 252 | }; |
| 253 | #endif // EVENT_SCHEDULER_H |