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) |
| 59 | { |
| 60 | event_set_fatal_callback(errorCallback); |
| 61 | evthread_use_pthreads(); |
| 62 | m_base = event_base_new(); |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 63 | |
| 64 | // This is a hack to prevent event_base_loop from exiting; |
| 65 | // the flag EVLOOP_NO_EXIT_ON_EMPTY is somehow ignored, at least on Mac OS X |
| 66 | // it's going to be scheduled to 10 years later |
| 67 | timeval tv; |
| 68 | tv.tv_sec = 365 * 24 * 3600; |
| 69 | tv.tv_usec = 0; |
Zhenkai Zhu | 3fe1172 | 2013-01-25 16:22:46 -0800 | [diff] [blame^] | 70 | m_ev = evtimer_new(m_base, dummyCallback, &m_ev); |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 71 | int res = evtimer_add(m_ev, &tv); |
| 72 | if (res < 0) |
| 73 | { |
| 74 | _LOG_ERROR("heck"); |
| 75 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Scheduler::~Scheduler() |
| 79 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 80 | shutdown (); |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 81 | evtimer_del(m_ev); |
| 82 | event_free(m_ev); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 83 | event_base_free(m_base); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | Scheduler::eventLoop() |
| 88 | { |
| 89 | while(true) |
| 90 | { |
| 91 | if (event_base_loop(m_base, EVLOOP_NO_EXIT_ON_EMPTY) < 0) |
| 92 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 93 | _LOG_DEBUG ("scheduler loop break error"); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 94 | } |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 95 | else |
| 96 | { |
| 97 | _LOG_DEBUG ("scheduler loop break normal"); |
| 98 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 100 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 101 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 34edd4d | 2013-01-17 17:55:45 -0800 | [diff] [blame] | 102 | if (!m_running) |
| 103 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 104 | _LOG_DEBUG ("scheduler loop break normal"); |
Alexander Afanasyev | 34edd4d | 2013-01-17 17:55:45 -0800 | [diff] [blame] | 105 | break; |
| 106 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | Scheduler::start() |
| 113 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 114 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 115 | if (!m_running) |
| 116 | { |
| 117 | m_thread = boost::thread(&Scheduler::eventLoop, this); |
| 118 | m_running = true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | Scheduler::shutdown() |
| 124 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 125 | bool breakAndWait = false; |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 126 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 127 | ScopedLock lock (m_mutex); |
| 128 | if (m_running) |
| 129 | { |
| 130 | m_running = false; |
| 131 | breakAndWait = true; |
| 132 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 133 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 134 | |
| 135 | if (breakAndWait) |
| 136 | { |
| 137 | event_base_loopbreak(m_base); |
| 138 | m_thread.join(); |
| 139 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 142 | TaskPtr |
| 143 | Scheduler::scheduleOneTimeTask (SchedulerPtr scheduler, double delay, |
| 144 | const Task::Callback &callback, const Task::Tag &tag) |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 145 | { |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 146 | TaskPtr task = make_shared<OneTimeTask> (callback, tag, scheduler, delay); |
| 147 | if (scheduler->addTask (task)) |
| 148 | return task; |
| 149 | else |
| 150 | return TaskPtr (); |
| 151 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 152 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 153 | TaskPtr |
| 154 | Scheduler::schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator, |
| 155 | const Task::Callback &callback, const Task::Tag &tag) |
| 156 | { |
| 157 | TaskPtr task = make_shared<PeriodicTask> (callback, tag, scheduler, delayGenerator); |
| 158 | |
| 159 | if (scheduler->addTask (task)) |
| 160 | return task; |
| 161 | else |
| 162 | return TaskPtr (); |
| 163 | } |
| 164 | |
| 165 | bool |
| 166 | Scheduler::addTask(TaskPtr newTask) |
| 167 | { |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 168 | if (addToMap(newTask)) |
| 169 | { |
| 170 | newTask->reset(); |
| 171 | int res = evtimer_add(newTask->ev(), newTask->tv()); |
| 172 | if (res < 0) |
| 173 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 174 | _LOG_ERROR ("evtimer_add failed for " << newTask->tag()); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | else |
| 179 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 180 | _LOG_ERROR ("fail to add task: " << newTask->tag()); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
Alexander Afanasyev | fb4c43f | 2013-01-18 17:43:25 -0800 | [diff] [blame] | 186 | void |
Alexander Afanasyev | 997ba63 | 2013-01-18 17:40:23 -0800 | [diff] [blame] | 187 | Scheduler::deleteTask(TaskPtr task) |
| 188 | { |
| 189 | deleteTask (task->tag ()); |
| 190 | } |
| 191 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 192 | void |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 193 | Scheduler::rescheduleTask(const TaskPtr &task) |
| 194 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 195 | ScopedLock lock(m_mutex); |
Zhenkai Zhu | d2ca392 | 2013-01-18 17:58:48 -0800 | [diff] [blame] | 196 | TaskMapIt it = m_taskMap.find(task->tag()); |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 197 | if (it != m_taskMap.end()) |
| 198 | { |
| 199 | TaskPtr task = it->second; |
| 200 | task->reset(); |
| 201 | int res = evtimer_add(task->ev(), task->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 " << task->tag()); |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | addTask(task); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 214 | Scheduler::rescheduleTask(const Task::Tag &tag) |
| 215 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 216 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 217 | TaskMapIt it = m_taskMap.find(tag); |
| 218 | if (it != m_taskMap.end()) |
| 219 | { |
| 220 | TaskPtr task = it->second; |
| 221 | task->reset(); |
| 222 | int res = evtimer_add(task->ev(), task->tv()); |
| 223 | if (res < 0) |
| 224 | { |
| 225 | cout << "evtimer_add failed for " << task->tag() << endl; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | bool |
| 231 | Scheduler::addToMap(const TaskPtr &task) |
| 232 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 233 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 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 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 245 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 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 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 258 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 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 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 281 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 282 | return m_taskMap.size(); |
| 283 | } |