Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 1 | #include "event-scheduler.h" |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 2 | #include <utility> |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 3 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 4 | #define EVLOOP_NO_EXIT_ON_EMPTY 0x04 |
| 5 | |
| 6 | IntervalGeneratorPtr |
| 7 | IntervalGenerator:: Null; |
| 8 | |
| 9 | void |
| 10 | eventCallback(evutil_socket_t fd, short what, void *arg) |
| 11 | { |
| 12 | Task *task = static_cast<Task *>(arg); |
| 13 | task->run(); |
| 14 | task = NULL; |
| 15 | } |
| 16 | |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 17 | void errorCallback(int err) |
| 18 | { |
| 19 | cout << "Fatal error: " << err << endl; |
| 20 | } |
| 21 | |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 22 | Task::Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler) |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 23 | : m_callback(callback) |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 24 | , m_tag(tag) |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 25 | , m_scheduler(scheduler) |
| 26 | , m_invoked(false) |
| 27 | , m_event(NULL) |
| 28 | , m_tv(NULL) |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 29 | { |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 30 | m_event = evtimer_new(scheduler->base(), eventCallback, this); |
| 31 | m_tv = new timeval; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 34 | Task::~Task() |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 35 | { |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 36 | if (m_event != NULL) |
| 37 | { |
| 38 | event_free(m_event); |
| 39 | m_event = NULL; |
| 40 | } |
| 41 | |
| 42 | if (m_tv != NULL) |
| 43 | { |
| 44 | delete m_tv; |
| 45 | m_tv = NULL; |
| 46 | } |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 49 | void |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 50 | Task::setTv(double delay) |
| 51 | { |
| 52 | double intPart, fraction; |
| 53 | fraction = modf(abs(delay), &intPart); |
| 54 | m_tv->tv_sec = static_cast<int>(intPart); |
| 55 | m_tv->tv_usec = static_cast<int>((fraction * 1000000)); |
| 56 | } |
| 57 | |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 58 | OneTimeTask::OneTimeTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, double delay) |
| 59 | : Task(callback, tag, scheduler) |
| 60 | { |
| 61 | setTv(delay); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | OneTimeTask::run() |
| 66 | { |
| 67 | if (!m_invoked) |
| 68 | { |
| 69 | m_callback(); |
| 70 | m_invoked = true; |
| 71 | deregisterSelf(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | OneTimeTask::deregisterSelf() |
| 77 | { |
| 78 | m_scheduler->deleteTask(m_tag); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | OneTimeTask::reset() |
| 83 | { |
| 84 | m_invoked = false; |
| 85 | } |
| 86 | |
Zhenkai Zhu | 06b8e95 | 2013-01-15 12:21:15 -0800 | [diff] [blame] | 87 | PeriodicTask::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] | 88 | : Task(callback, tag, scheduler) |
| 89 | , m_generator(generator) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | PeriodicTask::run() |
| 95 | { |
| 96 | if (!m_invoked) |
| 97 | { |
| 98 | m_callback(); |
| 99 | m_invoked = true; |
Zhenkai Zhu | 06b8e95 | 2013-01-15 12:21:15 -0800 | [diff] [blame] | 100 | m_scheduler->rescheduleTask(m_tag); |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | PeriodicTask::reset() |
| 106 | { |
| 107 | m_invoked = false; |
| 108 | double interval = m_generator->nextInterval(); |
| 109 | setTv(interval); |
| 110 | } |
| 111 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 112 | RandomIntervalGenerator::RandomIntervalGenerator(double interval, double percent, Direction direction) |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 113 | : m_interval(interval) |
| 114 | , m_rng(time(NULL)) |
| 115 | , m_percent(percent) |
| 116 | , m_dist(0.0, fractional(percent)) |
| 117 | , m_random(m_rng, m_dist) |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 118 | , m_direction(direction) |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 119 | { |
| 120 | } |
| 121 | |
| 122 | double |
| 123 | RandomIntervalGenerator::nextInterval() |
| 124 | { |
| 125 | double percent = m_random(); |
| 126 | double interval = m_interval; |
| 127 | switch (m_direction) |
| 128 | { |
| 129 | case UP: interval = m_interval * (1.0 + percent); break; |
| 130 | case DOWN: interval = m_interval * (1.0 - percent); break; |
| 131 | case EVEN: interval = m_interval * (1.0 - m_percent/2.0 + percent); break; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 132 | default: break; |
Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return interval; |
| 136 | } |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 137 | |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 138 | Scheduler::Scheduler() : m_running(false) |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 139 | { |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 140 | event_set_fatal_callback(errorCallback); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 141 | evthread_use_pthreads(); |
| 142 | m_base = event_base_new(); |
| 143 | } |
| 144 | |
| 145 | Scheduler::~Scheduler() |
| 146 | { |
| 147 | event_base_free(m_base); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | Scheduler::eventLoop() |
| 152 | { |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 153 | while(true) |
| 154 | { |
| 155 | if (event_base_loop(m_base, EVLOOP_NO_EXIT_ON_EMPTY) < 0) |
| 156 | { |
| 157 | cout << "scheduler loop break error" << endl; |
| 158 | } |
| 159 | ReadLock(m_mutex); |
| 160 | if (!m_running) |
| 161 | { |
| 162 | cout << "scheduler loop break normal" << endl; |
| 163 | break; |
| 164 | } |
| 165 | } |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
| 169 | Scheduler::start() |
| 170 | { |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 171 | { |
| 172 | WriteLock(m_mutex); |
| 173 | m_running = true; |
| 174 | } |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 175 | m_thread = boost::thread(&Scheduler::eventLoop, this); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | Scheduler::shutdown() |
| 180 | { |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 181 | { |
| 182 | WriteLock(m_mutex); |
| 183 | m_running = false; |
| 184 | } |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 185 | event_base_loopbreak(m_base); |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 186 | cout << "shutdown, calling loop break" << endl; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 187 | m_thread.join(); |
| 188 | } |
| 189 | |
| 190 | bool |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 191 | Scheduler::addTask(const TaskPtr &task) |
| 192 | { |
| 193 | TaskPtr newTask = task; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 194 | |
| 195 | if (addToMap(newTask)) |
| 196 | { |
| 197 | newTask->reset(); |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 198 | int res = evtimer_add(newTask->ev(), newTask->tv()); |
| 199 | if (res < 0) |
| 200 | { |
| 201 | cout << "evtimer_add failed for " << task->tag() << endl; |
| 202 | } |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 203 | return true; |
| 204 | } |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 205 | else |
| 206 | { |
| 207 | cout << "fail to add task: " << task->tag() << endl; |
| 208 | } |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 209 | |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | Scheduler::rescheduleTask(const Task::Tag &tag) |
| 215 | { |
| 216 | ReadLock(m_mutex); |
| 217 | TaskMapIt it = m_taskMap.find(tag); |
| 218 | if (it != m_taskMap.end()) |
| 219 | { |
| 220 | TaskPtr task = it->second; |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 221 | task->reset(); |
Zhenkai Zhu | f8e81e0 | 2013-01-15 16:02:47 -0800 | [diff] [blame] | 222 | int res = evtimer_add(task->ev(), task->tv()); |
| 223 | if (res < 0) |
| 224 | { |
| 225 | cout << "evtimer_add failed for " << task->tag() << endl; |
| 226 | } |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | bool |
| 231 | Scheduler::addToMap(const TaskPtr &task) |
| 232 | { |
| 233 | WriteLock(m_mutex); |
| 234 | if (m_taskMap.find(task->tag()) == m_taskMap.end()) |
| 235 | { |
| 236 | m_taskMap.insert(make_pair(task->tag(), task)); |
| 237 | return true; |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | Scheduler::deleteTask(const Task::Tag &tag) |
| 244 | { |
| 245 | WriteLock(m_mutex); |
| 246 | TaskMapIt it = m_taskMap.find(tag); |
| 247 | if (it != m_taskMap.end()) |
| 248 | { |
| 249 | TaskPtr task = it->second; |
| 250 | evtimer_del(task->ev()); |
| 251 | m_taskMap.erase(it); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | Scheduler::deleteTask(const Task::TaskMatcher &matcher) |
| 257 | { |
| 258 | WriteLock(m_mutex); |
| 259 | TaskMapIt it = m_taskMap.begin(); |
| 260 | while(it != m_taskMap.end()) |
| 261 | { |
| 262 | TaskPtr task = it->second; |
| 263 | if (matcher(task)) |
| 264 | { |
| 265 | evtimer_del(task->ev()); |
| 266 | // Use post increment; map.erase invalidate the iterator that is beening erased, |
| 267 | // but does not invalidate other iterators. This seems to be the convention to |
| 268 | // erase something from C++ STL map while traversing. |
| 269 | m_taskMap.erase(it++); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | ++it; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | int |
| 279 | Scheduler::size() |
| 280 | { |
| 281 | ReadLock(m_mutex); |
| 282 | return m_taskMap.size(); |
| 283 | } |