-
Notifications
You must be signed in to change notification settings - Fork 7
/
antirtos.h
489 lines (436 loc) · 15 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// under MIT license, Aleksei Tertychnyi
#ifndef antirtos_h
#define antirtos_h
/**
* @class fQ
* @brief A queue-based task scheduler for function pointers without parameters.
*
* This class provides a mechanism to manage and execute tasks represented as function pointers.
* Tasks are executed in the order they are added to the queue.
*/
class fQ {
public:
/**
* @typedef fP
* @brief Function pointer type that takes no parameters.
*/
typedef void (*fP)(void);
/**
* @brief Constructor to initialize the queue with a specified size.
* @param sizeQ The maximum number of tasks the queue can hold.
*/
fQ(int sizeQ);
/**
* @brief Destructor to clean up dynamically allocated resources.
*/
~fQ();
/**
* @brief Pushes a function pointer into the queue.
* @param pointerF The function pointer to be added to the queue.
* @return 0 if the function was successfully added; 1 if the queue is full.
*/
int push(fP pointerF);
/**
* @brief Pulls the next function from the queue and executes it.
* @return 0 if a function was successfully executed; 1 if the queue is empty.
*/
int pull(void);
private:
int first; ///< Index of the first element in the queue.
volatile int last; ///< Index of the last element in the queue.
int length; ///< Maximum size of the queue.
fP* fQueue; ///< Queue for function pointers.
};
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 from 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;
}
};
/**
* @class fQP
* @brief A queue-based task scheduler for function pointers with parameter support.
*
* This template class allows function pointers with associated parameters to be queued
* for execution. Tasks are executed in the order they are added.
*
* @tparam T The type of the parameter passed to the function pointers.
*/
template <typename T>
class fQP {
public:
/**
* @brief Constructor to initialize the queue with a specified size.
* @param sizeQ The maximum number of tasks the queue can hold.
*/
fQP(int sizeQ);
/**
* @brief Destructor to clean up dynamically allocated resources.
*/
~fQP();
/**
* @brief Pushes a function pointer and its parameter into the queue.
* @param pointerF The function pointer to be added to the queue.
* @param parameterQ The parameter associated with the function pointer.
* @return 0 if the function was successfully added; 1 if the queue is full.
*/
int push(void (*pointerF)(T), T parameterQ);
/**
* @brief Pulls the next function from the queue and executes it with its parameter.
* @return 0 if a function was successfully executed; 1 if the queue is empty.
*/
int pull();
private:
int first; ///< Index of the first element in the queue.
volatile int last; ///< Index of the last element in the queue.
int length; ///< Maximum size of the queue.
typedef void (*fP)(T); ///< Function pointer type that accepts a parameter of type T.
fP* FP_Queue; ///< Queue for function pointers.
T* PARAMS_array; ///< Queue for parameters associated with the function pointers.
};
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;
}
}
/**
* @class del_fQ
* @brief A queue-based task scheduler for function pointers without parameters, with optional delayed execution.
*
* This class provides a mechanism to manage function pointers with optional delays in their execution.
* Tasks can be executed immediately or scheduled for future execution based on a time delay.
*
*/
class del_fQ {
public:
/**
* @typedef fP
* @brief Function pointer type that takes no parameters.
*/
typedef void (*fP)(void);
/**
* @brief Constructor to initialize the queue with a specified size.
* @param sizeQ The maximum number of tasks the queue can hold.
*/
del_fQ(int sizeQ);
/**
* @brief Destructor to clean up dynamically allocated resources.
*/
~del_fQ();
/**
* @brief Pushes a function pointer into the delayed queue for future execution.
* @param pointerF The function pointer to be added to the delayed queue.
* @param delayTime The delay in ticks before the function is executed.
* @return 0 if the function was successfully added; 1 if the delayed queue is full.
*/
int push_delayed(fP pointerF, unsigned long delayTime);
/**
* @brief Pushes a function pointer into the queue for immediate execution.
* @param pointerF The function pointer to be added to the queue.
* @return 0 if the function was successfully added; 1 if the queue is full.
*/
int push(fP pointerF);
/**
* @brief Periodic function to manage delayed tasks. Call this method in an ISR or main loop.
*/
void tick(void);
/**
* @brief Pulls the next function from the queue and executes it.
* @return 0 if a function was successfully executed; 1 if the queue is empty.
*/
int pull(void);
/**
* @brief Revokes a function pointer from the delayed queue.
* @param pointerF The function pointer to be removed.
* @return 0 if the function pointer was successfully revoked; 1 if it was not found.
*/
int revoke(fP pointerF);
private:
int first; ///< Index of the first element in the queue.
volatile int last; ///< Index of the last element in the queue.
int length; ///< Maximum size of the queue.
unsigned long time; ///< Current tick count for managing delays.
fP *fQueue; ///< Queue for immediate function pointers.
fP *del_fQueue; ///< Queue for delayed function pointers.
bool *execArr; ///< Execution flags for delayed tasks.
unsigned long *execTime; ///< Execution times for delayed tasks.
};
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) { // revocation 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 from 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;
}
};
/**
* @class del_fQP
* @brief A queue-based task scheduler for function pointers with optional delayed execution.
*
* This template class manages function pointers and their execution. It supports both immediate
* and delayed execution of tasks with associated parameters. The class is suitable for
* embedded applications with constrained resources.
*
* @tparam T The type of the parameter passed to the function pointers.
*/
template <typename T>
class del_fQP {
public:
/**
* @typedef fP
* @brief Function pointer type that accepts a parameter of type T.
*/
typedef void (*fP)(T);
/**
* @brief Constructor to initialize the queue with a given size.
* @param sizeQ The maximum number of tasks the queue can hold.
*/
del_fQP(int sizeQ);
/**
* @brief Destructor to clean up dynamically allocated resources.
*/
~del_fQP();
/**
* @brief Pushes a function pointer with its parameter into the queue for immediate execution.
* @param pointerF The function pointer to be pushed.
* @param parameterQ The parameter associated with the function pointer.
* @return 0 if the function was successfully added; 1 if the queue is full.
*/
int push(void (*pointerF)(T), T parameterQ);
/**
* @brief Pushes a function pointer with its parameter into the delayed queue for future execution.
* @param pointerF The function pointer to be added to the delayed queue.
* @param parameterQ The parameter associated with the function pointer.
* @param delayTime The delay in ticks before the function is executed.
* @return 0 if the function was successfully added; 1 if the delayed queue is full.
*/
int push_delayed(void (*pointerF)(T), T parameterQ, unsigned long delayTime);
/**
* @brief Periodic function to manage delayed tasks. Call this method in an ISR or main loop.
*/
void tick(void);
/**
* @brief Revokes a function pointer from the delayed queue.
* @param pointerF The function pointer to be removed.
* @return 0 if the function pointer was successfully revoked; 1 if it was not found.
*/
int revoke(void (*pointerF)(T));
/**
* @brief Pulls the next function from the queue and executes it.
* @return 0 if a function was successfully executed; 1 if the queue is empty.
*/
int pull();
private:
int first; ///< Index of the first element in the queue.
volatile int last; ///< Index of the last element in the queue.
int length; ///< Maximum size of the queue.
unsigned long time; ///< Current tick count for managing delays.
fP *FP_Queue; ///< Queue for immediate function pointers.
fP *del_FP_Queue; ///< Queue for delayed function pointers.
bool *execArr; ///< Execution flags for delayed tasks.
unsigned long *execTime; ///< Execution times for delayed tasks.
T *PARAMS_array; ///< Parameters for immediate tasks.
T *delayed_PARAMS_array; ///< Parameters for delayed tasks.
};
template <typename T>
del_fQP<T>::del_fQP(int sizeQ) {
FP_Queue = new fP[sizeQ];
del_FP_Queue = new fP[sizeQ];
execArr = new bool[sizeQ];
PARAMS_array = new T[sizeQ];
delayed_PARAMS_array = new T[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;
}
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) {
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) {
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)) { // revocation method, revokes
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() {
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