blob: e7630d237736e98f5ed7a698022486915229a0e9 [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"
23#include <utility>
24
25using namespace std;
26
27#define EVLOOP_NO_EXIT_ON_EMPTY 0x04
28
29// IntervalGeneratorPtr
30// IntervalGenerator:: Null;
31
32void errorCallback(int err)
33{
34 cout << "Fatal error: " << err << endl;
35}
36
37Scheduler::Scheduler()
38 : m_running(false)
39{
40 event_set_fatal_callback(errorCallback);
41 evthread_use_pthreads();
42 m_base = event_base_new();
43}
44
45Scheduler::~Scheduler()
46{
47 event_base_free(m_base);
48}
49
50void
51Scheduler::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 Afanasyev34edd4d2013-01-17 17:55:45 -080059
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080060 {
Alexander Afanasyev34edd4d2013-01-17 17:55:45 -080061 ReadLock lock(m_mutex);
62 if (!m_running)
63 {
64 cout << "scheduler loop break normal" << endl;
65 break;
66 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080067 }
68 }
69}
70
71void
72Scheduler::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
82void
83Scheduler::shutdown()
84{
Zhenkai Zhue5466812013-01-17 18:28:32 -080085 WriteLock lock(m_mutex);
86 if (m_running)
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080087 {
Zhenkai Zhue5466812013-01-17 18:28:32 -080088 event_base_loopbreak(m_base);
89 m_thread.join();
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080090 m_running = false;
91 }
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080092}
93
94bool
95Scheduler::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
117void
118Scheduler::rescheduleTask(const Task::Tag &tag)
119{
120 ReadLock lock(m_mutex);
121 TaskMapIt it = m_taskMap.find(tag);
122 if (it != m_taskMap.end())
123 {
124 TaskPtr task = it->second;
125 task->reset();
126 int res = evtimer_add(task->ev(), task->tv());
127 if (res < 0)
128 {
129 cout << "evtimer_add failed for " << task->tag() << endl;
130 }
131 }
132}
133
134bool
135Scheduler::addToMap(const TaskPtr &task)
136{
137 WriteLock lock(m_mutex);
138 if (m_taskMap.find(task->tag()) == m_taskMap.end())
139 {
140 m_taskMap.insert(make_pair(task->tag(), task));
141 return true;
142 }
143 return false;
144}
145
146void
147Scheduler::deleteTask(const Task::Tag &tag)
148{
149 WriteLock lock(m_mutex);
150 TaskMapIt it = m_taskMap.find(tag);
151 if (it != m_taskMap.end())
152 {
153 TaskPtr task = it->second;
154 evtimer_del(task->ev());
155 m_taskMap.erase(it);
156 }
157}
158
159void
160Scheduler::deleteTask(const Task::TaskMatcher &matcher)
161{
162 WriteLock lock(m_mutex);
163 TaskMapIt it = m_taskMap.begin();
164 while(it != m_taskMap.end())
165 {
166 TaskPtr task = it->second;
167 if (matcher(task))
168 {
169 evtimer_del(task->ev());
170 // Use post increment; map.erase invalidate the iterator that is beening erased,
171 // but does not invalidate other iterators. This seems to be the convention to
172 // erase something from C++ STL map while traversing.
173 m_taskMap.erase(it++);
174 }
175 else
176 {
177 ++it;
178 }
179 }
180}
181
182int
183Scheduler::size()
184{
185 ReadLock lock(m_mutex);
186 return m_taskMap.size();
187}