mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21: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,80 +7,35 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/spinlock.h>
|
||||
|
||||
#include "mockdata/DriverStationData.h"
|
||||
#include "mockdata/NotifyListenerVector.h"
|
||||
#include "mockdata/SimDataValue.h"
|
||||
|
||||
namespace hal {
|
||||
struct JoystickOutputStore;
|
||||
|
||||
class DriverStationData {
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Enabled)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Autonomous)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Test)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(EStop)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(FmsAttached)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(DsAttached)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(AllianceStationId)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(MatchTime)
|
||||
|
||||
static LLVM_ATTRIBUTE_ALWAYS_INLINE HAL_Value
|
||||
MakeAllianceStationIdValue(HAL_AllianceStationID value) {
|
||||
return MakeEnum(value);
|
||||
}
|
||||
|
||||
public:
|
||||
DriverStationData();
|
||||
void ResetData();
|
||||
|
||||
int32_t RegisterEnabledCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelEnabledCallback(int32_t uid);
|
||||
void InvokeEnabledCallback(HAL_Value value);
|
||||
HAL_Bool GetEnabled();
|
||||
void SetEnabled(HAL_Bool enabled);
|
||||
|
||||
int32_t RegisterAutonomousCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelAutonomousCallback(int32_t uid);
|
||||
void InvokeAutonomousCallback(HAL_Value value);
|
||||
HAL_Bool GetAutonomous();
|
||||
void SetAutonomous(HAL_Bool autonomous);
|
||||
|
||||
int32_t RegisterTestCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelTestCallback(int32_t uid);
|
||||
void InvokeTestCallback(HAL_Value value);
|
||||
HAL_Bool GetTest();
|
||||
void SetTest(HAL_Bool test);
|
||||
|
||||
int32_t RegisterEStopCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelEStopCallback(int32_t uid);
|
||||
void InvokeEStopCallback(HAL_Value value);
|
||||
HAL_Bool GetEStop();
|
||||
void SetEStop(HAL_Bool eStop);
|
||||
|
||||
int32_t RegisterFmsAttachedCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelFmsAttachedCallback(int32_t uid);
|
||||
void InvokeFmsAttachedCallback(HAL_Value value);
|
||||
HAL_Bool GetFmsAttached();
|
||||
void SetFmsAttached(HAL_Bool fmsAttached);
|
||||
|
||||
int32_t RegisterDsAttachedCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelDsAttachedCallback(int32_t uid);
|
||||
void InvokeDsAttachedCallback(HAL_Value value);
|
||||
HAL_Bool GetDsAttached();
|
||||
void SetDsAttached(HAL_Bool dsAttached);
|
||||
|
||||
int32_t RegisterAllianceStationIdCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelAllianceStationIdCallback(int32_t uid);
|
||||
void InvokeAllianceStationIdCallback(HAL_Value value);
|
||||
HAL_AllianceStationID GetAllianceStationId();
|
||||
void SetAllianceStationId(HAL_AllianceStationID allianceStationId);
|
||||
|
||||
int32_t RegisterMatchTimeCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelMatchTimeCallback(int32_t uid);
|
||||
void InvokeMatchTimeCallback(HAL_Value value);
|
||||
double GetMatchTime();
|
||||
void SetMatchTime(double matchTime);
|
||||
|
||||
void GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes);
|
||||
void GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs);
|
||||
void GetJoystickButtons(int32_t joystickNum, HAL_JoystickButtons* buttons);
|
||||
@@ -103,28 +58,20 @@ class DriverStationData {
|
||||
|
||||
void NotifyNewData();
|
||||
|
||||
private:
|
||||
wpi::mutex m_registerMutex;
|
||||
std::atomic<HAL_Bool> m_enabled{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_enabledCallbacks = nullptr;
|
||||
std::atomic<HAL_Bool> m_autonomous{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_autonomousCallbacks = nullptr;
|
||||
std::atomic<HAL_Bool> m_test{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_testCallbacks = nullptr;
|
||||
std::atomic<HAL_Bool> m_eStop{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_eStopCallbacks = nullptr;
|
||||
std::atomic<HAL_Bool> m_fmsAttached{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_fmsAttachedCallbacks = nullptr;
|
||||
std::atomic<HAL_Bool> m_dsAttached{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_dsAttachedCallbacks = nullptr;
|
||||
std::atomic<HAL_AllianceStationID> m_allianceStationId{
|
||||
static_cast<HAL_AllianceStationID>(0)};
|
||||
std::shared_ptr<NotifyListenerVector> m_allianceStationIdCallbacks = nullptr;
|
||||
std::atomic<double> m_matchTime{0.0};
|
||||
std::shared_ptr<NotifyListenerVector> m_matchTimeCallbacks = nullptr;
|
||||
SimDataValue<HAL_Bool, MakeBoolean, GetEnabledName> enabled{false};
|
||||
SimDataValue<HAL_Bool, MakeBoolean, GetAutonomousName> autonomous{false};
|
||||
SimDataValue<HAL_Bool, MakeBoolean, GetTestName> test{false};
|
||||
SimDataValue<HAL_Bool, MakeBoolean, GetEStopName> eStop{false};
|
||||
SimDataValue<HAL_Bool, MakeBoolean, GetFmsAttachedName> fmsAttached{false};
|
||||
SimDataValue<HAL_Bool, MakeBoolean, GetDsAttachedName> dsAttached{false};
|
||||
SimDataValue<HAL_AllianceStationID, MakeAllianceStationIdValue,
|
||||
GetAllianceStationIdName>
|
||||
allianceStationId{static_cast<HAL_AllianceStationID>(0)};
|
||||
SimDataValue<double, MakeDouble, GetMatchTimeName> matchTime{0.0};
|
||||
|
||||
wpi::mutex m_joystickDataMutex;
|
||||
wpi::mutex m_matchInfoMutex;
|
||||
private:
|
||||
wpi::spinlock m_joystickDataMutex;
|
||||
wpi::spinlock m_matchInfoMutex;
|
||||
|
||||
std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
|
||||
std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
|
||||
|
||||
Reference in New Issue
Block a user