blob: 83b9926082b353687ac308413159e99eb39f4c61 [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>
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 */
45class Scheduler
46{
47public:
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 Afanasyev997ba632013-01-18 17:40:23 -080063 // delete task by task->tag, regardless of whether it's invoked or not
64 virtual void
65 deleteTask(TaskPtr task);
66
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080067 // 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
Zhenkai Zhu66ddb232013-01-18 17:53:52 -080085 // if the task is not pending, it will be added to the schedule queue
86 // if the task is pending, the delay is changed to the passed in delay
87 // e.g. if at second 0 task A with delay 5 is originally going to run at second 5 and
88 // rescheduleTask(A) is called at second 4, A will be reschedule to run
89 // at second 9
90 virtual void
91 rescheduleTask(const TaskPtr &task);
92
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080093 void
94 eventLoop();
95
96 event_base *
97 base() { return m_base; }
98
99 // used in test
100 int
101 size();
102
103protected:
104 bool
105 addToMap(const TaskPtr &task);
106
107protected:
108 typedef std::map<Task::Tag, TaskPtr> TaskMap;
109 typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt;
110 typedef boost::shared_mutex Mutex;
111 typedef boost::unique_lock<Mutex> WriteLock;
112 typedef boost::shared_lock<Mutex> ReadLock;
113 TaskMap m_taskMap;
114 Mutex m_mutex;
115 bool m_running;
116 event_base *m_base;
117 boost::thread m_thread;
118};
119
120class Scheduler;
121typedef boost::shared_ptr<Scheduler> SchedulerPtr;
122
123struct SchedulerException : virtual boost::exception, virtual std::exception { };
124
125#endif // SCHEDULER_H