Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
realtime_property.h
1// SPDX-FileCopyrightText: © 2024 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5#include <atomic>
6
7#include <QObject>
8
13{
14public:
15 virtual ~IRealtimeProperty () = default;
16
21 virtual bool processUpdates () = 0;
22};
23
29template <typename T> class RealtimeProperty : public IRealtimeProperty
30{
31public:
32 RealtimeProperty (T initial = T{}) : value_ (initial), pending_ (initial) { }
33
34 // Real-time safe setter
35 void setRT (const T &newValue)
36 {
37 pending_.store (newValue, std::memory_order_release);
38 has_update_.store (true, std::memory_order_release);
39 }
40
41 // Real-time safe getter
42 T getRT () const { return pending_.load (std::memory_order_acquire); }
43
44 // Non-RT getter (for main thread/property system)
45 T get () const { return value_; }
46
47 // Non-RT setter
48 void set (const T &newValue)
49 {
50 value_ = newValue;
51 pending_.store (newValue, std::memory_order_release);
52 has_update_.store (false, std::memory_order_release);
53 }
54
55 bool processUpdates () override
56 {
57 if (!has_update_.load (std::memory_order_acquire))
58 {
59 return false;
60 }
61
62 value_ = pending_.load (std::memory_order_acquire);
63 has_update_.store (false, std::memory_order_release);
64 return true;
65 }
66
67private:
68 T value_; // Main thread value
69 std::atomic<T> pending_; // Real-time thread value
70 std::atomic<bool> has_update_{ false };
71};
Interface for real-time property updates.
virtual bool processUpdates()=0
Process pending updates from real-time thread.
bool processUpdates() override
Process pending updates from real-time thread.