blob: 096be983561b60cfee1a5a40fdd169e6098ce257 [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#ifndef SCHEDULER_H
23#define SCHEDULER_H
24
25#include <event2/event.h>
26#include <event2/thread.h>
27#include <event2/event-config.h>
28#include <event2/util.h>
29
30#include <boost/function.hpp>
31#include <boost/shared_ptr.hpp>
32
33#include <boost/exception/all.hpp>
Zhenkai Zhu563150d2013-01-25 21:06:52 -080034#include <boost/thread/recursive_mutex.hpp>
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080035#include <boost/thread/thread.hpp>
36#include <math.h>
37#include <map>
38#include <sys/time.h>
39
40#include "task.h"
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080041#include "interval-generator.h"
Zhenkai Zhu1888f742013-01-28 12:47:33 -080042#include "executor.h"
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080043
44class Scheduler;
45typedef boost::shared_ptr<Scheduler> SchedulerPtr;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080046
47/**
48 * @brief Scheduler class
49 */
50class Scheduler
51{
52public:
53 Scheduler();
54 virtual ~Scheduler();
55
56 // start event scheduling
57 virtual void
58 start();
59
60 // stop event scheduling
61 virtual void
62 shutdown();
63
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080064 // helper method to schedule one-time task
65 static TaskPtr
66 scheduleOneTimeTask (SchedulerPtr scheduler, double delay, const Task::Callback &callback, const Task::Tag &tag);
67
68 // helper method to schedule periodic task
69 static TaskPtr
70 schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator,
71 const Task::Callback &callback, const Task::Tag &tag);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080072
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080073 // if task with the same tag exists, the task is not added and return false
74 virtual bool
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080075 addTask(TaskPtr task, bool reset = true);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080076
Alexander Afanasyev997ba632013-01-18 17:40:23 -080077 // delete task by task->tag, regardless of whether it's invoked or not
78 virtual void
79 deleteTask(TaskPtr task);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080080
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080081 // delete task by tag, regardless of whether it's invoked or not
82 // if no task is found, no effect
83 virtual void
84 deleteTask(const Task::Tag &tag);
85
86 // delete tasks by matcher, regardless of whether it's invoked or not
87 // this is flexiable in that you can use any form of criteria in finding tasks to delete
88 // but keep in mind this is a linear scan
89
90 // if no task is found, no effect
91 virtual void
92 deleteTask(const Task::TaskMatcher &matcher);
93
94 // task must already have been added to the scheduler, otherwise this method has no effect
95 // this is usually used by PeriodicTask
96 virtual void
97 rescheduleTask(const Task::Tag &tag);
98
Zhenkai Zhu66ddb232013-01-18 17:53:52 -080099 // if the task is not pending, it will be added to the schedule queue
100 // if the task is pending, the delay is changed to the passed in delay
101 // e.g. if at second 0 task A with delay 5 is originally going to run at second 5 and
102 // rescheduleTask(A) is called at second 4, A will be reschedule to run
103 // at second 9
104 virtual void
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800105 rescheduleTask(TaskPtr task);
106
107 virtual void
108 rescheduleTaskAt (const Task::Tag &tag, double time);
109
110 virtual void
111 rescheduleTaskAt (TaskPtr task, double time);
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800112
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800113 void
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800114 execute(Executor::Job);
115
116 void
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800117 eventLoop();
118
119 event_base *
120 base() { return m_base; }
121
122 // used in test
123 int
124 size();
125
126protected:
127 bool
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800128 addToMap(TaskPtr task);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800129
130protected:
131 typedef std::map<Task::Tag, TaskPtr> TaskMap;
132 typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt;
Zhenkai Zhu563150d2013-01-25 21:06:52 -0800133 typedef boost::recursive_mutex Mutex;
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800134 typedef boost::unique_lock<Mutex> ScopedLock;
135
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800136 TaskMap m_taskMap;
137 Mutex m_mutex;
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800138 volatile bool m_running;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800139 event_base *m_base;
Zhenkai Zhub16be8f2013-01-25 16:12:30 -0800140 event *m_ev;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800141 boost::thread m_thread;
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800142 Executor m_executor;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800143};
144
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800145struct SchedulerException : virtual boost::exception, virtual std::exception { };
146
147#endif // SCHEDULER_H