blob: 11a9fa6dafa5ffdf655834e12a9b9c2021d9c3e6 [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 TASK_H
23#define TASK_H
24
25#define _OVERRIDE
26#ifdef __GNUC__
27#if __GNUC_MAJOR >= 4 && __GNUC_MINOR__ >= 7
28 #undef _OVERRIDE
29 #define _OVERRIDE override
30#endif // __GNUC__ version
31#endif // __GNUC__
32
33#include <boost/function.hpp>
34#include <boost/shared_ptr.hpp>
Zhenkai Zhud4a8b122013-01-29 15:40:39 -080035#include <boost/enable_shared_from_this.hpp>
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080036#include <sys/time.h>
37
38//////////////////////////////////////////////////
39// forward declarations
40class Task;
41typedef boost::shared_ptr<Task> TaskPtr;
42
43class Scheduler;
44typedef boost::shared_ptr<Scheduler> SchedulerPtr;
45
46struct event;
47//////////////////////////////////////////////////
48
49
50/**
51 * @brief Base class for a task
52 */
Zhenkai Zhud4a8b122013-01-29 15:40:39 -080053class Task : public boost::enable_shared_from_this<Task>
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080054{
55public:
56 // callback of this task
57 typedef boost::function<void ()> Callback;
58 // tag identifies this task, should be unique
59 typedef std::string Tag;
60 // used to match tasks
61 typedef boost::function<bool (const TaskPtr &task)> TaskMatcher;
62
63 // Task is associated with Schedulers due to the requirement that libevent event is associated with an libevent event_base
64 Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler);
65 virtual ~Task();
66
67 virtual void
68 run() = 0;
69
70 Tag
71 tag() { return m_tag; }
72
73 event *
74 ev() { return m_event; }
75
76 timeval *
77 tv() { return m_tv; }
78
79 // Task needs to be resetted after the callback is invoked if it is to be schedule again; just for safety
80 // it's called by scheduler automatically when addTask or rescheduleTask is called;
81 // Tasks should do preparation work here (e.g. set up new delay, etc. )
82 virtual void
83 reset() = 0;
84
85 // set delay
86 // This overrides whatever delay kept in m_tv
87 void
88 setTv(double delay);
89
Zhenkai Zhu1888f742013-01-28 12:47:33 -080090 void
91 execute();
92
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080093protected:
94 Callback m_callback;
95 Tag m_tag;
96 SchedulerPtr m_scheduler;
97 bool m_invoked;
98 event *m_event;
99 timeval *m_tv;
100};
101
102
103#endif // TASK_H