@@ -29,7 +29,8 @@ struct recurrent_fn_t
29
29
recurrent_fn_t (esp8266::polledTimeout::periodicFastUs interval) : callNow(interval) { }
30
30
};
31
31
32
- static recurrent_fn_t rFirst (0 ); // fifo not needed
32
+ static recurrent_fn_t rFirst (0 );
33
+ static recurrent_fn_t * rLast = &rFirst;
33
34
34
35
// Returns a pointer to an unused sched_fn_t,
35
36
// or if none are available allocates a new one,
@@ -98,13 +99,14 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn, uint32_
98
99
return false ;
99
100
100
101
item->mFunc = fn;
102
+ item->mNext = nullptr ;
101
103
item->wakeupToken = wakeupToken;
102
104
item->wakeupTokenCmp = wakeupToken && wakeupToken->load ();
103
105
104
106
esp8266::InterruptLock lockAllInterruptsInThisScope;
105
107
106
- item ->mNext = rFirst. mNext ;
107
- rFirst. mNext = item;
108
+ rLast ->mNext = item ;
109
+ rLast = item;
108
110
109
111
return true ;
110
112
}
@@ -124,9 +126,9 @@ void run_scheduled_functions()
124
126
sFirst = sFirst ->mNext ;
125
127
if (!sFirst )
126
128
sLast = nullptr ;
127
- recycle_fn_unsafe (to_recycle);
128
- }
129
-
129
+ recycle_fn_unsafe (to_recycle);
130
+ }
131
+
130
132
if (yieldNow)
131
133
{
132
134
// because scheduled function are allowed to last:
@@ -147,7 +149,7 @@ void run_scheduled_recurrent_functions()
147
149
// its purpose is that it is never called from an interrupt
148
150
// (always on cont stack).
149
151
150
- recurrent_fn_t * current = rFirst.mNext ;
152
+ auto current = rFirst.mNext ;
151
153
if (!current)
152
154
return ;
153
155
@@ -164,10 +166,14 @@ void run_scheduled_recurrent_functions()
164
166
fence = true ;
165
167
}
166
168
167
- recurrent_fn_t * prev = &rFirst;
169
+ auto prev = &rFirst;
170
+ // prevent scheduling of new functions during this run
171
+ auto stop = rLast;
168
172
173
+ bool done;
169
174
do
170
175
{
176
+ done = current == stop;
171
177
const bool wakeupToken = current->wakeupToken && current->wakeupToken ->load ();
172
178
const bool wakeup = current->wakeupTokenCmp != wakeupToken;
173
179
if (wakeup) current->wakeupTokenCmp = wakeupToken;
@@ -180,11 +186,10 @@ void run_scheduled_recurrent_functions()
180
186
181
187
auto to_ditch = current;
182
188
183
- // current function recursively scheduled something
184
- if (prev->mNext != current) prev = prev->mNext ;
185
-
186
189
current = current->mNext ;
187
190
prev->mNext = current;
191
+ // removing rLast
192
+ if (!current) rLast = prev;
188
193
189
194
delete (to_ditch);
190
195
}
@@ -201,7 +206,7 @@ void run_scheduled_recurrent_functions()
201
206
esp_schedule ();
202
207
cont_yield (g_pcont);
203
208
}
204
- } while (current);
209
+ } while (current && !done );
205
210
206
211
fence = false ;
207
212
}
0 commit comments