blob: 02b6431cebd703e6c958f61899f1f73fdb8a105a [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 }
Zhenkai Zhu238db852013-01-25 16:27:28 -0800108
109 // just to prevent craziness in CPU usage which supposedly should not happen
110 // after adding the dummy event
111 usleep(1000);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800112 }
113}
114
115void
116Scheduler::start()
117{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800118 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800119 if (!m_running)
120 {
121 m_thread = boost::thread(&Scheduler::eventLoop, this);
122 m_running = true;
123 }
124}
125
126void
127Scheduler::shutdown()
128{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800129 bool breakAndWait = false;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800130 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800131 ScopedLock lock (m_mutex);
132 if (m_running)
133 {
134 m_running = false;
135 breakAndWait = true;
136 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800137 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800138
139 if (breakAndWait)
140 {
141 event_base_loopbreak(m_base);
142 m_thread.join();
143 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800144}
145
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800146TaskPtr
147Scheduler::scheduleOneTimeTask (SchedulerPtr scheduler, double delay,
148 const Task::Callback &callback, const Task::Tag &tag)
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800149{
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800150 TaskPtr task = make_shared<OneTimeTask> (callback, tag, scheduler, delay);
151 if (scheduler->addTask (task))
152 return task;
153 else
154 return TaskPtr ();
155}
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800156
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800157TaskPtr
158Scheduler::schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator,
159 const Task::Callback &callback, const Task::Tag &tag)
160{
161 TaskPtr task = make_shared<PeriodicTask> (callback, tag, scheduler, delayGenerator);
162
163 if (scheduler->addTask (task))
164 return task;
165 else
166 return TaskPtr ();
167}
168
169bool
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800170Scheduler::addTask(TaskPtr newTask, bool reset/* = true*/)
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800171{
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800172 if (addToMap(newTask))
173 {
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800174 if (reset)
175 {
176 newTask->reset();
177 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800178 int res = evtimer_add(newTask->ev(), newTask->tv());
179 if (res < 0)
180 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800181 _LOG_ERROR ("evtimer_add failed for " << newTask->tag());
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800182 }
183 return true;
184 }
185 else
186 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800187 _LOG_ERROR ("fail to add task: " << newTask->tag());
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800188 }
189
190 return false;
191}
192
Alexander Afanasyevfb4c43f2013-01-18 17:43:25 -0800193void
Alexander Afanasyev997ba632013-01-18 17:40:23 -0800194Scheduler::deleteTask(TaskPtr task)
195{
196 deleteTask (task->tag ());
197}
198
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800199void
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800200Scheduler::rescheduleTask(TaskPtr task)
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800201{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800202 ScopedLock lock(m_mutex);
Zhenkai Zhud2ca3922013-01-18 17:58:48 -0800203 TaskMapIt it = m_taskMap.find(task->tag());
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800204 if (it != m_taskMap.end())
205 {
206 TaskPtr task = it->second;
207 task->reset();
208 int res = evtimer_add(task->ev(), task->tv());
209 if (res < 0)
210 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800211 _LOG_ERROR ("evtimer_add failed for " << task->tag());
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800212 }
213 }
214 else
215 {
216 addTask(task);
217 }
218}
219
220void
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800221Scheduler::rescheduleTask(const Task::Tag &tag)
222{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800223 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800224 TaskMapIt it = m_taskMap.find(tag);
225 if (it != m_taskMap.end())
226 {
227 TaskPtr task = it->second;
228 task->reset();
229 int res = evtimer_add(task->ev(), task->tv());
230 if (res < 0)
231 {
232 cout << "evtimer_add failed for " << task->tag() << endl;
233 }
234 }
235}
236
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800237void
238Scheduler::rescheduleTaskAt (const Task::Tag &tag, double time)
239{
240 ScopedLock lock(m_mutex);
241 TaskMapIt it = m_taskMap.find (tag);
242 if (it != m_taskMap.end())
243 {
244 TaskPtr task = it->second;
245 task->reset();
246 task->setTv (time);
247
248 int res = evtimer_add(task->ev(), task->tv());
249 if (res < 0)
250 {
251 _LOG_ERROR ("evtimer_add failed for " << task->tag());
252 }
253 }
254 else
255 {
256 _LOG_ERROR ("Task for tag " << tag << " not found");
257 }
258}
259
260void
261Scheduler::rescheduleTaskAt (TaskPtr task, double time)
262{
263 ScopedLock lock(m_mutex);
264 TaskMapIt it = m_taskMap.find(task->tag());
265 if (it != m_taskMap.end())
266 {
267 TaskPtr task = it->second;
268 task->reset();
269 task->setTv (time);
270
271 int res = evtimer_add(task->ev(), task->tv());
272 if (res < 0)
273 {
274 _LOG_ERROR ("evtimer_add failed for " << task->tag());
275 }
276 }
277 else
278 {
279 task->setTv (time); // force different time
280 addTask (task, false);
281 }
282}
283
284
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800285bool
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800286Scheduler::addToMap(TaskPtr task)
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800287{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800288 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800289 if (m_taskMap.find(task->tag()) == m_taskMap.end())
290 {
291 m_taskMap.insert(make_pair(task->tag(), task));
292 return true;
293 }
294 return false;
295}
296
297void
298Scheduler::deleteTask(const Task::Tag &tag)
299{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800300 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800301 TaskMapIt it = m_taskMap.find(tag);
302 if (it != m_taskMap.end())
303 {
304 TaskPtr task = it->second;
305 evtimer_del(task->ev());
306 m_taskMap.erase(it);
307 }
308}
309
310void
311Scheduler::deleteTask(const Task::TaskMatcher &matcher)
312{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800313 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800314 TaskMapIt it = m_taskMap.begin();
315 while(it != m_taskMap.end())
316 {
317 TaskPtr task = it->second;
318 if (matcher(task))
319 {
320 evtimer_del(task->ev());
321 // Use post increment; map.erase invalidate the iterator that is beening erased,
322 // but does not invalidate other iterators. This seems to be the convention to
323 // erase something from C++ STL map while traversing.
324 m_taskMap.erase(it++);
325 }
326 else
327 {
328 ++it;
329 }
330 }
331}
332
333int
334Scheduler::size()
335{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800336 ScopedLock lock(m_mutex);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800337 return m_taskMap.size();
338}