-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProcess.cpp
More file actions
executable file
·205 lines (160 loc) · 4.53 KB
/
Process.cpp
File metadata and controls
executable file
·205 lines (160 loc) · 4.53 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "Process.h"
#include "Scheduler.h"
/*********** PUBLIC *************/
Process::Process(Scheduler &scheduler, ProcPriority priority, uint32_t period,
int iterations, uint16_t overSchedThresh)
: _scheduler(scheduler), _pLevel(priority)
{
this->_enabled = false;
this->_period = period;
this->_iterations = iterations;
this->_force = false;
this->_overSchedThresh = overSchedThresh;
resetTimeStamps();
#ifdef _PROCESS_TIMEOUT_INTERRUPTS
setTimeout(PROCESS_NO_TIMEOUT);
#endif
}
void Process::resetTimeStamps()
{
ATOMIC_START
{
this->_scheduledTS = _scheduler.getCurrTS();
this->_actualTS = _scheduler.getCurrTS();
this->_pBehind = 0;
}
ATOMIC_END
}
bool Process::disable()
{
return _scheduler.disable(*this);
}
bool Process::enable()
{
return _scheduler.enable(*this);
}
bool Process::destroy()
{
return _scheduler.destroy(*this);
}
bool Process::add(bool enableIfNot)
{
return _scheduler.add(*this, enableIfNot);
}
bool Process::restart()
{
return _scheduler.restart(*this);
}
bool Process::needsServicing(uint32_t start)
{
return (isEnabled() &&
(_force ||
((getPeriod() == SERVICE_CONSTANTLY || timeToNextRun(start) <= 0) &&
(getIterations() == RUNTIME_FOREVER || getIterations() > 0))));
}
void Process::setIterations(int iterations)
{
ATOMIC_START
{
_iterations = iterations;
}
ATOMIC_END
}
void Process::setPeriod(uint32_t period)
{
ATOMIC_START
{
_period = period;
}
ATOMIC_END
}
// both must need servicing
Process *Process::runWhich(Process *p1, Process *p2)
{
// All things being equal pick yes
// Compare forces
if (p1->forceSet() || p2->forceSet())
return p1->forceSet() ? p1 : p2;
// whichever one is more behind goes first
return (p1->timeToNextRun() <= p2->timeToNextRun()) ? p1 : p2;
}
/*********** PROTECTED *************/
// Fired on creation/destroy
void Process::setup() { return; }
void Process::cleanup() { return; }
//called on enable/disable
void Process::onEnable() { return; }
void Process::onDisable() { return; }
void Process::handleWarning(ProcessWarning warning)
{
if (warning == WARNING_PROC_OVERSCHEDULED)
resetOverSchedWarning();
#ifdef _PROCESS_TIMEOUT_INTERRUPTS
else if (warning == ERROR_PROC_TIMED_OUT)
restart(); // default by restarting it
#endif
}
/*********** PRIVATE *************/
bool Process::isPBehind(uint32_t curr)
{
return (curr - getScheduledTS()) >= getPeriod();
}
void Process::willService(uint32_t now)
{
if (!_force)
{
if (getPeriod() != SERVICE_CONSTANTLY)
setScheduledTS(getScheduledTS() + getPeriod());
else
setScheduledTS(now);
} else {
_force = false;
}
setActualTS(now);
// Handle scheduler warning
if (getOverSchedThresh() != OVERSCHEDULED_NO_WARNING && isPBehind(now)) {
incrPBehind();
if (getCurrPBehind() >= getOverSchedThresh())
handleWarning(WARNING_PROC_OVERSCHEDULED);
} else {
resetOverSchedWarning();
}
}
// Return true if last if should disable
bool Process::wasServiced(bool wasForced)
{
if (!wasForced && getIterations() > 0) { //Was an iteration
decIterations();
if (getIterations() == 0)
return true;
}
return false;
}
#ifdef _PROCESS_TIMEOUT_INTERRUPTS
void Process::setTimeout(uint32_t timeout)
{
// Can not let interrupt happen
ATOMIC_START
{
_timeout = timeout;
}
ATOMIC_END
}
#endif
#ifdef _PROCESS_STATISTICS
uint32_t Process::getAvgRunTime()
{
if (!_histIterations)
return 0;
return _histRunTime / _histIterations;
}
bool Process::statsWillOverflow(hIterCount_t iter, hTimeCount_t tm)
{
return (_histIterations > _histIterations + iter) || (_histRunTime > _histRunTime + tm);
}
void Process::divStats(uint8_t div)
{
++_histIterations /= div;
++_histRunTime /= div;
}
#endif