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> |
| 34 | #include <boost/thread/shared_mutex.hpp> |
| 35 | #include <boost/thread/thread.hpp> |
| 36 | #include <math.h> |
| 37 | #include <map> |
| 38 | #include <sys/time.h> |
| 39 | |
| 40 | #include "task.h" |
| 41 | |
| 42 | /** |
| 43 | * @brief Scheduler class |
| 44 | */ |
| 45 | class Scheduler |
| 46 | { |
| 47 | public: |
| 48 | Scheduler(); |
| 49 | virtual ~Scheduler(); |
| 50 | |
| 51 | // start event scheduling |
| 52 | virtual void |
| 53 | start(); |
| 54 | |
| 55 | // stop event scheduling |
| 56 | virtual void |
| 57 | shutdown(); |
| 58 | |
| 59 | // if task with the same tag exists, the task is not added and return false |
| 60 | virtual bool |
| 61 | addTask(const TaskPtr &task); |
| 62 | |
Alexander Afanasyev | 997ba63 | 2013-01-18 17:40:23 -0800 | [diff] [blame^] | 63 | // delete task by task->tag, regardless of whether it's invoked or not |
| 64 | virtual void |
| 65 | deleteTask(TaskPtr task); |
| 66 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 67 | // delete task by tag, regardless of whether it's invoked or not |
| 68 | // if no task is found, no effect |
| 69 | virtual void |
| 70 | deleteTask(const Task::Tag &tag); |
| 71 | |
| 72 | // delete tasks by matcher, regardless of whether it's invoked or not |
| 73 | // this is flexiable in that you can use any form of criteria in finding tasks to delete |
| 74 | // but keep in mind this is a linear scan |
| 75 | |
| 76 | // if no task is found, no effect |
| 77 | virtual void |
| 78 | deleteTask(const Task::TaskMatcher &matcher); |
| 79 | |
| 80 | // task must already have been added to the scheduler, otherwise this method has no effect |
| 81 | // this is usually used by PeriodicTask |
| 82 | virtual void |
| 83 | rescheduleTask(const Task::Tag &tag); |
| 84 | |
| 85 | void |
| 86 | eventLoop(); |
| 87 | |
| 88 | event_base * |
| 89 | base() { return m_base; } |
| 90 | |
| 91 | // used in test |
| 92 | int |
| 93 | size(); |
| 94 | |
| 95 | protected: |
| 96 | bool |
| 97 | addToMap(const TaskPtr &task); |
| 98 | |
| 99 | protected: |
| 100 | typedef std::map<Task::Tag, TaskPtr> TaskMap; |
| 101 | typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt; |
| 102 | typedef boost::shared_mutex Mutex; |
| 103 | typedef boost::unique_lock<Mutex> WriteLock; |
| 104 | typedef boost::shared_lock<Mutex> ReadLock; |
| 105 | TaskMap m_taskMap; |
| 106 | Mutex m_mutex; |
| 107 | bool m_running; |
| 108 | event_base *m_base; |
| 109 | boost::thread m_thread; |
| 110 | }; |
| 111 | |
| 112 | class Scheduler; |
| 113 | typedef boost::shared_ptr<Scheduler> SchedulerPtr; |
| 114 | |
| 115 | struct SchedulerException : virtual boost::exception, virtual std::exception { }; |
| 116 | |
| 117 | #endif // SCHEDULER_H |