cello
JUCE ValueTrees for Humans
Loading...
Searching...
No Matches
cello_sync.cpp
1/*
2 Copyright (c) 2023 Brett g Porter
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in all
10 copies or substantial portions of the Software.
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 SOFTWARE.
18*/
19
20#include "cello_sync.h"
21#include "cello_object.h"
22
23namespace cello
24{
25
26UpdateQueue::UpdateQueue (Object& consumer, juce::Thread* thread)
27: dest (consumer)
28, destThread (thread)
29{
30}
31
33{
34 const juce::ScopedLock lock { mutex };
35 return static_cast<int> (queue.size ());
36}
37
43
45{
46 if (getPendingUpdateCount () == 0)
47 return;
48
49 // lock the queue and get the block at its head
50 juce::MemoryBlock block;
51 {
52 const juce::ScopedLock lock { mutex };
53 block = std::move (queue.front ());
54 queue.pop_front ();
55 }
56
57 startUpdate (block.getData (), block.getSize ());
58 dest.update (block);
59 endUpdate ();
60}
61
62void UpdateQueue::pushUpdate (juce::MemoryBlock&& update)
63{
64 // push the update data onto the queue
65 {
66 const juce::ScopedLock lock { mutex };
67 queue.push_back (update);
68 }
69 if (destThread == nullptr)
70 juce::MessageManager::callAsync (
71 [this] ()
72 {
73 jassert (juce::MessageManager::existsAndIsCurrentThread ());
74 performAllUpdates ();
75 });
76 else
77 // wake the consumer thread up if it's waiting. It's the duty
78 // of that thread to call either `performNextUpdate()` (iterating through
79 // pending updates on its own) or `performAllUpdates()` to apply any
80 // pending changes waiting in the queue.
81 destThread->notify ();
82}
83
84//
86//
87
88Sync::Sync (Object& producer, Object& consumer, juce::Thread* thread, SyncController* controller)
89: UpdateQueue (consumer, thread)
90, juce::ValueTreeSynchroniser { producer }
91, controller (controller)
92{
93 // cannot sync to yourself!
94 jassert (static_cast<juce::ValueTree> (producer) != static_cast<juce::ValueTree> (consumer));
95}
96
97void Sync::stateChanged (const void* encodedChange, size_t encodedChangeSize)
98{
99 if (controller != nullptr)
100 {
101 if (!controller->shouldHandleUpdate (this, (char*)encodedChange, encodedChangeSize))
102 return;
103 }
104
105 pushUpdate (juce::MemoryBlock (encodedChange, encodedChangeSize));
106}
107
108void Sync::startUpdate (void* data, size_t size)
109{
110 if (controller != nullptr)
111 controller->startUpdate (this, data, size);
112}
113
114
115void Sync::endUpdate ()
116{
117 if (controller != nullptr)
118 controller->endUpdate (this);
119}
120
121//
123//
124
125SyncController::SyncController (Object& obj1, juce::Thread* thread1, Object& obj2, juce::Thread* thread2)
126: sync1to2 (obj1, obj2, thread2, this)
127, sync2to1 (obj2, obj1, thread1, this)
128{
129 jassert (thread2 != thread1);
130}
131
132void SyncController::startUpdate (Sync* sync, void* data, size_t size)
133{
134 if (sync == &sync1to2)
135 update1to2 = SyncData ((char*)data, size);
136 else if (sync == &sync2to1)
137 update2to1 = SyncData ((char*)data, size);
138 else
139 jassertfalse;
140}
141
142void SyncController::endUpdate (Sync* sync)
143{
144 if (sync == &sync1to2)
145 update1to2.data = {};
146 else if (sync == &sync2to1)
147 update2to1.data = {};
148 else
149 jassertfalse;
150}
151
152bool SyncController::shouldHandleUpdate (Sync* sync, void* data, size_t size) const
153{
154 if (sync == &sync1to2)
155 return update2to1 != SyncData { data, size };
156 else if (sync == &sync2to1)
157 return update1to2 != SyncData { data, size };
158 else
159 jassertfalse;
160 return false;
161}
162
163void SyncController::performNextUpdate (juce::Thread* thread)
164{
165 // This method should perform the next update for the specified thread
166 if (sync1to2.isDestinationThread (thread))
167 sync1to2.performNextUpdate ();
168 else if (sync2to1.isDestinationThread (thread))
169 sync2to1.performNextUpdate ();
170 else
171 jassertfalse;
172}
173
174void SyncController::performAllUpdates (juce::Thread* thread)
175{
176 // This method should perform all updates for the specified thread
177 if (sync1to2.isDestinationThread (thread))
178 sync1to2.performAllUpdates ();
179 else if (sync2to1.isDestinationThread (thread))
180 sync2to1.performAllUpdates ();
181 else
182 jassertfalse;
183}
184} // namespace cello
185
186#if RUN_UNIT_TESTS
187#include "test/test_cello_sync.inl"
188#endif
Definition cello_object.h:34
Class to manage bi-directional sync between two Objects in different threads, preventing feedback loo...
Definition cello_sync.h:176
void performNextUpdate(juce::Thread *thread)
Perform the next update for the given thread.
Definition cello_sync.cpp:163
SyncController(Object &obj1, juce::Thread *threadForObj1, Object &obj2, juce::Thread *threadForObj2)
Construct a new SyncController object.
Definition cello_sync.cpp:125
void performAllUpdates(juce::Thread *thread)
Perform all updates for the given thread.
Definition cello_sync.cpp:174
Permits thread-safe Object updates by using the juce::ValueTreeSynchroniser class to generate small b...
Definition cello_sync.h:133
Sync(Object &producer, Object &consumer, juce::Thread *thread, SyncController *controller=nullptr)
Construct a new Sync object.
Definition cello_sync.cpp:88
void performAllUpdates()
Execute each of the updates that are ready.
Definition cello_sync.cpp:38
virtual void endUpdate()=0
Called when the update is complete. clear the update data.
int getPendingUpdateCount() const
Definition cello_sync.cpp:32
virtual void startUpdate(void *data, size_t size)=0
Called when a new update is pushed onto the queue. We use this to prevent feedback loops.
void performNextUpdate()
Pop the next event from the queue and apply the change to the destination value tree.
Definition cello_sync.cpp:44
Data structure for holding synchronization update information.
Definition cello_sync.h:109