Skip to content

Commit 176f1e4

Browse files
author
dave
committed
#22 fixed up method of including std::atomic features.
* removed from inside a namespace definition * moved to using latest atomic definition header.
1 parent 22db92e commit 176f1e4

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/TaskPlatformDeps.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ namespace tm_internal {
8080
typedef uint8_t pintype_t;
8181
# define IOA_USE_ARDUINO
8282

83-
namespace tm_internal {
8483

8584
#if defined(ESP8266)
86-
#include "stdatomic.h"
87-
typedef _Atomic(TimerTask*) TimerTaskAtomicPtr;
85+
#include <atomic>
86+
namespace tm_internal {
87+
88+
typedef std::atomic<TimerTask *> TimerTaskAtomicPtr;
89+
90+
typedef std::atomic<uint32_t> TmAtomicBool;
8891

89-
typedef atomic_bool TmAtomicBool;
9092
/**
9193
* Sets the boolean to the new value ONLY when the existing value matches expected.
9294
* @param ptr the bool memory location to compare / swap
@@ -95,10 +97,11 @@ namespace tm_internal {
9597
* @return true if the replacement was done, otherwise false
9698
*/
9799
inline bool atomicSwapBool(TmAtomicBool *ptr, bool expected, bool newValue) {
100+
// compare and swap is not implemented on ESP8266
98101
auto ret = false;
99102
noInterrupts();
100-
if(atomic_load(ptr) == expected) {
101-
atomic_store(ptr, newValue);
103+
if(ptr->load() == expected) {
104+
ptr->store(newValue);
102105
ret = true;
103106
}
104107
interrupts();
@@ -111,7 +114,7 @@ namespace tm_internal {
111114
* @return the boolean value.
112115
*/
113116
inline bool atomicReadBool(TmAtomicBool *pPtr) {
114-
return atomic_load(pPtr);
117+
return pPtr->load();
115118
}
116119

117120
/**
@@ -120,7 +123,7 @@ namespace tm_internal {
120123
* @param newVal the new value
121124
*/
122125
inline void atomicWriteBool(TmAtomicBool *pPtr, bool newVal) {
123-
atomic_store(pPtr, newVal);
126+
pPtr->store(newVal);
124127
}
125128

126129
/**
@@ -131,7 +134,7 @@ namespace tm_internal {
131134
* @return the pointer.
132135
*/
133136
inline TimerTask *atomicReadPtr(TimerTaskAtomicPtr *pPtr) {
134-
return atomic_load(pPtr);
137+
return pPtr->load();
135138
}
136139

137140
/**
@@ -141,9 +144,12 @@ namespace tm_internal {
141144
* @param newValue
142145
*/
143146
inline void atomicWritePtr(TimerTaskAtomicPtr *pPtr, TimerTask *newValue) {
144-
atomic_store(pPtr, newValue);
147+
pPtr->store(newValue);
145148
}
149+
}
146150
#else
151+
namespace tm_internal {
152+
147153
typedef TimerTask* volatile TimerTaskAtomicPtr;
148154
typedef volatile uint32_t TmAtomicBool; // to use CAS, the bool must be 32 bits wide
149155
inline bool atomicSwapBool(TmAtomicBool *ptr, bool expected, bool newValue) {
@@ -191,8 +197,8 @@ namespace tm_internal {
191197
inline void atomicWritePtr(TimerTaskAtomicPtr *pPtr, TimerTask *newValue) {
192198
*pPtr = newValue;
193199
}
194-
#endif
195200
}
201+
#endif
196202

197203
#else
198204
// fall back to using Arduino regular logic, works for all single core boards. If we end up here for a multicore

0 commit comments

Comments
 (0)