mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
Change hal sim to use spinlocks (#1291)
This makes callback registration completely thread safe. This patch also uses templates and macros to dramatically reduce the amount of manual boilerplate.
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
|
||||
#include "../PortsInternal.h"
|
||||
#include "AccelerometerDataInternal.h"
|
||||
#include "mockdata/NotifyCallbackHelpers.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
@@ -22,196 +21,11 @@ void InitializeAccelerometerData() {
|
||||
|
||||
AccelerometerData* hal::SimAccelerometerData;
|
||||
void AccelerometerData::ResetData() {
|
||||
m_active = false;
|
||||
m_activeCallbacks = nullptr;
|
||||
m_range = static_cast<HAL_AccelerometerRange>(0);
|
||||
m_rangeCallbacks = nullptr;
|
||||
m_x = 0.0;
|
||||
m_xCallbacks = nullptr;
|
||||
m_y = 0.0;
|
||||
m_yCallbacks = nullptr;
|
||||
m_z = 0.0;
|
||||
m_zCallbacks = nullptr;
|
||||
}
|
||||
|
||||
int32_t AccelerometerData::RegisterActiveCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<wpi::mutex> lock(m_registerMutex);
|
||||
m_activeCallbacks =
|
||||
RegisterCallback(m_activeCallbacks, "Active", callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeBoolean(GetActive());
|
||||
callback("Active", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void AccelerometerData::CancelActiveCallback(int32_t uid) {
|
||||
m_activeCallbacks = CancelCallback(m_activeCallbacks, uid);
|
||||
}
|
||||
|
||||
void AccelerometerData::InvokeActiveCallback(HAL_Value value) {
|
||||
InvokeCallback(m_activeCallbacks, "Active", &value);
|
||||
}
|
||||
|
||||
HAL_Bool AccelerometerData::GetActive() { return m_active; }
|
||||
|
||||
void AccelerometerData::SetActive(HAL_Bool active) {
|
||||
HAL_Bool oldValue = m_active.exchange(active);
|
||||
if (oldValue != active) {
|
||||
InvokeActiveCallback(MakeBoolean(active));
|
||||
}
|
||||
}
|
||||
|
||||
int32_t AccelerometerData::RegisterRangeCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<wpi::mutex> lock(m_registerMutex);
|
||||
m_rangeCallbacks =
|
||||
RegisterCallback(m_rangeCallbacks, "Range", callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeEnum(GetRange());
|
||||
callback("Range", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void AccelerometerData::CancelRangeCallback(int32_t uid) {
|
||||
m_rangeCallbacks = CancelCallback(m_rangeCallbacks, uid);
|
||||
}
|
||||
|
||||
void AccelerometerData::InvokeRangeCallback(HAL_Value value) {
|
||||
InvokeCallback(m_rangeCallbacks, "Range", &value);
|
||||
}
|
||||
|
||||
HAL_AccelerometerRange AccelerometerData::GetRange() { return m_range; }
|
||||
|
||||
void AccelerometerData::SetRange(HAL_AccelerometerRange range) {
|
||||
HAL_AccelerometerRange oldValue = m_range.exchange(range);
|
||||
if (oldValue != range) {
|
||||
InvokeRangeCallback(MakeEnum(range));
|
||||
}
|
||||
}
|
||||
|
||||
int32_t AccelerometerData::RegisterXCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<wpi::mutex> lock(m_registerMutex);
|
||||
m_xCallbacks =
|
||||
RegisterCallback(m_xCallbacks, "X", callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeDouble(GetX());
|
||||
callback("X", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void AccelerometerData::CancelXCallback(int32_t uid) {
|
||||
m_xCallbacks = CancelCallback(m_xCallbacks, uid);
|
||||
}
|
||||
|
||||
void AccelerometerData::InvokeXCallback(HAL_Value value) {
|
||||
InvokeCallback(m_xCallbacks, "X", &value);
|
||||
}
|
||||
|
||||
double AccelerometerData::GetX() { return m_x; }
|
||||
|
||||
void AccelerometerData::SetX(double x) {
|
||||
double oldValue = m_x.exchange(x);
|
||||
if (oldValue != x) {
|
||||
InvokeXCallback(MakeDouble(x));
|
||||
}
|
||||
}
|
||||
|
||||
int32_t AccelerometerData::RegisterYCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<wpi::mutex> lock(m_registerMutex);
|
||||
m_yCallbacks =
|
||||
RegisterCallback(m_yCallbacks, "Y", callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeDouble(GetY());
|
||||
callback("Y", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void AccelerometerData::CancelYCallback(int32_t uid) {
|
||||
m_yCallbacks = CancelCallback(m_yCallbacks, uid);
|
||||
}
|
||||
|
||||
void AccelerometerData::InvokeYCallback(HAL_Value value) {
|
||||
InvokeCallback(m_yCallbacks, "Y", &value);
|
||||
}
|
||||
|
||||
double AccelerometerData::GetY() { return m_y; }
|
||||
|
||||
void AccelerometerData::SetY(double y) {
|
||||
double oldValue = m_y.exchange(y);
|
||||
if (oldValue != y) {
|
||||
InvokeYCallback(MakeDouble(y));
|
||||
}
|
||||
}
|
||||
|
||||
int32_t AccelerometerData::RegisterZCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<wpi::mutex> lock(m_registerMutex);
|
||||
m_zCallbacks =
|
||||
RegisterCallback(m_zCallbacks, "Z", callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeDouble(GetZ());
|
||||
callback("Z", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void AccelerometerData::CancelZCallback(int32_t uid) {
|
||||
m_zCallbacks = CancelCallback(m_zCallbacks, uid);
|
||||
}
|
||||
|
||||
void AccelerometerData::InvokeZCallback(HAL_Value value) {
|
||||
InvokeCallback(m_zCallbacks, "Z", &value);
|
||||
}
|
||||
|
||||
double AccelerometerData::GetZ() { return m_z; }
|
||||
|
||||
void AccelerometerData::SetZ(double z) {
|
||||
double oldValue = m_z.exchange(z);
|
||||
if (oldValue != z) {
|
||||
InvokeZCallback(MakeDouble(z));
|
||||
}
|
||||
active.Reset(false);
|
||||
range.Reset(static_cast<HAL_AccelerometerRange>(0));
|
||||
x.Reset(0.0);
|
||||
y.Reset(0.0);
|
||||
z.Reset(0.0);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
@@ -219,116 +33,28 @@ void HALSIM_ResetAccelerometerData(int32_t index) {
|
||||
SimAccelerometerData[index].ResetData();
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterAccelerometerActiveCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
return SimAccelerometerData[index].RegisterActiveCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, Accelerometer##CAPINAME, \
|
||||
SimAccelerometerData, LOWERNAME)
|
||||
|
||||
void HALSIM_CancelAccelerometerActiveCallback(int32_t index, int32_t uid) {
|
||||
SimAccelerometerData[index].CancelActiveCallback(uid);
|
||||
}
|
||||
DEFINE_CAPI(HAL_Bool, Active, active)
|
||||
DEFINE_CAPI(HAL_AccelerometerRange, Range, range)
|
||||
DEFINE_CAPI(double, X, x)
|
||||
DEFINE_CAPI(double, Y, y)
|
||||
DEFINE_CAPI(double, Z, z)
|
||||
|
||||
HAL_Bool HALSIM_GetAccelerometerActive(int32_t index) {
|
||||
return SimAccelerometerData[index].GetActive();
|
||||
}
|
||||
|
||||
void HALSIM_SetAccelerometerActive(int32_t index, HAL_Bool active) {
|
||||
SimAccelerometerData[index].SetActive(active);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterAccelerometerRangeCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
return SimAccelerometerData[index].RegisterRangeCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
|
||||
void HALSIM_CancelAccelerometerRangeCallback(int32_t index, int32_t uid) {
|
||||
SimAccelerometerData[index].CancelRangeCallback(uid);
|
||||
}
|
||||
|
||||
HAL_AccelerometerRange HALSIM_GetAccelerometerRange(int32_t index) {
|
||||
return SimAccelerometerData[index].GetRange();
|
||||
}
|
||||
|
||||
void HALSIM_SetAccelerometerRange(int32_t index, HAL_AccelerometerRange range) {
|
||||
SimAccelerometerData[index].SetRange(range);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterAccelerometerXCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
return SimAccelerometerData[index].RegisterXCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
|
||||
void HALSIM_CancelAccelerometerXCallback(int32_t index, int32_t uid) {
|
||||
SimAccelerometerData[index].CancelXCallback(uid);
|
||||
}
|
||||
|
||||
double HALSIM_GetAccelerometerX(int32_t index) {
|
||||
return SimAccelerometerData[index].GetX();
|
||||
}
|
||||
|
||||
void HALSIM_SetAccelerometerX(int32_t index, double x) {
|
||||
SimAccelerometerData[index].SetX(x);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterAccelerometerYCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
return SimAccelerometerData[index].RegisterYCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
|
||||
void HALSIM_CancelAccelerometerYCallback(int32_t index, int32_t uid) {
|
||||
SimAccelerometerData[index].CancelYCallback(uid);
|
||||
}
|
||||
|
||||
double HALSIM_GetAccelerometerY(int32_t index) {
|
||||
return SimAccelerometerData[index].GetY();
|
||||
}
|
||||
|
||||
void HALSIM_SetAccelerometerY(int32_t index, double y) {
|
||||
SimAccelerometerData[index].SetY(y);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterAccelerometerZCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
return SimAccelerometerData[index].RegisterZCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
|
||||
void HALSIM_CancelAccelerometerZCallback(int32_t index, int32_t uid) {
|
||||
SimAccelerometerData[index].CancelZCallback(uid);
|
||||
}
|
||||
|
||||
double HALSIM_GetAccelerometerZ(int32_t index) {
|
||||
return SimAccelerometerData[index].GetZ();
|
||||
}
|
||||
|
||||
void HALSIM_SetAccelerometerZ(int32_t index, double z) {
|
||||
SimAccelerometerData[index].SetZ(z);
|
||||
}
|
||||
#define REGISTER(NAME) \
|
||||
SimAccelerometerData[index].NAME.RegisterCallback(callback, param, \
|
||||
initialNotify)
|
||||
|
||||
void HALSIM_RegisterAccelerometerAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimAccelerometerData[index].RegisterActiveCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAccelerometerData[index].RegisterRangeCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAccelerometerData[index].RegisterXCallback(callback, param, initialNotify);
|
||||
SimAccelerometerData[index].RegisterYCallback(callback, param, initialNotify);
|
||||
SimAccelerometerData[index].RegisterZCallback(callback, param, initialNotify);
|
||||
REGISTER(active);
|
||||
REGISTER(range);
|
||||
REGISTER(x);
|
||||
REGISTER(y);
|
||||
REGISTER(z);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
Reference in New Issue
Block a user