blob: ffac5d25596307fb1ed4aab5efb902e690c01cda [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 * Zhenkai Zhu
6 *
7 * BSD license, See the LICENSE file for more information
8 *
9 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
10 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
11 */
12
13#ifndef SCHEDULER_H
14#define SCHEDULER_H
15
16#include <event2/event.h>
17#include <event2/thread.h>
18#include <event2/event-config.h>
19#include <event2/util.h>
20
21#include <boost/function.hpp>
22#include <boost/shared_ptr.hpp>
23
24#include <boost/exception/all.hpp>
25#include <boost/thread/recursive_mutex.hpp>
26#include <boost/thread/thread.hpp>
27#include <math.h>
28#include <map>
29#include <sys/time.h>
30
31#include "scheduler/task.h"
32#include "scheduler/interval-generator.h"
33#include "executor/executor.h"
34
35class Scheduler;
36typedef boost::shared_ptr<Scheduler> SchedulerPtr;
37
38/**
39 * @brief Scheduler class
40 */
41class Scheduler
42{
43public:
44 Scheduler();
45 virtual ~Scheduler();
46
47 // start event scheduling
48 virtual void
49 start();
50
51 // stop event scheduling
52 virtual void
53 shutdown();
54
55 // helper method to schedule one-time task
56 static TaskPtr
57 scheduleOneTimeTask (SchedulerPtr scheduler, double delay, const Task::Callback &callback, const Task::Tag &tag);
58
59 // helper method to schedule periodic task
60 static TaskPtr
61 schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator,
62 const Task::Callback &callback, const Task::Tag &tag);
63
64 // if task with the same tag exists, the task is not added and return false
65 virtual bool
66 addTask(TaskPtr task, bool reset = true);
67
68 // delete task by task->tag, regardless of whether it's invoked or not
69 virtual void
70 deleteTask(TaskPtr task);
71
72 // delete task by tag, regardless of whether it's invoked or not
73 // if no task is found, no effect
74 virtual void
75 deleteTask(const Task::Tag &tag);
76
77 // delete tasks by matcher, regardless of whether it's invoked or not
78 // this is flexiable in that you can use any form of criteria in finding tasks to delete
79 // but keep in mind this is a linear scan
80
81 // if no task is found, no effect
82 virtual void
83 deleteTask(const Task::TaskMatcher &matcher);
84
85 // task must already have been added to the scheduler, otherwise this method has no effect
86 // this is usually used by PeriodicTask
87 virtual void
88 rescheduleTask(const Task::Tag &tag);
89
90 // if the task is not pending, it will be added to the schedule queue
91 // if the task is pending, the delay is changed to the passed in delay
92 // e.g. if at second 0 task A with delay 5 is originally going to run at second 5 and
93 // rescheduleTask(A) is called at second 4, A will be reschedule to run
94 // at second 9
95 virtual void
96 rescheduleTask(TaskPtr task);
97
98 virtual void
99 rescheduleTaskAt (const Task::Tag &tag, double time);
100
101 virtual void
102 rescheduleTaskAt (TaskPtr task, double time);
103
104 void
105 execute(Executor::Job);
106
107 void
108 eventLoop();
109
110 event_base *
111 base() { return m_base; }
112
113 // used in test
114 int
115 size();
116
117protected:
118 bool
119 addToMap(TaskPtr task);
120
121protected:
122 typedef std::map<Task::Tag, TaskPtr> TaskMap;
123 typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt;
124 typedef boost::recursive_mutex Mutex;
125 typedef boost::unique_lock<Mutex> ScopedLock;
126
127 TaskMap m_taskMap;
128 Mutex m_mutex;
129 volatile bool m_running;
130 event_base *m_base;
131 event *m_ev;
132 boost::thread m_thread;
133 Executor m_executor;
134};
135
136struct SchedulerException : virtual boost::exception, virtual std::exception { };
137
138#endif // SCHEDULER_H