Repository navigation
Expand file tree
/
Copy pathSchedule.cpp
More file actions
233 lines (189 loc) · 5.48 KB
/
Copy pathSchedule.cpp
File metadata and controls
233 lines (189 loc) · 5.48 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
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
#include <assert.h>
#include "Schedule.h"
#include "PolledTimeout.h"
#include "interrupts.h"
#include "coredecls.h"
typedef std::function<void(void)> mSchedFuncT;
struct scheduled_fn_t
{
scheduled_fn_t* mNext = nullptr;
mSchedFuncT mFunc;
};
static scheduled_fn_t* sFirst = nullptr;
static scheduled_fn_t* sLast = nullptr;
static scheduled_fn_t* sUnused = nullptr;
static int sCount = 0;
typedef std::function<bool(void)> mRecFuncT;
struct recurrent_fn_t
{
recurrent_fn_t* mNext = nullptr;
mRecFuncT mFunc;
esp8266::polledTimeout::periodicFastUs callNow;
std::function<bool(void)> alarm = nullptr;
recurrent_fn_t(esp8266::polledTimeout::periodicFastUs interval) : callNow(interval) { }
};
static recurrent_fn_t* rFirst = nullptr;
static recurrent_fn_t* rLast = nullptr;
// Returns a pointer to an unused sched_fn_t,
// or if none are available allocates a new one,
// or nullptr if limit is reached
IRAM_ATTR // called from ISR
static scheduled_fn_t* get_fn_unsafe()
{
scheduled_fn_t* result = nullptr;
// try to get an item from unused items list
if (sUnused)
{
result = sUnused;
sUnused = sUnused->mNext;
}
// if no unused items, and count not too high, allocate a new one
else if (sCount < SCHEDULED_FN_MAX_COUNT)
{
result = new (std::nothrow) scheduled_fn_t;
if (result)
++sCount;
}
return result;
}
static void recycle_fn_unsafe(scheduled_fn_t* fn)
{
fn->mFunc = nullptr; // special overload in c++ std lib
fn->mNext = sUnused;
sUnused = fn;
}
IRAM_ATTR // (not only) called from ISR
bool schedule_function(const std::function<void(void)>& fn)
{
if (!fn)
return false;
esp8266::InterruptLock lockAllInterruptsInThisScope;
scheduled_fn_t* item = get_fn_unsafe();
if (!item)
return false;
item->mFunc = fn;
item->mNext = nullptr;
if (sFirst)
sLast->mNext = item;
else
sFirst = item;
sLast = item;
return true;
}
bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
uint32_t repeat_us, const std::function<bool(void)>& alarm)
{
assert(repeat_us < decltype(recurrent_fn_t::callNow)::neverExpires); //~26800000us (26.8s)
if (!fn)
return false;
recurrent_fn_t* item = new (std::nothrow) recurrent_fn_t(repeat_us);
if (!item)
return false;
item->mFunc = fn;
item->alarm = alarm;
esp8266::InterruptLock lockAllInterruptsInThisScope;
if (rLast)
{
rLast->mNext = item;
}
else
{
rFirst = item;
}
rLast = item;
return true;
}
void run_scheduled_functions()
{
esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms
// prevent scheduling of new functions during this run
auto stop = sLast;
bool done = false;
while (sFirst && !done)
{
done = sFirst == stop;
sFirst->mFunc();
{
// remove function from stack
esp8266::InterruptLock lockAllInterruptsInThisScope;
auto to_recycle = sFirst;
// removing rLast
if (sLast == sFirst)
sLast = nullptr;
sFirst = sFirst->mNext;
recycle_fn_unsafe(to_recycle);
}
if (yieldNow)
{
// because scheduled functions might last too long for watchdog etc,
// this is yield() in cont stack:
esp_schedule();
cont_yield(g_pcont);
}
}
}
void run_scheduled_recurrent_functions()
{
esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms
// Note to the reader:
// There is no exposed API to remove a scheduled function:
// Scheduled functions are removed only from this function, and
// its purpose is that it is never called from an interrupt
// (always on cont stack).
auto current = rFirst;
if (!current)
return;
static bool fence = false;
{
// fence is like a mutex but as we are never called from ISR,
// locking is useless here. Leaving comment for reference.
//esp8266::InterruptLock lockAllInterruptsInThisScope;
if (fence)
// prevent recursive calls from yield()
// (even if they are not allowed)
return;
fence = true;
}
recurrent_fn_t* prev = nullptr;
// prevent scheduling of new functions during this run
auto stop = rLast;
bool done;
do
{
done = current == stop;
const bool wakeup = current->alarm && current->alarm();
bool callNow = current->callNow;
if ((wakeup || callNow) && !current->mFunc())
{
// remove function from stack
esp8266::InterruptLock lockAllInterruptsInThisScope;
auto to_ditch = current;
// removing rLast
if (rLast == current)
rLast = prev;
current = current->mNext;
if (prev)
{
prev->mNext = current;
}
else
{
rFirst = current;
}
delete(to_ditch);
}
else
{
prev = current;
current = current->mNext;
}
if (yieldNow)
{
// because scheduled functions might last too long for watchdog etc,
// this is yield() in cont stack:
esp_schedule();
cont_yield(g_pcont);
}
} while (current && !done);
fence = false;
}