-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.h
104 lines (83 loc) · 2.22 KB
/
Task.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2011 Alan Burlison, alan@bleaklow.com. All rights reserved.
* Use is subject to license terms.
*/
/*
* Simple task classes.
*/
#ifndef Task_h
#define Task_h
#include <stdint.h>
// Maximum time into the future - approximately 50 days.
#define MAX_TIME UINT32_MAX
/*
* A simple task.
*/
class Task {
public:
/*
* Can the task currently run?
* now - current time, in milliseconds.
*/
virtual bool canRun(uint32_t now) = 0;
/*
* Run the task,
* now - current time, in milliseconds.
*/
virtual void run(uint32_t now) = 0;
};
/*
* A task that is triggered by an external event.
*/
class TriggeredTask : public Task {
public:
/*
* Can the task currently run?
* now - current time, in milliseconds.
*/
virtual bool canRun(uint32_t now);
/*
* Mark the task as runnable.
*/
inline void setRunnable() { runFlag = true; }
/*
* Mark the task as non-runnable.
*/
inline void resetRunnable() { runFlag = false; }
protected:
bool runFlag; // True if the task is currently runnable.
};
/*
* A task that is run on a periodic basis.
*/
class TimedTask : public Task {
public:
/*
* Create a periodically executed task.
* when - the system clock tick when the task should run, in milliseconds.
*/
inline TimedTask(uint32_t when) { runTime = when; }
/*
* Can the task currently run?
* now - current system clock tick, in milliseconds.
*/
virtual bool canRun(uint32_t now);
/*
* Set the system clock tick when the task can next run.
* when - the system clock tick when the task should run, in milliseconds.
*/
inline void setRunTime(uint32_t when) { runTime = when; }
/*
* Increment the system clock tick when the task can next run.
* inc - system clock increment, in milliseconds.
*/
inline void incRunTime(uint32_t inc) { runTime += inc; }
/*
* Get the system clock tick when the task can next run.
* return - system clock tick when the task is next due to run.
*/
inline uint32_t getRunTime() { return runTime; }
protected:
uint32_t runTime; // The system clock tick when the task can next run.
};
#endif