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