@@ -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
129129bool 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)
168167HardwarePWM::HardwarePWM (NRF_PWM_Type* pwm) :
169168 _pwm(pwm)
170169{
170+ _owner_token = 0U ;
171171 arrclr (_seq0);
172172
173173 _max_value = 255 ;
0 commit comments