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 | // generator is needed only when this is a periodic task |
| 62 | // two simple generators implementation (SimpleIntervalGenerator and RandomIntervalGenerator) are provided; |
| 63 | // if user needs more complex pattern in the intervals between calls, extend class IntervalGenerator |
| 64 | |
| 65 | // Task is associated with Schedulers due to the requirement that libevent event is associated with an libevent event_base |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 66 | Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator = IntervalGenerator::Null); |
| 67 | ~Task(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 68 | |
| 69 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 70 | run(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 71 | |
| 72 | Tag |
| 73 | tag() { return m_tag; } |
| 74 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 75 | event * |
| 76 | ev() { return m_event; } |
| 77 | |
| 78 | timeval * |
| 79 | tv() { return m_tv; } |
| 80 | |
| 81 | void |
| 82 | setTv(double delay); |
| 83 | |
| 84 | bool |
| 85 | isPeriodic() { return m_generator != IntervalGenerator::Null; } |
| 86 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 87 | // Task needs to be resetted after the callback is invoked if it is to be schedule again; just for safety |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 88 | void |
| 89 | reset(); |
| 90 | |
| 91 | protected: |
| 92 | void |
| 93 | selfClean(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 94 | |
| 95 | protected: |
| 96 | Callback m_callback; |
| 97 | Tag m_tag; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 98 | SchedulerPtr m_scheduler; |
| 99 | bool m_invoked; |
| 100 | event *m_event; |
| 101 | timeval *m_tv; |
| 102 | IntervalGeneratorPtr m_generator; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 105 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 106 | struct SchedulerException : virtual boost::exception, virtual exception { }; |
| 107 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 108 | class Scheduler |
| 109 | { |
| 110 | public: |
| 111 | Scheduler(); |
| 112 | virtual ~Scheduler(); |
| 113 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 114 | // start event scheduling |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 115 | virtual void |
| 116 | start(); |
| 117 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 118 | // stop event scheduling |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 119 | virtual void |
| 120 | shutdown(); |
| 121 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 122 | // add a one time task, delay is in seconds |
| 123 | // if task with the same tag exists, return false |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 124 | virtual bool |
| 125 | addTask(const TaskPtr &task, double delay); |
| 126 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 127 | // add periodic task; task must have an interval generator |
| 128 | // if task with the same tag exists, return false |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 129 | virtual bool |
| 130 | addTask(const TaskPtr &task); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 131 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 132 | // delete task by tag, regardless of whether it's invoked or not |
| 133 | // if no task is found, no effect |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 134 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 135 | deleteTask(const Task::Tag &tag); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 136 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 137 | // delete tasks by matcher, regardless of whether it's invoked or not |
| 138 | // this is flexiable in that you can use any form of criteria in finding tasks to delete |
| 139 | // but keep in mind this is a linear scan |
| 140 | |
| 141 | // if no task is found, no effect |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 142 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 143 | deleteTask(const Task::TaskMatcher &matcher); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 144 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 145 | // for periodic tasks, reschedule the next invoke |
| 146 | // task must already have been added to the scheduler, otherwise this is no effect |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 147 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 148 | rescheduleTask(const Task::Tag &tag); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 149 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 150 | void |
| 151 | eventLoop(); |
| 152 | |
| 153 | event_base * |
| 154 | base() { return m_base; } |
| 155 | |
| 156 | // used in test |
| 157 | int |
| 158 | size(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 159 | |
| 160 | protected: |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 161 | bool |
| 162 | addToMap(const TaskPtr &task); |
| 163 | |
| 164 | protected: |
| 165 | typedef map<Task::Tag, TaskPtr> TaskMap; |
| 166 | typedef map<Task::Tag, TaskPtr>::iterator TaskMapIt; |
| 167 | typedef boost::shared_mutex Mutex; |
| 168 | typedef boost::unique_lock<Mutex> WriteLock; |
| 169 | typedef boost::shared_lock<Mutex> ReadLock; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 170 | TaskMap m_taskMap; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 171 | Mutex m_mutex; |
| 172 | event_base *m_base; |
| 173 | boost::thread m_thread; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 176 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 177 | class SimpleIntervalGenerator : public IntervalGenerator |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 178 | { |
| 179 | public: |
| 180 | SimpleIntervalGenerator(double interval) : m_interval(interval) {} |
| 181 | ~SimpleIntervalGenerator(){} |
| 182 | virtual double |
| 183 | nextInterval() _OVERRIDE { return m_interval; } |
| 184 | private: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 185 | double m_interval; |
| 186 | }; |
| 187 | |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 188 | // generates intervals with uniform distribution |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 189 | class RandomIntervalGenerator : public IntervalGenerator |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 190 | { |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 191 | public: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 192 | typedef enum |
| 193 | { |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 194 | UP = 1, |
| 195 | DOWN = 2, |
| 196 | EVEN = 3 |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 197 | } Direction; |
| 198 | |
| 199 | public: |
Zhenkai Zhu | 4eabef1 | 2013-01-08 20:29:52 -0800 | [diff] [blame^] | 200 | // percent is random-range/interval; e.g. if interval is 10 and you wish the random-range to be 2 |
| 201 | // e.g. 9 ~ 11, percent = 0.2 |
| 202 | // direction shifts the random range; e.g. in the above example, UP would produce a range of |
| 203 | // 10 ~ 12, DOWN of 8 ~ 10, and EVEN of 9 ~ 11 |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 204 | RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN); |
| 205 | ~RandomIntervalGenerator(){} |
| 206 | virtual double |
| 207 | nextInterval() _OVERRIDE; |
| 208 | |
| 209 | private: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 210 | inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); } |
| 211 | |
| 212 | private: |
| 213 | typedef boost::mt19937 RNG_TYPE; |
| 214 | RNG_TYPE m_rng; |
| 215 | boost::uniform_real<> m_dist; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 216 | boost::variate_generator<RNG_TYPE &, boost::uniform_real<> > m_random; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 217 | Direction m_direction; |
| 218 | double m_interval; |
| 219 | double m_percent; |
| 220 | |
| 221 | }; |
| 222 | #endif // EVENT_SCHEDULER_H |