Skip to content

Commit dcc5f5b

Browse files
committed
Fix for scheduling recurrent functions while inside scheduled function
1 parent 5ca0bde commit dcc5f5b

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

Diff for: cores/esp8266/Schedule.cpp

+13-19
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct recurrent_fn_t
2727
recurrent_fn_t (esp8266::polledTimeout::periodicFastUs interval): callNow(interval) { }
2828
};
2929

30-
static recurrent_fn_t* rFirst = nullptr; // fifo not needed
30+
static recurrent_fn_t rFirst(0); // fifo not needed
3131

3232
// Returns a pointer to an unused sched_fn_t,
3333
// or if none are available allocates a new one,
@@ -92,10 +92,8 @@ bool schedule_recurrent_function_us (const std::function<bool(void)>& fn, uint32
9292

9393
item->mFunc = fn;
9494

95-
if (rFirst)
96-
item->mNext = rFirst;
97-
98-
rFirst = item;
95+
item->mNext = rFirst.mNext;
96+
rFirst.mNext = item;
9997

10098
return true;
10199
}
@@ -136,7 +134,8 @@ void run_scheduled_recurrent_functions ()
136134
// its purpose is that it is never called from an interrupt
137135
// (always on cont stack).
138136

139-
if (!rFirst)
137+
recurrent_fn_t* current = rFirst.mNext;
138+
if (!current)
140139
return;
141140

142141
static bool fence = false;
@@ -152,10 +151,9 @@ void run_scheduled_recurrent_functions ()
152151
fence = true;
153152
}
154153

155-
recurrent_fn_t* prev = nullptr;
156-
recurrent_fn_t* current = rFirst;
154+
recurrent_fn_t* prev = &rFirst;
157155

158-
while (current)
156+
do
159157
{
160158
if (current->callNow && !current->mFunc())
161159
{
@@ -164,16 +162,11 @@ void run_scheduled_recurrent_functions ()
164162

165163
auto to_ditch = current;
166164

167-
if (prev)
168-
{
169-
current = current->mNext;
170-
prev->mNext = current;
171-
}
172-
else
173-
{
174-
rFirst = rFirst->mNext;
175-
current = rFirst;
176-
}
165+
// current function recursively scheduled something
166+
if (prev->mNext != current) prev = prev->mNext;
167+
168+
current = current->mNext;
169+
prev->mNext = current;
177170

178171
delete(to_ditch);
179172
}
@@ -183,6 +176,7 @@ void run_scheduled_recurrent_functions ()
183176
current = current->mNext;
184177
}
185178
}
179+
while (current);
186180

187181
fence = false;
188182
}

0 commit comments

Comments
 (0)