Skip to content

Commit d87e242

Browse files
committed
ISR safety.
1 parent 03713a7 commit d87e242

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

cores/esp8266/Schedule.cpp

+11-8
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()
@@ -248,9 +248,12 @@ void run_scheduled_recurrent_functions()
248248
{
249249
prev = current;
250250
current = current->mNext;
251+
252+
esp8266::InterruptLock lockAllInterruptsInThisScope;
253+
251254
// 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())
255+
const int32_t remaining = rTarget - micros();
256+
if (remaining > 0 && static_cast<uint32_t>(remaining) > current->callNow.remaining())
254257
{
255258
rTarget = micros() + current->callNow.remaining();
256259
}

0 commit comments

Comments
 (0)