blob: f6f1fe8b9e21a9acffc8fb184c88c2786b937342 [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
63 // delete task by tag, regardless of whether it's invoked or not
64 // if no task is found, no effect
65 virtual void
66 deleteTask(const Task::Tag &tag);
67
68 // delete tasks by matcher, regardless of whether it's invoked or not
69 // this is flexiable in that you can use any form of criteria in finding tasks to delete
70 // but keep in mind this is a linear scan
71
72 // if no task is found, no effect
73 virtual void
74 deleteTask(const Task::TaskMatcher &matcher);
75
76 // task must already have been added to the scheduler, otherwise this method has no effect
77 // this is usually used by PeriodicTask
78 virtual void
79 rescheduleTask(const Task::Tag &tag);
80
81 void
82 eventLoop();
83
84 event_base *
85 base() { return m_base; }
86
87 // used in test
88 int
89 size();
90
91protected:
92 bool
93 addToMap(const TaskPtr &task);
94
95protected:
96 typedef std::map<Task::Tag, TaskPtr> TaskMap;
97 typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt;
98 typedef boost::shared_mutex Mutex;
99 typedef boost::unique_lock<Mutex> WriteLock;
100 typedef boost::shared_lock<Mutex> ReadLock;
101 TaskMap m_taskMap;
102 Mutex m_mutex;
103 bool m_running;
104 event_base *m_base;
105 boost::thread m_thread;
106};
107
108class Scheduler;
109typedef boost::shared_ptr<Scheduler> SchedulerPtr;
110
111struct SchedulerException : virtual boost::exception, virtual std::exception { };
112
113#endif // SCHEDULER_H