10000 Readied MultiDelegate for ESP32 ISR safeness. · dok-net/arduino-esp8266@edbeeab · GitHub
[go: up one dir, main page]

Skip to content

Commit edbeeab

Browse files
committed
Readied MultiDelegate for ESP32 ISR safeness.
1 parent 79c3816 commit edbeeab

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

cores/esp8266/MultiDelegate.h

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
#ifndef __MULTIDELEGATE_H
22
#define __MULTIDELEGATE_H
33

4+
#include <atomic>
5+
#ifdef ESP8266
46
#include <interrupts.h>
7+
using esp8266::InterruptLock;
8+
#else
9+
class InterruptLock {
10+
public:
11+
InterruptLock() {
12+
noInterrupts();
13+
}
14+
~InterruptLock() {
15+
interrupts();
16+
}
17+
};
18+
#endif
519

620
namespace detail
721
{
@@ -177,7 +191,7 @@ namespace detail
177191
if (!del)
178192
return nullptr;
179193

180-
esp8266::InterruptLock lockAllInterruptsInThisScope;
194+
InterruptLock lockAllInterruptsInThisScope;
181195

182196
Node_t* item = get_node_unsafe();
183197
if (!item)
@@ -207,7 +221,7 @@ namespace detail
207221
if (del == &current->mDelegate)
208222
{
209223
// remove callback from stack
210-
esp8266::InterruptLock lockAllInterruptsInThisScope;
224+
InterruptLock lockAllInterruptsInThisScope;
211225

212226
auto to_recycle = current;
213227

@@ -243,15 +257,14 @@ namespace detail
243257
if (!current)
244258
return;
245259

246-
static bool fence = false;
247-
{
248-
esp8266::InterruptLock lockAllInterruptsInThisScope;
249-
250-
// prevent recursive calls
251-
if (fence)
252-
return;
253-
fence = true;
254-
}
260+
static std::atomic<bool> fence(false);
261+
// prevent recursive calls
262+
#ifdef ESP8266
263+
if (fence.load()) return;
264+
fence.store(true);
265+
#else
266+
if (fence.exchange(true)) return;
267+
#endif
255268

256269
Node_t* prev = nullptr;
257270
// prevent execution of new callbacks during this run
@@ -264,7 +277,7 @@ namespace detail
264277
if (!CallP<Delegate, R, ISQUEUE, P...>::execute(current->mDelegate, args...))
265278
{
266279
// remove callback from stack
267-
esp8266::InterruptLock lockAllInterruptsInThisScope;
280+
InterruptLock lockAllInterruptsInThisScope;
268281

269282
auto to_recycle = current;
270283

@@ -294,7 +307,7 @@ namespace detail
294307
optimistic_yield(10000);
295308
} while (current && !done);
296309

297-
fence = false;
310+
fence.store(false);
298311
}
299312
};
300313

@@ -318,15 +331,14 @@ namespace detail
318331
if (!current)
319332
return;
320333

321-
static bool fence = false;
322-
{
323-
esp8266::InterruptLock lockAllInterruptsInThisScope;
324-
325-
// prevent recursive calls
326-
if (fence)
327-
return;
328-
fence = true;
329-
}
334+
static std::atomic<bool> fence(false);
335+
// prevent recursive calls
336+
#ifdef ESP8266
337+
if (fence.load()) return;
338+
fence.store(true);
339+
#else
340+
if (fence.exchange(true)) return;
341+
#endif
330342

331343
Node_t* prev = nullptr;
332344
// prevent execution of new callbacks during this run
@@ -339,7 +351,7 @@ namespace detail
339351
if (!Call<Delegate, R, ISQUEUE>::execute(current->mDelegate))
340352
{
341353
// remove callback from stack
342-
esp8266::InterruptLock lockAllInterruptsInThisScope;
354+
InterruptLock lockAllInterruptsInThisScope;
343355

344356
auto to_recycle = current;
345357

@@ -369,7 +381,7 @@ namespace detail
369381
optimistic_yield(10000);
370382
} while (current && !done);
371383

372-
fence = false;
384+
fence.store(false);
373385
}
374386
};
375387

0 commit comments

Comments
 (0)
0