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 | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 31 | static void |
| 32 | eventCallback(evutil_socket_t fd, short what, void *arg); |
| 33 | |
| 34 | class Scheduler; |
| 35 | typedef boost::shared_ptr<Scheduler> SchedulerPtr; |
| 36 | class IntervalGenerator; |
| 37 | typedef boost::shared_ptr<IntervalGenerator> IntervalGeneratorPtr; |
| 38 | class Task; |
| 39 | typedef boost::shared_ptr<Task> TaskPtr; |
| 40 | |
| 41 | class IntervalGenerator |
| 42 | { |
| 43 | public: |
| 44 | virtual double |
| 45 | nextInterval() = 0; |
| 46 | static IntervalGeneratorPtr Null; |
| 47 | }; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 48 | |
| 49 | class Task |
| 50 | { |
| 51 | public: |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 52 | typedef boost::function<void ()> Callback; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 53 | typedef string Tag; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 54 | typedef boost::function<bool (const TaskPtr &task)> TaskMatcher; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 55 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 56 | Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator = IntervalGenerator::Null); |
| 57 | ~Task(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 58 | |
| 59 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 60 | run(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 61 | |
| 62 | Tag |
| 63 | tag() { return m_tag; } |
| 64 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 65 | event * |
| 66 | ev() { return m_event; } |
| 67 | |
| 68 | timeval * |
| 69 | tv() { return m_tv; } |
| 70 | |
| 71 | void |
| 72 | setTv(double delay); |
| 73 | |
| 74 | bool |
| 75 | isPeriodic() { return m_generator != IntervalGenerator::Null; } |
| 76 | |
| 77 | void |
| 78 | reset(); |
| 79 | |
| 80 | protected: |
| 81 | void |
| 82 | selfClean(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 83 | |
| 84 | protected: |
| 85 | Callback m_callback; |
| 86 | Tag m_tag; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 87 | SchedulerPtr m_scheduler; |
| 88 | bool m_invoked; |
| 89 | event *m_event; |
| 90 | timeval *m_tv; |
| 91 | IntervalGeneratorPtr m_generator; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 94 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 95 | struct SchedulerException : virtual boost::exception, virtual exception { }; |
| 96 | |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 97 | class Scheduler |
| 98 | { |
| 99 | public: |
| 100 | Scheduler(); |
| 101 | virtual ~Scheduler(); |
| 102 | |
| 103 | virtual void |
| 104 | start(); |
| 105 | |
| 106 | virtual void |
| 107 | shutdown(); |
| 108 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 109 | virtual bool |
| 110 | addTask(const TaskPtr &task, double delay); |
| 111 | |
| 112 | virtual bool |
| 113 | addTask(const TaskPtr &task); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 114 | |
| 115 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 116 | deleteTask(const Task::Tag &tag); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 117 | |
| 118 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 119 | deleteTask(const Task::TaskMatcher &matcher); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 120 | |
| 121 | virtual void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 122 | rescheduleTask(const Task::Tag &tag); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 123 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 124 | void |
| 125 | eventLoop(); |
| 126 | |
| 127 | event_base * |
| 128 | base() { return m_base; } |
| 129 | |
| 130 | // used in test |
| 131 | int |
| 132 | size(); |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 133 | |
| 134 | protected: |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 135 | bool |
| 136 | addToMap(const TaskPtr &task); |
| 137 | |
| 138 | protected: |
| 139 | typedef map<Task::Tag, TaskPtr> TaskMap; |
| 140 | typedef map<Task::Tag, TaskPtr>::iterator TaskMapIt; |
| 141 | typedef boost::shared_mutex Mutex; |
| 142 | typedef boost::unique_lock<Mutex> WriteLock; |
| 143 | typedef boost::shared_lock<Mutex> ReadLock; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 144 | TaskMap m_taskMap; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 145 | Mutex m_mutex; |
| 146 | event_base *m_base; |
| 147 | boost::thread m_thread; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 150 | class SimpleIntervalGenerator : public IntervalGenerator |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 151 | { |
| 152 | public: |
| 153 | SimpleIntervalGenerator(double interval) : m_interval(interval) {} |
| 154 | ~SimpleIntervalGenerator(){} |
| 155 | virtual double |
| 156 | nextInterval() _OVERRIDE { return m_interval; } |
| 157 | private: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 158 | double m_interval; |
| 159 | }; |
| 160 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 161 | class RandomIntervalGenerator : public IntervalGenerator |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 162 | { |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 163 | public: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 164 | typedef enum |
| 165 | { |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 166 | UP = 1, |
| 167 | DOWN = 2, |
| 168 | EVEN = 3 |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 169 | } Direction; |
| 170 | |
| 171 | public: |
| 172 | RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN); |
| 173 | ~RandomIntervalGenerator(){} |
| 174 | virtual double |
| 175 | nextInterval() _OVERRIDE; |
| 176 | |
| 177 | private: |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 178 | inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); } |
| 179 | |
| 180 | private: |
| 181 | typedef boost::mt19937 RNG_TYPE; |
| 182 | RNG_TYPE m_rng; |
| 183 | boost::uniform_real<> m_dist; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 184 | boost::variate_generator<RNG_TYPE &, boost::uniform_real<> > m_random; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 185 | Direction m_direction; |
| 186 | double m_interval; |
| 187 | double m_percent; |
| 188 | |
| 189 | }; |
| 190 | #endif // EVENT_SCHEDULER_H |