blob: a52649a5af9405052ba4cf29b0525c1308f767a2 [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
Yingdi Yu57f667b2013-07-11 10:37:59 -070073 // helper method to schedule/reschedule one-time task
74 static TaskPtr
75 scheduleDelayOneTimeTask (SchedulerPtr scheduler, double delay, const Task::Callback &callback, const Task::Tag &tag);
76
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080077 // if task with the same tag exists, the task is not added and return false
78 virtual bool
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080079 addTask(TaskPtr task, bool reset = true);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080080
Alexander Afanasyev997ba632013-01-18 17:40:23 -080081 // delete task by task->tag, regardless of whether it's invoked or not
82 virtual void
83 deleteTask(TaskPtr task);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080084
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080085 // delete task by tag, regardless of whether it's invoked or not
86 // if no task is found, no effect
87 virtual void
88 deleteTask(const Task::Tag &tag);
89
90 // delete tasks by matcher, regardless of whether it's invoked or not
91 // this is flexiable in that you can use any form of criteria in finding tasks to delete
92 // but keep in mind this is a linear scan
93
94 // if no task is found, no effect
95 virtual void
96 deleteTask(const Task::TaskMatcher &matcher);
97
98 // task must already have been added to the scheduler, otherwise this method has no effect
99 // this is usually used by PeriodicTask
100 virtual void
101 rescheduleTask(const Task::Tag &tag);
102
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800103 // if the task is not pending, it will be added to the schedule queue
104 // if the task is pending, the delay is changed to the passed in delay
105 // e.g. if at second 0 task A with delay 5 is originally going to run at second 5 and
106 // rescheduleTask(A) is called at second 4, A will be reschedule to run
107 // at second 9
108 virtual void
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800109 rescheduleTask(TaskPtr task);
110
111 virtual void
112 rescheduleTaskAt (const Task::Tag &tag, double time);
113
114 virtual void
115 rescheduleTaskAt (TaskPtr task, double time);
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800116
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800117 void
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800118 execute(Executor::Job);
119
120 void
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800121 eventLoop();
122
123 event_base *
124 base() { return m_base; }
125
126 // used in test
127 int
128 size();
129
130protected:
131 bool
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800132 addToMap(TaskPtr task);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800133
134protected:
135 typedef std::map<Task::Tag, TaskPtr> TaskMap;
136 typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt;
Zhenkai Zhu563150d2013-01-25 21:06:52 -0800137 typedef boost::recursive_mutex Mutex;
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800138 typedef boost::unique_lock<Mutex> ScopedLock;
139
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800140 TaskMap m_taskMap;
141 Mutex m_mutex;
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800142 volatile bool m_running;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800143 event_base *m_base;
Zhenkai Zhub16be8f2013-01-25 16:12:30 -0800144 event *m_ev;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800145 boost::thread m_thread;
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800146 Executor m_executor;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800147};
148
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800149struct SchedulerException : virtual boost::exception, virtual std::exception { };
150
151#endif // SCHEDULER_H