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 | |
| 178 | bool |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 179 | Scheduler::addTask(TaskPtr newTask, bool reset/* = true*/) |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 180 | { |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 181 | if (addToMap(newTask)) |
| 182 | { |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 183 | if (reset) |
| 184 | { |
| 185 | newTask->reset(); |
| 186 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 187 | int res = evtimer_add(newTask->ev(), newTask->tv()); |
| 188 | if (res < 0) |
| 189 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 190 | _LOG_ERROR ("evtimer_add failed for " << newTask->tag()); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | else |
| 195 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 196 | _LOG_ERROR ("fail to add task: " << newTask->tag()); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return false; |
| 200 | } |
| 201 | |
Alexander Afanasyev | fb4c43f | 2013-01-18 17:43:25 -0800 | [diff] [blame] | 202 | void |
Alexander Afanasyev | 997ba63 | 2013-01-18 17:40:23 -0800 | [diff] [blame] | 203 | Scheduler::deleteTask(TaskPtr task) |
| 204 | { |
| 205 | deleteTask (task->tag ()); |
| 206 | } |
| 207 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 208 | void |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 209 | Scheduler::rescheduleTask(TaskPtr task) |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 210 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 211 | ScopedLock lock(m_mutex); |
Zhenkai Zhu | d2ca392 | 2013-01-18 17:58:48 -0800 | [diff] [blame] | 212 | TaskMapIt it = m_taskMap.find(task->tag()); |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 213 | if (it != m_taskMap.end()) |
| 214 | { |
| 215 | TaskPtr task = it->second; |
| 216 | task->reset(); |
| 217 | int res = evtimer_add(task->ev(), task->tv()); |
| 218 | if (res < 0) |
| 219 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 220 | _LOG_ERROR ("evtimer_add failed for " << task->tag()); |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | addTask(task); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 230 | Scheduler::rescheduleTask(const Task::Tag &tag) |
| 231 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 232 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 233 | TaskMapIt it = m_taskMap.find(tag); |
| 234 | if (it != m_taskMap.end()) |
| 235 | { |
| 236 | TaskPtr task = it->second; |
| 237 | task->reset(); |
| 238 | int res = evtimer_add(task->ev(), task->tv()); |
| 239 | if (res < 0) |
| 240 | { |
| 241 | cout << "evtimer_add failed for " << task->tag() << endl; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 246 | void |
| 247 | Scheduler::rescheduleTaskAt (const Task::Tag &tag, double time) |
| 248 | { |
| 249 | ScopedLock lock(m_mutex); |
| 250 | TaskMapIt it = m_taskMap.find (tag); |
| 251 | if (it != m_taskMap.end()) |
| 252 | { |
| 253 | TaskPtr task = it->second; |
| 254 | task->reset(); |
| 255 | task->setTv (time); |
| 256 | |
| 257 | int res = evtimer_add(task->ev(), task->tv()); |
| 258 | if (res < 0) |
| 259 | { |
| 260 | _LOG_ERROR ("evtimer_add failed for " << task->tag()); |
| 261 | } |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | _LOG_ERROR ("Task for tag " << tag << " not found"); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | Scheduler::rescheduleTaskAt (TaskPtr task, double time) |
| 271 | { |
| 272 | ScopedLock lock(m_mutex); |
| 273 | TaskMapIt it = m_taskMap.find(task->tag()); |
| 274 | if (it != m_taskMap.end()) |
| 275 | { |
| 276 | TaskPtr task = it->second; |
| 277 | task->reset(); |
| 278 | task->setTv (time); |
| 279 | |
| 280 | int res = evtimer_add(task->ev(), task->tv()); |
| 281 | if (res < 0) |
| 282 | { |
| 283 | _LOG_ERROR ("evtimer_add failed for " << task->tag()); |
| 284 | } |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | task->setTv (time); // force different time |
| 289 | addTask (task, false); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 294 | bool |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 295 | Scheduler::addToMap(TaskPtr task) |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 296 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 297 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 298 | if (m_taskMap.find(task->tag()) == m_taskMap.end()) |
| 299 | { |
| 300 | m_taskMap.insert(make_pair(task->tag(), task)); |
| 301 | return true; |
| 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | Scheduler::deleteTask(const Task::Tag &tag) |
| 308 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 309 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 310 | TaskMapIt it = m_taskMap.find(tag); |
| 311 | if (it != m_taskMap.end()) |
| 312 | { |
| 313 | TaskPtr task = it->second; |
| 314 | evtimer_del(task->ev()); |
| 315 | m_taskMap.erase(it); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | Scheduler::deleteTask(const Task::TaskMatcher &matcher) |
| 321 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 322 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 323 | TaskMapIt it = m_taskMap.begin(); |
| 324 | while(it != m_taskMap.end()) |
| 325 | { |
| 326 | TaskPtr task = it->second; |
| 327 | if (matcher(task)) |
| 328 | { |
| 329 | evtimer_del(task->ev()); |
| 330 | // Use post increment; map.erase invalidate the iterator that is beening erased, |
| 331 | // but does not invalidate other iterators. This seems to be the convention to |
| 332 | // erase something from C++ STL map while traversing. |
| 333 | m_taskMap.erase(it++); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | ++it; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | int |
| 343 | Scheduler::size() |
| 344 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 345 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 346 | return m_taskMap.size(); |
| 347 | } |