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