@@ -121,9 +121,9 @@ bool HardwarePWM::takeOwnership(uintptr_t token)
121
121
// TODO: warn, but do not fail, if taking ownership with IRQs already enabled
122
122
// NVIC_GetActive
123
123
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);
127
127
}
128
128
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
129
129
bool HardwarePWM::releaseOwnership (uintptr_t token)
@@ -156,9 +156,8 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
156
156
// TODO: warn, but do not fail, if releasing ownership with IRQs enabled
157
157
// NVIC_GetActive
158
158
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 );
162
161
if (!result) {
163
162
LOG_LV1 (" HwPWM" , " race condition resulted in failure to acquire ownership" );
164
163
}
@@ -168,6 +167,7 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
168
167
HardwarePWM::HardwarePWM (NRF_PWM_Type* pwm) :
169
168
_pwm(pwm)
170
169
{
170
+ _owner_token = 0U ;
171
171
arrclr (_seq0);
172
172
173
173
_max_value = 255 ;
0 commit comments