From 2c27d8bc756fd4b134aa16ef9901734e1509062b Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 3 Dec 2023 14:18:32 -0800 Subject: Make the device clock members atomic Even though they're protected by a SeqLock of sorts, it's still UB to read and write non-atomic vars from different threads. It's fine to do relaxed reads and writes given the lock though, to help alleviate the cost. --- core/device.cpp | 3 +++ core/device.h | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/device.cpp b/core/device.cpp index 2766c5e4..a5edf63c 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -9,6 +9,9 @@ #include "mastering.h" +static_assert(std::atomic::is_always_lock_free); + + al::FlexArray DeviceBase::sEmptyContextArray{0u}; diff --git a/core/device.h b/core/device.h index 8abbef21..842f1d82 100644 --- a/core/device.h +++ b/core/device.h @@ -218,8 +218,8 @@ struct DeviceBase { */ NfcFilter mNFCtrlFilter{}; - uint SamplesDone{0u}; - std::chrono::nanoseconds ClockBase{0}; + std::atomic mSamplesDone{0u}; + std::atomic mClockBase{std::chrono::nanoseconds{}}; std::chrono::nanoseconds FixedLatency{0}; AmbiRotateMatrix mAmbiRotateMatrix{}; @@ -307,6 +307,20 @@ struct DeviceBase { return refcount; } + /** + * Helper to get the current clock time from the device's ClockBase, and + * SamplesDone converted from the sample rate. Should only be called while + * watching the MixCount. + */ + std::chrono::nanoseconds getClockTime() const noexcept + { + using std::chrono::seconds; + using std::chrono::nanoseconds; + + auto ns = nanoseconds{seconds{mSamplesDone.load(std::memory_order_relaxed)}} / Frequency; + return mClockBase.load(std::memory_order_relaxed) + ns; + } + void ProcessHrtf(const size_t SamplesToDo); void ProcessAmbiDec(const size_t SamplesToDo); void ProcessAmbiDecStablized(const size_t SamplesToDo); -- cgit v1.2.3