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 | |
| 37 | // IntervalGeneratorPtr |
| 38 | // IntervalGenerator:: Null; |
| 39 | |
| 40 | void errorCallback(int err) |
| 41 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 42 | _LOG_ERROR ("Fatal error: " << err); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | Scheduler::Scheduler() |
| 46 | : m_running(false) |
| 47 | { |
| 48 | event_set_fatal_callback(errorCallback); |
| 49 | evthread_use_pthreads(); |
| 50 | m_base = event_base_new(); |
| 51 | } |
| 52 | |
| 53 | Scheduler::~Scheduler() |
| 54 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 55 | shutdown (); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 56 | event_base_free(m_base); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | Scheduler::eventLoop() |
| 61 | { |
| 62 | while(true) |
| 63 | { |
| 64 | if (event_base_loop(m_base, EVLOOP_NO_EXIT_ON_EMPTY) < 0) |
| 65 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 66 | _LOG_DEBUG ("scheduler loop break error"); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 67 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 69 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 70 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 34edd4d | 2013-01-17 17:55:45 -0800 | [diff] [blame] | 71 | if (!m_running) |
| 72 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 73 | _LOG_DEBUG ("scheduler loop break normal"); |
Alexander Afanasyev | 34edd4d | 2013-01-17 17:55:45 -0800 | [diff] [blame] | 74 | break; |
| 75 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | Scheduler::start() |
| 82 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 83 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 84 | if (!m_running) |
| 85 | { |
| 86 | m_thread = boost::thread(&Scheduler::eventLoop, this); |
| 87 | m_running = true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | Scheduler::shutdown() |
| 93 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 94 | bool breakAndWait = false; |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 95 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 96 | ScopedLock lock (m_mutex); |
| 97 | if (m_running) |
| 98 | { |
| 99 | m_running = false; |
| 100 | breakAndWait = true; |
| 101 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 102 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 103 | |
| 104 | if (breakAndWait) |
| 105 | { |
| 106 | event_base_loopbreak(m_base); |
| 107 | m_thread.join(); |
| 108 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 111 | TaskPtr |
| 112 | Scheduler::scheduleOneTimeTask (SchedulerPtr scheduler, double delay, |
| 113 | const Task::Callback &callback, const Task::Tag &tag) |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 114 | { |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 115 | TaskPtr task = make_shared<OneTimeTask> (callback, tag, scheduler, delay); |
| 116 | if (scheduler->addTask (task)) |
| 117 | return task; |
| 118 | else |
| 119 | return TaskPtr (); |
| 120 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 122 | TaskPtr |
| 123 | Scheduler::schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator, |
| 124 | const Task::Callback &callback, const Task::Tag &tag) |
| 125 | { |
| 126 | TaskPtr task = make_shared<PeriodicTask> (callback, tag, scheduler, delayGenerator); |
| 127 | |
| 128 | if (scheduler->addTask (task)) |
| 129 | return task; |
| 130 | else |
| 131 | return TaskPtr (); |
| 132 | } |
| 133 | |
| 134 | bool |
| 135 | Scheduler::addTask(TaskPtr newTask) |
| 136 | { |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 137 | if (addToMap(newTask)) |
| 138 | { |
| 139 | newTask->reset(); |
| 140 | int res = evtimer_add(newTask->ev(), newTask->tv()); |
| 141 | if (res < 0) |
| 142 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 143 | _LOG_ERROR ("evtimer_add failed for " << newTask->tag()); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | else |
| 148 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 149 | _LOG_ERROR ("fail to add task: " << newTask->tag()); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
Alexander Afanasyev | fb4c43f | 2013-01-18 17:43:25 -0800 | [diff] [blame] | 155 | void |
Alexander Afanasyev | 997ba63 | 2013-01-18 17:40:23 -0800 | [diff] [blame] | 156 | Scheduler::deleteTask(TaskPtr task) |
| 157 | { |
| 158 | deleteTask (task->tag ()); |
| 159 | } |
| 160 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 161 | void |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 162 | Scheduler::rescheduleTask(const TaskPtr &task) |
| 163 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 164 | ScopedLock lock(m_mutex); |
Zhenkai Zhu | d2ca392 | 2013-01-18 17:58:48 -0800 | [diff] [blame] | 165 | TaskMapIt it = m_taskMap.find(task->tag()); |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 166 | if (it != m_taskMap.end()) |
| 167 | { |
| 168 | TaskPtr task = it->second; |
| 169 | task->reset(); |
| 170 | int res = evtimer_add(task->ev(), task->tv()); |
| 171 | if (res < 0) |
| 172 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 173 | _LOG_ERROR ("evtimer_add failed for " << task->tag()); |
Zhenkai Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | addTask(task); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 183 | Scheduler::rescheduleTask(const Task::Tag &tag) |
| 184 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 185 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 186 | TaskMapIt it = m_taskMap.find(tag); |
| 187 | if (it != m_taskMap.end()) |
| 188 | { |
| 189 | TaskPtr task = it->second; |
| 190 | task->reset(); |
| 191 | int res = evtimer_add(task->ev(), task->tv()); |
| 192 | if (res < 0) |
| 193 | { |
| 194 | cout << "evtimer_add failed for " << task->tag() << endl; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | bool |
| 200 | Scheduler::addToMap(const TaskPtr &task) |
| 201 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 202 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 203 | if (m_taskMap.find(task->tag()) == m_taskMap.end()) |
| 204 | { |
| 205 | m_taskMap.insert(make_pair(task->tag(), task)); |
| 206 | return true; |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | Scheduler::deleteTask(const Task::Tag &tag) |
| 213 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 214 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 215 | TaskMapIt it = m_taskMap.find(tag); |
| 216 | if (it != m_taskMap.end()) |
| 217 | { |
| 218 | TaskPtr task = it->second; |
| 219 | evtimer_del(task->ev()); |
| 220 | m_taskMap.erase(it); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | Scheduler::deleteTask(const Task::TaskMatcher &matcher) |
| 226 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 227 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 228 | TaskMapIt it = m_taskMap.begin(); |
| 229 | while(it != m_taskMap.end()) |
| 230 | { |
| 231 | TaskPtr task = it->second; |
| 232 | if (matcher(task)) |
| 233 | { |
| 234 | evtimer_del(task->ev()); |
| 235 | // Use post increment; map.erase invalidate the iterator that is beening erased, |
| 236 | // but does not invalidate other iterators. This seems to be the convention to |
| 237 | // erase something from C++ STL map while traversing. |
| 238 | m_taskMap.erase(it++); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | ++it; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | int |
| 248 | Scheduler::size() |
| 249 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 250 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 251 | return m_taskMap.size(); |
| 252 | } |