blob: 2e0e5b508309379471b1611d3bdb0d32fcf229cf [file] [log] [blame]
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -08001/* -*- 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 Afanasyeve41e7d22013-01-19 15:13:47 -080023#include "one-time-task.h"
24#include "periodic-task.h"
Alexander Afanasyevfc720362013-01-24 21:49:48 -080025#include "logging.h"
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080026
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080027#include <utility>
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080028#include <boost/make_shared.hpp>
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080029
Alexander Afanasyevfc720362013-01-24 21:49:48 -080030INIT_LOGGER ("Scheduler");
31
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080032using namespace std;
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080033using namespace boost;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080034
35#define EVLOOP_NO_EXIT_ON_EMPTY 0x04
36
Zhenkai Zhub16be8f2013-01-25 16:12:30 -080037static void
38dummyCallback(evutil_socket_t fd, short what, void *arg)
39{
40 // 1 year later, that was a long run for the app
Zhenkai Zhu3fe11722013-01-25 16:22:46 -080041 // 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 Zhub16be8f2013-01-25 16:12:30 -080047}
48
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080049// IntervalGeneratorPtr
50// IntervalGenerator:: Null;
51
52void errorCallback(int err)
53{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080054 _LOG_ERROR ("Fatal error: " << err);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080055}
56
57Scheduler::Scheduler()
58 : m_running(false)
59{
60 event_set_fatal_callback(errorCallback);
61 evthread_use_pthreads();
62 m_base = event_base_new();
Zhenkai Zhub16be8f2013-01-25 16:12:30 -080063
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 Zhu3fe11722013-01-25 16:22:46 -080070 m_ev = evtimer_new(m_base, dummyCallback, &m_ev);
Zhenkai Zhub16be8f2013-01-25 16:12:30 -080071 int res = evtimer_add(m_ev, &tv);
72 if (res < 0)
73 {
74 _LOG_ERROR("heck");
75 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080076}
77
78Scheduler::~Scheduler()
79{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080080 shutdown ();
Zhenkai Zhub16be8f2013-01-25 16:12:30 -080081 evtimer_del(m_ev);
82 event_free(m_ev);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080083 event_base_free(m_base);
84}
85
86void
87Scheduler::eventLoop()
88{
89 while(true)
90 {
91 if (event_base_loop(m_base, EVLOOP_NO_EXIT_ON_EMPTY) < 0)
92 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -080093 _LOG_DEBUG ("scheduler loop break error");
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080094 }
Zhenkai Zhub16be8f2013-01-25 16:12:30 -080095 else
96 {
97 _LOG_DEBUG ("scheduler loop break normal");
98 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080099
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800100 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800101 ScopedLock lock(m_mutex);
Alexander Afanasyev34edd4d2013-01-17 17:55:45 -0800102 if (!m_running)
103 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800104 _LOG_DEBUG ("scheduler loop break normal");
Alexander Afanasyev34edd4d2013-01-17 17:55:45 -0800105 break;
106 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800107 }
108 }
109}
110
111void
112Scheduler::start()
113{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800114 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800115 if (!m_running)
116 {
117 m_thread = boost::thread(&Scheduler::eventLoop, this);
118 m_running = true;
119 }
120}
121
122void
123Scheduler::shutdown()
124{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800125 bool breakAndWait = false;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800126 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800127 ScopedLock lock (m_mutex);
128 if (m_running)
129 {
130 m_running = false;
131 breakAndWait = true;
132 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800133 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800134
135 if (breakAndWait)
136 {
137 event_base_loopbreak(m_base);
138 m_thread.join();
139 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800140}
141
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800142TaskPtr
143Scheduler::scheduleOneTimeTask (SchedulerPtr scheduler, double delay,
144 const Task::Callback &callback, const Task::Tag &tag)
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800145{
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800146 TaskPtr task = make_shared<OneTimeTask> (callback, tag, scheduler, delay);
147 if (scheduler->addTask (task))
148 return task;
149 else
150 return TaskPtr ();
151}
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800152
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800153TaskPtr
154Scheduler::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
165bool
166Scheduler::addTask(TaskPtr newTask)
167{
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800168 if (addToMap(newTask))
169 {
170 newTask->reset();
171 int res = evtimer_add(newTask->ev(), newTask->tv());
172 if (res < 0)
173 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800174 _LOG_ERROR ("evtimer_add failed for " << newTask->tag());
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800175 }
176 return true;
177 }
178 else
179 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800180 _LOG_ERROR ("fail to add task: " << newTask->tag());
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800181 }
182
183 return false;
184}
185
Alexander Afanasyevfb4c43f2013-01-18 17:43:25 -0800186void
Alexander Afanasyev997ba632013-01-18 17:40:23 -0800187Scheduler::deleteTask(TaskPtr task)
188{
189 deleteTask (task->tag ());
190}
191
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800192void
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800193Scheduler::rescheduleTask(const TaskPtr &task)
194{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800195 ScopedLock lock(m_mutex);
Zhenkai Zhud2ca3922013-01-18 17:58:48 -0800196 TaskMapIt it = m_taskMap.find(task->tag());
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800197 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 Afanasyevfc720362013-01-24 21:49:48 -0800204 _LOG_ERROR ("evtimer_add failed for " << task->tag());
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800205 }
206 }
207 else
208 {
209 addTask(task);
210 }
211}
212
213void
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800214Scheduler::rescheduleTask(const Task::Tag &tag)
215{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800216 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800217 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
230bool
231Scheduler::addToMap(const TaskPtr &task)
232{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800233 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800234 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
242void
243Scheduler::deleteTask(const Task::Tag &tag)
244{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800245 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800246 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
255void
256Scheduler::deleteTask(const Task::TaskMatcher &matcher)
257{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800258 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800259 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
278int
279Scheduler::size()
280{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800281 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800282 return m_taskMap.size();
283}