Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 1 | /* -*- 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 Zhu | 563150d | 2013-01-25 21:06:52 -0800 | [diff] [blame] | 34 | #include <boost/thread/recursive_mutex.hpp> |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 35 | #include <boost/thread/thread.hpp> |
| 36 | #include <math.h> |
| 37 | #include <map> |
| 38 | #include <sys/time.h> |
| 39 | |
| 40 | #include "task.h" |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 41 | #include "interval-generator.h" |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 42 | #include "executor.h" |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 43 | |
| 44 | class Scheduler; |
| 45 | typedef boost::shared_ptr<Scheduler> SchedulerPtr; |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * @brief Scheduler class |
| 49 | */ |
| 50 | class Scheduler |
| 51 | { |
| 52 | public: |
| 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 Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 64 | // 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 Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 73 | // if task with the same tag exists, the task is not added and return false |
| 74 | virtual bool |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 75 | addTask(TaskPtr task, bool reset = true); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | 997ba63 | 2013-01-18 17:40:23 -0800 | [diff] [blame] | 77 | // delete task by task->tag, regardless of whether it's invoked or not |
| 78 | virtual void |
| 79 | deleteTask(TaskPtr task); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 81 | // 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 Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 99 | // 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 Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 105 | 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 Zhu | 66ddb23 | 2013-01-18 17:53:52 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 113 | void |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 114 | execute(Executor::Job); |
| 115 | |
| 116 | void |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 117 | eventLoop(); |
| 118 | |
| 119 | event_base * |
| 120 | base() { return m_base; } |
| 121 | |
| 122 | // used in test |
| 123 | int |
| 124 | size(); |
| 125 | |
| 126 | protected: |
| 127 | bool |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 128 | addToMap(TaskPtr task); |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 129 | |
| 130 | protected: |
| 131 | typedef std::map<Task::Tag, TaskPtr> TaskMap; |
| 132 | typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt; |
Zhenkai Zhu | 563150d | 2013-01-25 21:06:52 -0800 | [diff] [blame] | 133 | typedef boost::recursive_mutex Mutex; |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 134 | typedef boost::unique_lock<Mutex> ScopedLock; |
| 135 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 136 | TaskMap m_taskMap; |
| 137 | Mutex m_mutex; |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 138 | volatile bool m_running; |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 139 | event_base *m_base; |
Zhenkai Zhu | b16be8f | 2013-01-25 16:12:30 -0800 | [diff] [blame] | 140 | event *m_ev; |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 141 | boost::thread m_thread; |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 142 | Executor m_executor; |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 143 | }; |
| 144 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 145 | struct SchedulerException : virtual boost::exception, virtual std::exception { }; |
| 146 | |
| 147 | #endif // SCHEDULER_H |