[glass] DataSource: Add spinlock to protect value (#4771)

A lock is needed here due to HAL async callbacks.
This commit is contained in:
Peter Johnson
2022-12-06 21:09:14 -08:00
committed by GitHub
parent 348bd107fc
commit 2d0faecf4f

View File

@@ -11,6 +11,7 @@
#include <imgui.h>
#include <wpi/Signal.h>
#include <wpi/spinlock.h>
namespace glass {
@@ -37,12 +38,21 @@ class DataSource {
bool IsDigital() const { return m_digital; }
void SetValue(double value, int64_t time = 0) {
std::scoped_lock lock{m_valueMutex};
m_value = value;
m_valueTime = time;
valueChanged(value, time);
}
double GetValue() const { return m_value; }
int64_t GetValueTime() const { return m_valueTime; }
double GetValue() const {
std::scoped_lock lock{m_valueMutex};
return m_value;
}
int64_t GetValueTime() const {
std::scoped_lock lock{m_valueMutex};
return m_valueTime;
}
// drag source helpers
void LabelText(const char* label, const char* fmt, ...) const IM_FMTARGS(3);
@@ -59,7 +69,7 @@ class DataSource {
ImGuiInputTextFlags flags = 0) const;
void EmitDrag(ImGuiDragDropFlags flags = 0) const;
wpi::sig::Signal<double, int64_t> valueChanged;
wpi::sig::SignalBase<wpi::spinlock, double, int64_t> valueChanged;
static DataSource* Find(std::string_view id);
@@ -69,6 +79,7 @@ class DataSource {
std::string m_id;
std::string& m_name;
bool m_digital = false;
mutable wpi::spinlock m_valueMutex;
double m_value = 0;
int64_t m_valueTime = 0;
};