Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "scheduler.h" |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 23 | #include "one-time-task.h" |
| 24 | #include "periodic-task.h" |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 25 | #include "logging.h" |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 27 | #include <utility> |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 28 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 30 | INIT_LOGGER ("Scheduler"); |
| 31 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 32 | using namespace std; |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 33 | using namespace boost; |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 34 | |
| 35 | #define EVLOOP_NO_EXIT_ON_EMPTY 0x04 |
| 36 | |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 37 | static void |
| 38 | dummyCallback(evutil_socket_t fd, short what, void *arg) |
| 39 | { |
| 40 | // 1 year later, that was a long run for the app |
Zhenkai Zhu | 3fe1172 | 2013-01-25 16:22:46 -0800 | [diff] [blame] | 41 | // let's wait for another year |
| 42 | timeval tv; |
| 43 | tv.tv_sec = 365 * 24 * 3600; |
| 44 | tv.tv_usec = 0; |
| 45 | event *ev = *(event **)arg; |
| 46 | int res = evtimer_add(ev, &tv); |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 49 | // IntervalGeneratorPtr |
| 50 | // IntervalGenerator:: Null; |
| 51 | |
| 52 | void errorCallback(int err) |
| 53 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 54 | _LOG_ERROR ("Fatal error: " << err); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | Scheduler::Scheduler() |
| 58 | : m_running(false) |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 59 | , m_executor(1) |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 60 | { |
| 61 | event_set_fatal_callback(errorCallback); |
| 62 | evthread_use_pthreads(); |
| 63 | m_base = event_base_new(); |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 64 | |
| 65 | // This is a hack to prevent event_base_loop from exiting; |
| 66 | // the flag EVLOOP_NO_EXIT_ON_EMPTY is somehow ignored, at least on Mac OS X |
| 67 | // it's going to be scheduled to 10 years later |
| 68 | timeval tv; |
| 69 | tv.tv_sec = 365 * 24 * 3600; |
| 70 | tv.tv_usec = 0; |
Zhenkai Zhu | 3fe1172 | 2013-01-25 16:22:46 -0800 | [diff] [blame] | 71 | m_ev = evtimer_new(m_base, dummyCallback, &m_ev); |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 72 | int res = evtimer_add(m_ev, &tv); |
| 73 | if (res < 0) |
| 74 | { |
| 75 | _LOG_ERROR("heck"); |
| 76 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | Scheduler::~Scheduler() |
| 80 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 81 | shutdown (); |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 82 | evtimer_del(m_ev); |
| 83 | event_free(m_ev); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 84 | event_base_free(m_base); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | Scheduler::eventLoop() |
| 89 | { |
| 90 | while(true) |
| 91 | { |
| 92 | if (event_base_loop(m_base, EVLOOP_NO_EXIT_ON_EMPTY) < 0) |
| 93 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 94 | _LOG_DEBUG ("scheduler loop break error"); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 95 | } |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 96 | else |
| 97 | { |
| 98 | _LOG_DEBUG ("scheduler loop break normal"); |
| 99 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 101 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 102 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 34edd4d | 2013-01-17 17:55:45 -0800 | [diff] [blame] | 103 | if (!m_running) |
| 104 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 105 | _LOG_DEBUG ("scheduler loop break normal"); |
Alexander Afanasyev | 34edd4d | 2013-01-17 17:55:45 -0800 | [diff] [blame] | 106 | break; |
| 107 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 108 | } |
Zhenkai Zhu | 238db85 | 2013-01-25 16:27:28 -0800 | [diff] [blame] | 109 | |
| 110 | // just to prevent craziness in CPU usage which supposedly should not happen |
| 111 | // after adding the dummy event |
| 112 | usleep(1000); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 117 | Scheduler::execute(Executor::Job job) |
| 118 | { |
| 119 | m_executor.execute(job); |
| 120 | } |
| 121 | |
| 122 | void |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 123 | Scheduler::start() |
| 124 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 125 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 126 | if (!m_running) |
| 127 | { |
| 128 | m_thread = boost::thread(&Scheduler::eventLoop, this); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 129 | m_executor.start(); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 130 | m_running = true; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | Scheduler::shutdown() |
| 136 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 137 | bool breakAndWait = false; |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 138 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 139 | ScopedLock lock (m_mutex); |
| 140 | if (m_running) |
| 141 | { |
| 142 | m_running = false; |
| 143 | breakAndWait = true; |
| 144 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 145 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 146 | |
| 147 | if (breakAndWait) |
| 148 | { |
| 149 | event_base_loopbreak(m_base); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 150 | m_executor.shutdown(); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 151 | m_thread.join(); |
| 152 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 155 | TaskPtr |
| 156 | Scheduler::scheduleOneTimeTask (SchedulerPtr scheduler, double delay, |
| 157 | const Task::Callback &callback, const Task::Tag &tag) |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 158 | { |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 159 | TaskPtr task = make_shared<OneTimeTask> (callback, tag, scheduler, delay); |
| 160 | if (scheduler->addTask (task)) |
| 161 | return task; |
| 162 | else |
| 163 | return TaskPtr (); |
| 164 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 166 | TaskPtr |
| 167 | Scheduler::schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator, |
| 168 | const Task::Callback &callback, const Task::Tag &tag) |
| 169 | { |
| 170 | TaskPtr task = make_shared<PeriodicTask> (callback, tag, scheduler, delayGenerator); |
| 171 | |
| 172 | if (scheduler->addTask (task)) |
| 173 | return task; |
| 174 | else |
| 175 | return TaskPtr (); |
| 176 | } |
| 177 | |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame^] | 178 | TaskPtr |
| 179 | Scheduler::scheduleDelayOneTimeTask (SchedulerPtr scheduler, double delay, |
| 180 | const Task::Callback &callback, const Task::Tag &tag) |
| 181 | { |
| 182 | TaskPtr task = make_shared<OneTimeTask> (callback, tag, scheduler, delay); |
| 183 | if (scheduler->addTask (task)) |
| 184 | return task; |
| 185 | else{ |
| 186 | _LOG_ERROR ("reschedule task for " << tag); |
| 187 | scheduler->rescheduleTask(tag); |
| 188 | return TaskPtr (); |
| 189 | } |
| 190 | } |
| 191 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 192 | bool |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 193 | Scheduler::addTask(TaskPtr newTask, bool reset/* = true*/) |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 194 | { |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 195 | if (addToMap(newTask)) |
| 196 | { |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 197 | if (reset) |
| 198 | { |
| 199 | newTask->reset(); |
| 200 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 201 | int res = evtimer_add(newTask->ev(), newTask->tv()); |
| 202 | if (res < 0) |
| 203 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 204 | _LOG_ERROR ("evtimer_add failed for " << newTask->tag()); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | else |
| 209 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 210 | _LOG_ERROR ("fail to add task: " << newTask->tag()); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return false; |
| 214 | } |
| 215 | |
Alexander Afanasyev | fb4c43f | 2013-01-18 17:43:25 -0800 | [diff] [blame] | 216 | void |
Alexander Afanasyev | 997ba63 | 2013-01-18 17:40:23 -0800 | [diff] [blame] | 217 | Scheduler::deleteTask(TaskPtr task) |
| 218 | { |
| 219 | deleteTask (task->tag ()); |
| 220 | } |
| 221 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 222 | void |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 223 | Scheduler::rescheduleTask(TaskPtr task) |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 224 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 225 | ScopedLock lock(m_mutex); |
Zhenkai Zhu | d2ca392 | 2013-01-18 17:58:48 -0800 | [diff] [blame] | 226 | TaskMapIt it = m_taskMap.find(task->tag()); |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 227 | if (it != m_taskMap.end()) |
| 228 | { |
| 229 | TaskPtr task = it->second; |
| 230 | task->reset(); |
| 231 | int res = evtimer_add(task->ev(), task->tv()); |
| 232 | if (res < 0) |
| 233 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 234 | _LOG_ERROR ("evtimer_add failed for " << task->tag()); |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | addTask(task); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 244 | Scheduler::rescheduleTask(const Task::Tag &tag) |
| 245 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 246 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 247 | TaskMapIt it = m_taskMap.find(tag); |
| 248 | if (it != m_taskMap.end()) |
| 249 | { |
| 250 | TaskPtr task = it->second; |
| 251 | task->reset(); |
| 252 | int res = evtimer_add(task->ev(), task->tv()); |
| 253 | if (res < 0) |
| 254 | { |
| 255 | cout << "evtimer_add failed for " << task->tag() << endl; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 260 | void |
| 261 | Scheduler::rescheduleTaskAt (const Task::Tag &tag, double time) |
| 262 | { |
| 263 | ScopedLock lock(m_mutex); |
| 264 | TaskMapIt it = m_taskMap.find (tag); |
| 265 | if (it != m_taskMap.end()) |
| 266 | { |
| 267 | TaskPtr task = it->second; |
| 268 | task->reset(); |
| 269 | task->setTv (time); |
| 270 | |
| 271 | int res = evtimer_add(task->ev(), task->tv()); |
| 272 | if (res < 0) |
| 273 | { |
| 274 | _LOG_ERROR ("evtimer_add failed for " << task->tag()); |
| 275 | } |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | _LOG_ERROR ("Task for tag " << tag << " not found"); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | Scheduler::rescheduleTaskAt (TaskPtr task, double time) |
| 285 | { |
| 286 | ScopedLock lock(m_mutex); |
| 287 | TaskMapIt it = m_taskMap.find(task->tag()); |
| 288 | if (it != m_taskMap.end()) |
| 289 | { |
| 290 | TaskPtr task = it->second; |
| 291 | task->reset(); |
| 292 | task->setTv (time); |
| 293 | |
| 294 | int res = evtimer_add(task->ev(), task->tv()); |
| 295 | if (res < 0) |
| 296 | { |
| 297 | _LOG_ERROR ("evtimer_add failed for " << task->tag()); |
| 298 | } |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | task->setTv (time); // force different time |
| 303 | addTask (task, false); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 308 | bool |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 309 | Scheduler::addToMap(TaskPtr task) |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 310 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 311 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 312 | if (m_taskMap.find(task->tag()) == m_taskMap.end()) |
| 313 | { |
| 314 | m_taskMap.insert(make_pair(task->tag(), task)); |
| 315 | return true; |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | void |
| 321 | Scheduler::deleteTask(const Task::Tag &tag) |
| 322 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 323 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 324 | TaskMapIt it = m_taskMap.find(tag); |
| 325 | if (it != m_taskMap.end()) |
| 326 | { |
| 327 | TaskPtr task = it->second; |
| 328 | evtimer_del(task->ev()); |
| 329 | m_taskMap.erase(it); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void |
| 334 | Scheduler::deleteTask(const Task::TaskMatcher &matcher) |
| 335 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 336 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 337 | TaskMapIt it = m_taskMap.begin(); |
| 338 | while(it != m_taskMap.end()) |
| 339 | { |
| 340 | TaskPtr task = it->second; |
| 341 | if (matcher(task)) |
| 342 | { |
| 343 | evtimer_del(task->ev()); |
| 344 | // Use post increment; map.erase invalidate the iterator that is beening erased, |
| 345 | // but does not invalidate other iterators. This seems to be the convention to |
| 346 | // erase something from C++ STL map while traversing. |
| 347 | m_taskMap.erase(it++); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | ++it; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | int |
| 357 | Scheduler::size() |
| 358 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 359 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 360 | return m_taskMap.size(); |
| 361 | } |