mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +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,48 +7,21 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "mockdata/AnalogGyroData.h"
|
||||
#include "mockdata/NotifyListenerVector.h"
|
||||
#include "mockdata/SimDataValue.h"
|
||||
|
||||
namespace hal {
|
||||
class AnalogGyroData {
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Angle)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Rate)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Initialized)
|
||||
|
||||
public:
|
||||
int32_t RegisterAngleCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelAngleCallback(int32_t uid);
|
||||
void InvokeAngleCallback(HAL_Value value);
|
||||
double GetAngle();
|
||||
void SetAngle(double angle);
|
||||
|
||||
int32_t RegisterRateCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelRateCallback(int32_t uid);
|
||||
void InvokeRateCallback(HAL_Value value);
|
||||
double GetRate();
|
||||
void SetRate(double rate);
|
||||
|
||||
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelInitializedCallback(int32_t uid);
|
||||
void InvokeInitializedCallback(HAL_Value value);
|
||||
HAL_Bool GetInitialized();
|
||||
void SetInitialized(HAL_Bool initialized);
|
||||
SimDataValue<double, MakeDouble, GetAngleName> angle{0.0};
|
||||
SimDataValue<double, MakeDouble, GetRateName> rate{0.0};
|
||||
SimDataValue<HAL_Bool, MakeBoolean, GetInitializedName> initialized{false};
|
||||
|
||||
virtual void ResetData();
|
||||
|
||||
private:
|
||||
wpi::mutex m_registerMutex;
|
||||
std::atomic<double> m_angle{0.0};
|
||||
std::shared_ptr<NotifyListenerVector> m_angleCallbacks = nullptr;
|
||||
std::atomic<double> m_rate{0.0};
|
||||
std::shared_ptr<NotifyListenerVector> m_rateCallbacks = nullptr;
|
||||
std::atomic<HAL_Bool> m_initialized{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
|
||||
};
|
||||
extern AnalogGyroData* SimAnalogGyroData;
|
||||
} // namespace hal
|
||||
|
||||
Reference in New Issue
Block a user