-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.h
executable file
·177 lines (149 loc) · 4.14 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Task.h
*
* Threaded object representing a task for the scheduling
* test fixture. Tasks are composed their name, compute time,
* period, and deadline.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/
#ifndef TASK_H_
#define TASK_H_
#include <string>
#include <semaphore.h>
#include "Thread.h"
#include "SchedulingAlgorithms.h"
// The maximum number of misses which a task can endure when
// used with a net deadline met/deadline missed counter.
#define MAX_DEADLINE_MISSES (15)
/**
* Enumeration for the set of task priorities and task states.
* Task priorities are specified to be the valid QNX priority
* for which they will run at.
*/
enum TaskPriority {
TP_FINISHED = 1, TP_READY = 7, TP_RUNNING = 8, TP_SCHEDULER = 9
};
class Task : public Thread {
public:
/**
* @param name - user-specified name of the task.
* @param computeTime - user-specified time which the task busy-waits for.
* @param period - user-specified interval of when the task must run.
* @param deadline - user-specified interval of when the task must finish.
* @param scheduler - pointer to the schedler's semaphore.
*/
Task(std::string name, int computeTime, int period, int deadline, sem_t* scheduler);
virtual ~Task();
/**
* Kicks off the timer which runs repeatedly at the deadline interval.
*/
void startDeadlineTimer();
/**
* Thread's run method which waits for the deadline to pass,
* performs a blocking busy-wait using nanospin, and
* notifies the scheduler that it has completed its work.
*/
void* run();
/**
* Returns the compute time of the task.
*
* @return an int - the compute time for the task when no work has been
* done
*/
int getComputeTime();
/**
* @return The time remaining before the task's deadline, timesliced
* to the same value for which a single time tick is.
*/
long getRemainingDeadline();
/**
* Increments the amount of time completed.
*/
void incrementCompletedTime();
/**
* @return the number of time ticks remaining for task computation.
*/
int getRemainingComputeTime();
/**
* @return the period of the task.
*/
int getPeriod();
/**
* @return the deadline of the task.
*/
int getDeadline();
/**
* @return the task's state as a TaskPriority enum value.
*/
int getState();
/**
* Sets the state as a TaskPriority enum value.
*
* @param newState - the state this task will be in.
*/
void setState(int newState);
/**
* Called when the deadline has passed to signal the task it can perform
* work again.
*/
void postSem();
private:
/**
* Called by the timer to signify the deadline has passed.
*
* @param sig - contains a pointer to this Task.
*/
static void tick(union sigval sig);
/**
* The time it takes to actually complete the task in
* our specified timeslice.
*/
int computeTime;
/**
* The time computed in the current execution cycle of the task
* in our specified timeslice.
*/
volatile int completedTime;
/**
* The period of the task; how frequently it restarts in our
* specified timeslice.
*/
int period;
/**
* The deadline the task must finish by in our specified timeslice.
*/
int deadline;
/**
* Semaphore to wait on before executing work; this synchronizes the task
* and prevents it from doing work before its period expires.
*/
sem_t doWork;
/**
* The state of the task; ready, running, finished. Uses the priority enum
*/
int state;
/**
* The timer which the task uses to restart its computation.
*/
timer_t deadlineTimer;
/**
* Pointer to the semaphore the scheduler synchronizes on.
*/
sem_t* scheduler;
/**
* Cached values to decrease the latency in scheduling the timer.
*/
struct itimerspec deadlineTimerSpec;
struct sigevent deadlineEvent;
struct itimerspec deadlineRemainingSpec;
/**
* Cached strings to decrease the latency of the trace event logging.
*/
std::string deadlineTimerString;
std::string deadlineMissedString;
const char *deadlineTimerMsg;
const char *deadlineMissedMsg;
};
#endif /* TASK_H_ */