Use C++11 atomic

This commit is contained in:
Henry Gabryjelski committed 2020-08-11 22:24:37 -07:00
1 parent a566c693d9
commit e99b5ea40e
2 files changed
+8 -7

No files matched your search

+6 -6
View File
@@ -121,9 +121,9 @@ bool HardwarePWM::takeOwnership(uintptr_t token)
// TODO: warn, but do not fail, if taking ownership with IRQs already enabled
// NVIC_GetActive
// use gcc built-in intrinsic to ensure atomicity
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
return __sync_bool_compare_and_swap(&(this->_owner_token), 0, token);
// Use C++11 atomic CAS operation
uintptr_t newValue = 0U;
return this->_owner_token.compare_exchange_strong(newValue, token);
}
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
bool HardwarePWM::releaseOwnership(uintptr_t token)
@@ -156,9 +156,8 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
// TODO: warn, but do not fail, if releasing ownership with IRQs enabled
// NVIC_GetActive
// use gcc built-in intrinsic to ensure atomicity
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
bool result = __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);
// Use C++11 atomic CAS operation
bool result = this->_owner_token.compare_exchange_strong(token, 0U);
if (!result) {
LOG_LV1("HwPWM", "race condition resulted in failure to acquire ownership");
}
@@ -168,6 +167,7 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm) :
_pwm(pwm)
{
_owner_token = 0U;
arrclr(_seq0);
_max_value = 255;
+2 -1
View File
@@ -38,6 +38,7 @@
#include "common_inc.h"
#include "nrf.h"
#include <atomic>
#ifdef NRF52840_XXAA
#define HWPWM_MODULE_NUM 4
@@ -50,7 +51,7 @@ class HardwarePWM
private:
enum { MAX_CHANNELS = 4 }; // Max channel per group
NRF_PWM_Type * const _pwm;
volatile uintptr_t _owner_token = 0;
std::atomic_uintptr_t _owner_token;
uint16_t _seq0[MAX_CHANNELS];