Skip to content

Commit e99b5ea

Browse files
committed
Use C++11 atomic
1 parent a566c69 commit e99b5ea

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

cores/nRF5/HardwarePWM.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ bool HardwarePWM::takeOwnership(uintptr_t token)
121121
// TODO: warn, but do not fail, if taking ownership with IRQs already enabled
122122
// NVIC_GetActive
123123

124-
// use gcc built-in intrinsic to ensure atomicity
125-
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
126-
return __sync_bool_compare_and_swap(&(this->_owner_token), 0, token);
124+
// Use C++11 atomic CAS operation
125+
uintptr_t newValue = 0U;
126+
return this->_owner_token.compare_exchange_strong(newValue, token);
127127
}
128128
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
129129
bool HardwarePWM::releaseOwnership(uintptr_t token)
@@ -156,9 +156,8 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
156156
// TODO: warn, but do not fail, if releasing ownership with IRQs enabled
157157
// NVIC_GetActive
158158

159-
// use gcc built-in intrinsic to ensure atomicity
160-
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
161-
bool result = __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);
159+
// Use C++11 atomic CAS operation
160+
bool result = this->_owner_token.compare_exchange_strong(token, 0U);
162161
if (!result) {
163162
LOG_LV1("HwPWM", "race condition resulted in failure to acquire ownership");
164163
}
@@ -168,6 +167,7 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
168167
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm) :
169168
_pwm(pwm)
170169
{
170+
_owner_token = 0U;
171171
arrclr(_seq0);
172172

173173
_max_value = 255;

cores/nRF5/HardwarePWM.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "common_inc.h"
4040
#include "nrf.h"
41+
#include <atomic>
4142

4243
#ifdef NRF52840_XXAA
4344
#define HWPWM_MODULE_NUM 4
@@ -50,7 +51,7 @@ class HardwarePWM
5051
private:
5152
enum { MAX_CHANNELS = 4 }; // Max channel per group
5253
NRF_PWM_Type * const _pwm;
53-
volatile uintptr_t _owner_token = 0;
54+
std::atomic_uintptr_t _owner_token;
5455

5556
uint16_t _seq0[MAX_CHANNELS];
5657

0 commit comments

Comments
 (0)