diff options
author | Chris Robinson <[email protected]> | 2023-12-03 14:18:32 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-12-03 14:23:31 -0800 |
commit | 2c27d8bc756fd4b134aa16ef9901734e1509062b (patch) | |
tree | e3d982f434cf30ef3a802424c44180ee165cb2c1 /core | |
parent | e6bb91212be93b0b7e4c99c1409f91dd8e211688 (diff) |
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.
Diffstat (limited to 'core')
-rw-r--r-- | core/device.cpp | 3 | ||||
-rw-r--r-- | core/device.h | 18 |
2 files changed, 19 insertions, 2 deletions
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<std::chrono::nanoseconds>::is_always_lock_free); + + al::FlexArray<ContextBase*> 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<uint> mSamplesDone{0u}; + std::atomic<std::chrono::nanoseconds> 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); |