@@ -28,14 +28,14 @@ struct recurrent_fn_t
28
28
recurrent_fn_t (esp8266::polledTimeout::periodicFastUs interval) : callNow(interval) { }
29
29
};
30
30
31
- static recurrent_fn_t rFirst ( 0 ) ;
32
- static recurrent_fn_t * rLast = &rFirst ;
31
+ static recurrent_fn_t * rFirst = nullptr ;
32
+ static recurrent_fn_t * rLast = nullptr ;
33
33
34
34
// Returns a pointer to an unused sched_fn_t,
35
35
// or if none are available allocates a new one,
36
36
// or nullptr if limit is reached
37
37
IRAM_ATTR // called from ISR
38
- static scheduled_fn_t * get_fn_unsafe ()
38
+ static scheduled_fn_t * get_fn_unsafe ()
39
39
{
40
40
scheduled_fn_t * result = nullptr ;
41
41
// try to get an item from unused items list
@@ -54,15 +54,15 @@ static scheduled_fn_t* get_fn_unsafe ()
54
54
return result;
55
55
}
56
56
57
- static void recycle_fn_unsafe (scheduled_fn_t * fn)
57
+ static void recycle_fn_unsafe (scheduled_fn_t * fn)
58
58
{
59
59
fn->mFunc = nullptr ; // special overload in c++ std lib
60
60
fn->mNext = sUnused ;
61
61
sUnused = fn;
62
62
}
63
63
64
64
IRAM_ATTR // (not only) called from ISR
65
- bool schedule_function (const std::function<void (void )>& fn)
65
+ bool schedule_function (const std::function<void (void )>& fn)
66
66
{
67
67
if (!fn)
68
68
return false ;
@@ -98,18 +98,24 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
98
98
return false ;
99
99
100
100
item->mFunc = fn;
101
- item->mNext = nullptr ;
102
101
item->alarm = std::move (alarm);
103
102
104
103
esp8266::InterruptLock lockAllInterruptsInThisScope;
105
104
106
- rLast->mNext = item;
105
+ if (rLast)
106
+ {
107
+ rLast->mNext = item;
108
+ }
109
+ else
110
+ {
111
+ rFirst = item;
112
+ }
107
113
rLast = item;
108
114
109
115
return true ;
110
116
}
111
117
112
- void run_scheduled_functions ()
118
+ void run_scheduled_functions ()
113
119
{
114
120
esp8266::polledTimeout::periodicFastMs yieldNow (100 ); // yield every 100ms
115
121
@@ -147,7 +153,7 @@ void run_scheduled_recurrent_functions()
147
153
// its purpose is that it is never called from an interrupt
148
154
// (always on cont stack).
149
155
150
- auto current = rFirst. mNext ;
156
+ auto current = rFirst;
151
157
if (!current)
152
158
return ;
153
159
@@ -164,7 +170,7 @@ void run_scheduled_recurrent_functions()
164
170
fence = true ;
165
171
}
166
172
167
- auto prev = &rFirst ;
173
+ recurrent_fn_t * prev = nullptr ;
168
174
// prevent scheduling of new functions during this run
169
175
auto stop = rLast;
170
176
@@ -187,7 +193,14 @@ void run_scheduled_recurrent_functions()
187
193
rLast = prev;
188
194
189
195
current = current->mNext ;
190
- prev->mNext = current;
196
+ if (prev)
197
+ {
198
+ prev->mNext = current;
199
+ }
200
+ else
201
+ {
202
+ rFirst = current;
203
+ }
191
204
192
205
delete (to_ditch);
193
206
}
0 commit comments