-
Notifications
You must be signed in to change notification settings - Fork 6
/
antirtos.h
327 lines (299 loc) · 8.86 KB
/
antirtos.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// under MIT license, Aleksei Tertychnyi
#ifndef antirtos_h
#define antirtos_h
/// @brief class for finctional pointers queue without parameters
class fQ {
public:
typedef void(*fP)(void);
fQ(int sizeQ);
~fQ();
int push(fP);
int pull(void);
private:
int first;
volatile int last;
int length;
fP * fQueue;
};
fQ::fQ(int sizeQ) { // initialization of Queue (constructor)
fQueue = new fP[sizeQ];
last = 0;
first = 0;
length = sizeQ;
};
fQ::~fQ() { // destructor
delete [] fQueue;
};
int fQ::push(fP pointerF) { // push element to the queue
if ((last + 1) % length == first) {
return 1;
}
fQueue[last++] = pointerF;
last = last % length;
return 0;
};
int fQ::pull(void) { // pull element from the queue
if (last != first) {
fQueue[first++]();
first = first % length;
return 0;
}
else {
return 1;
}
};
/// @brief class for functional pointers queue with supporting of transfer of parameters
template <typename T>
class fQP {
public:
fQP(int sizeQ);
~fQP();
int push(void (*pointerF)(T), T parameterQ);
int pull();
private:
int first;
volatile int last;
int length;
typedef void (*fP)(T);
fP* FP_Queue;
T* PARAMS_array;
};
template <typename T>
fQP<T>::fQP(int sizeQ) { // constructor
FP_Queue = new fP[sizeQ];
PARAMS_array = new T[sizeQ];
last = 0;
first = 0;
length = sizeQ;
}
template <typename T>
fQP<T>::~fQP() { //destructor
delete[] FP_Queue;
delete[] PARAMS_array;
}
template <typename T>
int fQP<T>::push(void (*pointerF)(T), T parameterQ) { //push your task into queue
if ((last + 1) % length == first) return 1;
FP_Queue[last] = pointerF;
PARAMS_array[last] = parameterQ;
last = (last + 1) % length;
return 0;
}
template <typename T>
int fQP<T>::pull() { // pulls task and parameters from the queue and execute
fP pullVar;
if (last != first) {
T Params = PARAMS_array[first];
pullVar = FP_Queue[first];
first = (first + 1) % length;
pullVar(Params);
return 0;
}
else {
return 1;
}
}
/// @brief delayed functional pointers queue without parameters, only time delay parameter
class del_fQ {
public:
typedef void(*fP)(void);
del_fQ(int sizeQ); // constructor
~del_fQ(); // destructor
int push_delayed(fP pointerF, unsigned long delayTime); //push delayed
int push(fP pointerF); //direct push without delay, use with care
void tick(void); //tick() - put it into periodic function or interrupt ISR
int pull(void); //pull function pointer and execute
int revoke(fP pointerF); //revoke the function pointer/s from the queue (will be not executed)
private:
int first;
volatile int last;
int length;
unsigned long time;
fP * fQueue; //'ready' for execution functions will be put in this queue
fP * del_fQueue; // delayed functions queue
bool * execArr; //is need to be executed?
unsigned long * execTime; //execution time arr
};
del_fQ::del_fQ(int sizeQ) { // constructor
fQueue = new fP[sizeQ];
del_fQueue = new fP[sizeQ];
execArr = new bool[sizeQ];
execTime = new unsigned long[sizeQ];
last = 0;
first = 0;
time = 0;
for (unsigned int i = 0; i < sizeQ; i++) {
execArr[i] = false;
}
length = sizeQ;
};
del_fQ::~del_fQ() { // destructor
delete [] fQueue;
delete [] del_fQueue;
delete [] execArr;
delete [] execTime;
};
int del_fQ::push_delayed(fP pointerF, unsigned long delayTime) { // push element from the queue
bool fullQ = true; // is Queue full?
for (unsigned int i = 0; i < length; i++) {
if (!execArr[i] ) {
del_fQueue[i] = pointerF; // put pointer into exec queue
execArr[i] = true; // true flag for execution
execTime[i] = time + delayTime; //calc execution time, no worry if overload
fullQ = false;
break;
}
}
if (fullQ) return 1;
return 0;
};
void del_fQ::tick(void) { // tick method to provide delay functionality, put it into periodic routine
static unsigned int i = 0 ; //uses in search cycle every tick
for (i = 0; i < length; i++) {
if (execTime[i] == time)
if (execArr[i]) {
push(del_fQueue[i]); // bump into normal queue part of delayed Queue
execArr[i] = false;
}
}
time++;
}
int del_fQ::revoke(fP pointerF) { // revokation of task from the queue in case you do not need it any more
int result = 1;
for (int i = 0; i < length; i++) {
if (del_fQueue[i] == pointerF) {
execArr[i] = false;
result = 0;
}
}
return result;
}
int del_fQ::push(fP pointerF) { // push element to the queue
if ((last + 1) % length == first) {
return 1;
}
fQueue[last++] = pointerF;
last = last % length;
return 0;
};
int del_fQ::pull(void) { // pull element from the queue
if (last != first) {
fQueue[first++]();
first = first % length;
return 0;
}
else {
return 1;
}
};
/// @brief delayed functional pointers queue with parameters
template <typename T>
class del_fQP {
public:
typedef void (*fP)(T);
int push(void (*pointerF)(T), T parameterQ); //push directly (without delay) use with care here
del_fQP(int sizeQ);
~del_fQP();
int push_delayed(void (*pointerF)(T), T parameterQ, unsigned long delayTime); //push delayed
void tick(void); //tick() - put it into periodic function or interrupt ISR
int revoke(void (*pointerF)(T)); // revoke the function pointer/s from the queue
int pull();
private:
int first;
volatile int last;
int length;
unsigned long time;
fP * FP_Queue; // function pointers queue
fP * del_FP_Queue; // delayed function pointers
bool * execArr; // is need to be executed?
unsigned long * execTime; // execution time array
T* PARAMS_array; // parameters for functions ready to execute
T* delayed_PARAMS_array; // parameters for delayed functions
};
template <typename T>
del_fQP<T>::del_fQP(int sizeQ) {
FP_Queue = new fP[sizeQ]; // function queue (ready to execute)
del_FP_Queue = new fP[sizeQ]; // array of delayed tasks (functions)
execArr = new bool[sizeQ]; // to mark vacant position in temporal (delayed) array
PARAMS_array = new T[sizeQ]; //array of parameters ready to be passed to funtions (to execute)
delayed_PARAMS_array = new T[sizeQ]; //array of parameters (to pass to delayed functions)
execTime = new unsigned long[sizeQ]; //array to store execution time (schedule)
last = 0;
first = 0;
time = 0;
for (unsigned int i = 0; i < sizeQ; i++) {
execArr[i] = false;
}
length = sizeQ;
}
template <typename T>
del_fQP<T>::~del_fQP() {
delete[] FP_Queue;
delete[] del_FP_Queue;
delete[] PARAMS_array;
delete[] delayed_PARAMS_array;
delete [] execArr;
delete [] execTime;
}
template <typename T>
int del_fQP<T>::push(void (*pointerF)(T), T parameterQ) { //push directly, without delay
if ((last + 1) % length == first) return 1;
FP_Queue[last] = pointerF;
PARAMS_array[last] = parameterQ;
last = (last + 1) % length;
return 0;
}
template <typename T>
int del_fQP<T>::push_delayed(void (*pointerF)(T), T parameterQ, unsigned long delayTime) { // push delayed function
bool fullQ = true; // is Queue full?
for (unsigned int i = 0; i < length; i++) {
if (!execArr[i] ) {
del_FP_Queue[i] = pointerF; // put function pointer into exec queue
delayed_PARAMS_array[i] = parameterQ; // put parameter into exec queue
execArr[i] = true; // true flag for execution
execTime[i] = time + delayTime; //calc execution time, no worry if overload
fullQ = false;
break;
}
}
if (fullQ) return 1;
return 0;
}
template <typename T>
void del_fQP<T>::tick(void) {
static unsigned int i = 0 ; //uses in search cycle every tick
for (i = 0; i < length; i++) {
if (execTime[i] == time)
if (execArr[i]) {
push(del_FP_Queue[i], delayed_PARAMS_array[i]); // bump into normal queue part of delayed Queue
execArr[i] = false;
}
}
time++;
}
template <typename T>
int del_fQP<T>::revoke(void (*pointerF)(T)) { // revoke from execution
int result = 1;
for (int i = 0; i < length; i++) {
if (del_FP_Queue[i] == pointerF) {
execArr[i] = false;
result = 0;
}
}
return result;
}
template <typename T>
int del_fQP<T>::pull() { // pull from a queue (and execute)
fP pullVar;
if (last != first) {
T Params = PARAMS_array[first];
pullVar = FP_Queue[first];
first = (first + 1) % length;
pullVar(Params);
return 0;
}
else {
return 1;
}
}
#endif