1
1
#ifndef __MULTIDELEGATE_H
2
2
#define __MULTIDELEGATE_H
3
3
4
+ #include < atomic>
5
+ #ifdef ESP8266
4
6
#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
5
19
6
20
namespace detail
7
21
{
@@ -177,7 +191,7 @@ namespace detail
177
191
if (!del)
178
192
return nullptr ;
179
193
180
- esp8266:: InterruptLock lockAllInterruptsInThisScope;
194
+ InterruptLock lockAllInterruptsInThisScope;
181
195
182
196
Node_t* item = get_node_unsafe ();
183
197
if (!item)
@@ -207,7 +221,7 @@ namespace detail
207
221
if (del == ¤t->mDelegate )
208
222
{
209
223
// remove callback from stack
210
- esp8266:: InterruptLock lockAllInterruptsInThisScope;
224
+ InterruptLock lockAllInterruptsInThisScope;
211
225
212
226
auto to_recycle = current;
213
227
@@ -243,15 +257,14 @@ namespace detail
243
257
if (!current)
244
258
return ;
245
259
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
255
268
256
269
Node_t* prev = nullptr ;
257
270
// prevent execution of new callbacks during this run
@@ -264,7 +277,7 @@ namespace detail
264
277
if (!CallP<Delegate, R, ISQUEUE, P...>::execute (current->mDelegate , args...))
265
278
{
266
279
// remove callback from stack
267
- esp8266:: InterruptLock lockAllInterruptsInThisScope;
280
+ InterruptLock lockAllInterruptsInThisScope;
268
281
269
282
auto to_recycle = current;
270
283
@@ -294,7 +307,7 @@ namespace detail
294
307
optimistic_yield (10000 );
295
308
} while (current && !done);
296
309
297
- fence = false ;
310
+ fence. store ( false ) ;
298
311
}
299
312
};
300
313
@@ -318,15 +331,14 @@ namespace detail
318
331
if (!current)
319
332
return ;
320
333
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
330
342
331
343
Node_t* prev = nullptr ;
332
344
// prevent execution of new callbacks during this run
@@ -339,7 +351,7 @@ namespace detail
339
351
if (!Call<Delegate, R, ISQUEUE>::execute (current->mDelegate ))
340
352
{
341
353
// remove callback from stack
342
- esp8266:: InterruptLock lockAllInterruptsInThisScope;
354
+ InterruptLock lockAllInterruptsInThisScope;
343
355
344
356
auto to_recycle = current;
345
357
@@ -369,7 +381,7 @@ namespace detail
369
381
optimistic_yield (10000 );
370
382
} while (current && !done);
371
383
372
- fence = false ;
384
+ fence. store ( false ) ;
373
385
}
374
386
};
375
387
0 commit comments