|
| 1 | + |
| 2 | +#include <assert.h> |
| 3 | + |
1 | 4 | #include "Schedule.h"
|
| 5 | +#include "PolledTimeout.h" |
| 6 | +#include "interrupts.h" |
| 7 | + |
| 8 | +typedef std::function<bool(void)> mFuncT; |
2 | 9 |
|
3 | 10 | struct scheduled_fn_t
|
4 | 11 | {
|
5 |
| - scheduled_fn_t* mNext; |
6 |
| - std::function<void(void)> mFunc; |
| 12 | + scheduled_fn_t* mNext = nullptr; |
| 13 | + mFuncT mFunc; |
| 14 | + esp8266::polledTimeout::periodicFastUs callNow; |
| 15 | + |
| 16 | + scheduled_fn_t() : callNow(esp8266::polledTimeout::periodicFastUs::alwaysExpired) { } |
7 | 17 | };
|
8 | 18 |
|
9 |
| -static scheduled_fn_t* sFirst = 0; |
10 |
| -static scheduled_fn_t* sLast = 0; |
| 19 | +static scheduled_fn_t* sFirst = nullptr; |
| 20 | +static scheduled_fn_t* sLast = nullptr; |
11 | 21 |
|
12 |
| -static scheduled_fn_t* sFirstUnused = 0; |
13 |
| -static scheduled_fn_t* sLastUnused = 0; |
| 22 | +static scheduled_fn_t* sUnused = nullptr; |
14 | 23 |
|
15 | 24 | static int sCount = 0;
|
16 | 25 |
|
17 |
| -static scheduled_fn_t* get_fn() { |
18 |
| - scheduled_fn_t* result = NULL; |
| 26 | +IRAM_ATTR // called from ISR |
| 27 | +static scheduled_fn_t* get_fn_unsafe() |
| 28 | +{ |
| 29 | + scheduled_fn_t* result = nullptr; |
19 | 30 | // try to get an item from unused items list
|
20 |
| - if (sFirstUnused) { |
21 |
| - result = sFirstUnused; |
22 |
| - sFirstUnused = result->mNext; |
23 |
| - if (sFirstUnused == NULL) { |
24 |
| - sLastUnused = NULL; |
25 |
| - } |
| 31 | + if (sUnused) |
| 32 | + { |
| 33 | + result = sUnused; |
| 34 | + sUnused = sUnused->mNext; |
| 35 | + result->mNext = nullptr; |
26 | 36 | }
|
27 | 37 | // if no unused items, and count not too high, allocate a new one
|
28 |
| - else if (sCount != SCHEDULED_FN_MAX_COUNT) { |
| 38 | + else if (sCount < SCHEDULED_FN_MAX_COUNT) |
| 39 | + { |
29 | 40 | result = new scheduled_fn_t;
|
30 |
| - result->mNext = NULL; |
31 | 41 | ++sCount;
|
32 | 42 | }
|
33 | 43 | return result;
|
34 | 44 | }
|
35 | 45 |
|
36 |
| -static void recycle_fn(scheduled_fn_t* fn) |
| 46 | +static void recycle_fn_unsafe(scheduled_fn_t* fn) |
37 | 47 | {
|
38 |
| - if (!sLastUnused) { |
39 |
| - sFirstUnused = fn; |
40 |
| - } |
41 |
| - else { |
42 |
| - sLastUnused->mNext = fn; |
43 |
| - } |
44 |
| - fn->mNext = NULL; |
45 |
| - sLastUnused = fn; |
| 48 | + fn->mFunc = nullptr; // special overload in c++ std lib |
| 49 | + fn->mNext = sUnused; |
| 50 | + sUnused = fn; |
46 | 51 | }
|
47 | 52 |
|
48 |
| -bool schedule_function(std::function<void(void)> fn) |
| 53 | +IRAM_ATTR // (not only) called from ISR |
| 54 | +bool schedule_function_us(std::function<bool(void)>&& fn, uint32_t repeat_us) |
49 | 55 | {
|
50 |
| - scheduled_fn_t* item = get_fn(); |
51 |
| - if (!item) { |
| 56 | + assert(repeat_us < decltype(scheduled_fn_t::callNow)::neverExpires); //~26800000us (26.8s) |
| 57 | + |
| 58 | + InterruptLock lockAllInterruptsInThisScope; |
| 59 | + |
| 60 | + scheduled_fn_t* item = get_fn_unsafe(); |
| 61 | + if (!item) |
52 | 62 | return false;
|
53 |
| - } |
| 63 | + |
| 64 | + if (repeat_us) |
| 65 | + item->callNow.reset(repeat_us); |
| 66 | + |
54 | 67 | item->mFunc = fn;
|
55 |
| - item->mNext = NULL; |
56 |
| - if (!sFirst) { |
57 |
| - sFirst = item; |
58 |
| - } |
59 |
| - else { |
| 68 | + if (sFirst) |
60 | 69 | sLast->mNext = item;
|
61 |
| - } |
| 70 | + else |
| 71 | + sFirst = item; |
62 | 72 | sLast = item;
|
| 73 | + |
63 | 74 | return true;
|
64 | 75 | }
|
65 | 76 |
|
| 77 | +IRAM_ATTR // (not only) called from ISR |
| 78 | +bool schedule_function_us(const std::function<bool(void)>& fn, uint32_t repeat_us) |
| 79 | +{ |
| 80 | + return schedule_function_us(std::function<bool(void)>(fn), repeat_us); |
| 81 | +} |
| 82 | + |
| 83 | +IRAM_ATTR // called from ISR |
| 84 | +bool schedule_function(std::function<void(void)>&& fn) |
| 85 | +{ |
| 86 | + return schedule_function_us([fn]() { fn(); return false; }, 0); |
| 87 | +} |
| 88 | + |
| 89 | +IRAM_ATTR // called from ISR |
| 90 | +bool schedule_function(const std::function<void(void)>& fn) |
| 91 | +{ |
| 92 | + return schedule_function(std::function<void(void)>(fn)); |
| 93 | +} |
| 94 | + |
66 | 95 | void run_scheduled_functions()
|
67 | 96 | {
|
68 |
| - scheduled_fn_t* rFirst = sFirst; |
69 |
| - sFirst = NULL; |
70 |
| - sLast = NULL; |
71 |
| - while (rFirst) { |
72 |
| - scheduled_fn_t* item = rFirst; |
73 |
| - rFirst = item->mNext; |
74 |
| - item->mFunc(); |
75 |
| - item->mFunc = std::function<void(void)>(); |
76 |
| - recycle_fn(item); |
| 97 | + // Note to the reader: |
| 98 | + // There is no exposed API to remove a scheduled function: |
| 99 | + // Scheduled functions are removed only from this function, and |
| 100 | + // its purpose is that it is never called from an interrupt |
| 101 | + // (always on cont stack). |
| 102 | + |
| 103 | + scheduled_fn_t* lastRecurring = nullptr; |
| 104 | + scheduled_fn_t* toCall = sFirst; |
| 105 | + while (toCall) |
| 106 | + { |
| 107 | + scheduled_fn_t* item = toCall; |
| 108 | + toCall = toCall->mNext; |
| 109 | + if (item->callNow) |
| 110 | + { |
| 111 | + if (item->mFunc()) |
| 112 | + { |
| 113 | + lastRecurring = item; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + InterruptLock lockAllInterruptsInThisScope; |
| 118 | + |
| 119 | + if (sFirst == item) |
| 120 | + sFirst = sFirst->mNext; |
| 121 | + else if (lastRecurring) |
| 122 | + lastRecurring->mNext = item->mNext; |
| 123 | + |
| 124 | + if (sLast == item) |
| 125 | + sLast = lastRecurring; |
| 126 | + |
| 127 | + recycle_fn_unsafe(item); |
| 128 | + } |
| 129 | + } |
77 | 130 | }
|
78 | 131 | }
|
0 commit comments