blob: 18bd9bdcd615bfc73f92ba8e35cb37706234058d [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"
42
43class Scheduler;
44typedef boost::shared_ptr<Scheduler> SchedulerPtr;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080045
46/**
47 * @brief Scheduler class
48 */
49class Scheduler
50{
51public:
52 Scheduler();
53 virtual ~Scheduler();
54
55 // start event scheduling
56 virtual void
57 start();
58
59 // stop event scheduling
60 virtual void
61 shutdown();
62
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080063 // helper method to schedule one-time task
64 static TaskPtr
65 scheduleOneTimeTask (SchedulerPtr scheduler, double delay, const Task::Callback &callback, const Task::Tag &tag);
66
67 // helper method to schedule periodic task
68 static TaskPtr
69 schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator,
70 const Task::Callback &callback, const Task::Tag &tag);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080071
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080072 // if task with the same tag exists, the task is not added and return false
73 virtual bool
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080074 addTask(TaskPtr task, bool reset = true);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080075
Alexander Afanasyev997ba632013-01-18 17:40:23 -080076 // delete task by task->tag, regardless of whether it's invoked or not
77 virtual void
78 deleteTask(TaskPtr task);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080079
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080080 // delete task by tag, regardless of whether it's invoked or not
81 // if no task is found, no effect
82 virtual void
83 deleteTask(const Task::Tag &tag);
84
85 // delete tasks by matcher, regardless of whether it's invoked or not
86 // this is flexiable in that you can use any form of criteria in finding tasks to delete
87 // but keep in mind this is a linear scan
88
89 // if no task is found, no effect
90 virtual void
91 deleteTask(const Task::TaskMatcher &matcher);
92
93 // task must already have been added to the scheduler, otherwise this method has no effect
94 // this is usually used by PeriodicTask
95 virtual void
96 rescheduleTask(const Task::Tag &tag);
97
Zhenkai Zhu66ddb232013-01-18 17:53:52 -080098 // if the task is not pending, it will be added to the schedule queue
99 // if the task is pending, the delay is changed to the passed in delay
100 // e.g. if at second 0 task A with delay 5 is originally going to run at second 5 and
101 // rescheduleTask(A) is called at second 4, A will be reschedule to run
102 // at second 9
103 virtual void
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800104 rescheduleTask(TaskPtr task);
105
106 virtual void
107 rescheduleTaskAt (const Task::Tag &tag, double time);
108
109 virtual void
110 rescheduleTaskAt (TaskPtr task, double time);
Zhenkai Zhu66ddb232013-01-18 17:53:52 -0800111
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800112 void
113 eventLoop();
114
115 event_base *
116 base() { return m_base; }
117
118 // used in test
119 int
120 size();
121
122protected:
123 bool
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800124 addToMap(TaskPtr task);
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800125
126protected:
127 typedef std::map<Task::Tag, TaskPtr> TaskMap;
128 typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt;
Zhenkai Zhu563150d2013-01-25 21:06:52 -0800129 typedef boost::recursive_mutex Mutex;
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800130 typedef boost::unique_lock<Mutex> ScopedLock;
131
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800132 TaskMap m_taskMap;
133 Mutex m_mutex;
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800134 volatile bool m_running;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800135 event_base *m_base;
Zhenkai Zhub16be8f2013-01-25 16:12:30 -0800136 event *m_ev;
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800137 boost::thread m_thread;
138};
139
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -0800140struct SchedulerException : virtual boost::exception, virtual std::exception { };
141
142#endif // SCHEDULER_H