Skip to content

Commit 207bc27

Browse files
committed
ISR safety.
1 parent 03713a7 commit 207bc27

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

Diff for: cores/esp8266/Schedule.cpp

+22-14
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
119119
item->mFunc = fn;
120120
item->alarm = alarm;
121121

122+
esp8266::InterruptLock lockAllInterruptsInThisScope;
123+
122124
// prevent new item overwriting an already expired rTarget.
123-
const int32_t rRemaining = rTarget - micros();
124-
if (!rFirst || (rRemaining > 0 && static_cast<uint32_t>(rRemaining) > item->callNow.remaining()))
125+
const int32_t remaining = rTarget - micros();
126+
if (!rFirst || (remaining > 0 && static_cast<uint32_t>(remaining) > item->callNow.remaining()))
125127
{
126128
rTarget = micros() + item->callNow.remaining();
127129
}
128130

129-
esp8266::InterruptLock lockAllInterruptsInThisScope;
130-
131131
if (rLast)
132132
{
133133
rLast->mNext = item;
@@ -145,8 +145,8 @@ uint32_t get_scheduled_recurrent_delay_us()
145145
{
146146
if (!rFirst) return ~static_cast<uint32_t>(0);
147147
// handle already expired rTarget.
148-
const int32_t rRemaining = rTarget - micros();
149-
return (rRemaining > 0) ? static_cast<uint32_t>(rRemaining) : 0;
148+
const int32_t remaining = rTarget - micros();
149+
return (remaining > 0) ? static_cast<uint32_t>(remaining) : 0;
150150
}
151151

152152
void run_scheduled_functions()
@@ -209,12 +209,18 @@ void run_scheduled_recurrent_functions()
209209
fence = true;
210210
}
211211

212-
rTarget = micros() + current->callNow.remaining();
212+
decltype(rLast) stop;
213213
recurrent_fn_t* prev = nullptr;
214-
// prevent scheduling of new functions during this run
215-
auto stop = rLast;
216-
217214
bool done;
215+
216+
{
217+
esp8266::InterruptLock lockAllInterruptsInThisScope;
218+
219+
// prevent scheduling of new functions during this run
220+
stop = rLast;
221+
rTarget = micros() + (~static_cast<decltype(micros())>(0) >> 1);
222+
}
223+
218224
do
219225
{
220226
done = current == stop;
@@ -246,14 +252,16 @@ void run_scheduled_recurrent_functions()
246252
}
247253
else
248254
{
249-
prev = current;
250-
current = current->mNext;
255+
esp8266::InterruptLock lockAllInterruptsInThisScope;
256+
251257
// prevent current item overwriting an already expired rTarget.
252-
const int32_t rRemaining = rTarget - micros();
253-
if (rRemaining > 0 && static_cast<uint32_t>(rRemaining) > current->callNow.remaining())
258+
const int32_t remaining = rTarget - micros();
259+
if (remaining > 0 && static_cast<uint32_t>(remaining) > current->callNow.remaining())
254260
{
255261
rTarget = micros() + current->callNow.remaining();
256262
}
263+
prev = current;
264+
current = current->mNext;
257265
}
258266

259267
if (yieldNow)

0 commit comments

Comments
 (0)