20#include "cello_sync.h"
21#include "cello_object.h"
26UpdateQueue::UpdateQueue (
Object& consumer, juce::Thread* thread)
34 const juce::ScopedLock lock { mutex };
35 return static_cast<int> (queue.size ());
50 juce::MemoryBlock block;
52 const juce::ScopedLock lock { mutex };
53 block = std::move (queue.front ());
62void UpdateQueue::pushUpdate (juce::MemoryBlock&& update)
66 const juce::ScopedLock lock { mutex };
67 queue.push_back (update);
69 if (destThread ==
nullptr)
70 juce::MessageManager::callAsync (
73 jassert (juce::MessageManager::existsAndIsCurrentThread ());
81 destThread->notify ();
89: UpdateQueue (consumer, thread)
90, juce::ValueTreeSynchroniser { producer }
91, controller (controller)
94 jassert (
static_cast<juce::ValueTree
> (producer) !=
static_cast<juce::ValueTree
> (consumer));
97void Sync::stateChanged (
const void* encodedChange,
size_t encodedChangeSize)
99 if (controller !=
nullptr)
101 if (!controller->shouldHandleUpdate (
this, (
char*)encodedChange, encodedChangeSize))
105 pushUpdate (juce::MemoryBlock (encodedChange, encodedChangeSize));
108void Sync::startUpdate (
void* data,
size_t size)
110 if (controller !=
nullptr)
111 controller->startUpdate (
this, data, size);
115void Sync::endUpdate ()
117 if (controller !=
nullptr)
118 controller->endUpdate (
this);
126: sync1to2 (obj1, obj2, thread2, this)
127, sync2to1 (obj2, obj1, thread1, this)
129 jassert (thread2 != thread1);
132void SyncController::startUpdate (
Sync* sync,
void* data,
size_t size)
134 if (sync == &sync1to2)
135 update1to2 =
SyncData ((
char*)data, size);
136 else if (sync == &sync2to1)
137 update2to1 =
SyncData ((
char*)data, size);
142void SyncController::endUpdate (Sync* sync)
144 if (sync == &sync1to2)
145 update1to2.data = {};
146 else if (sync == &sync2to1)
147 update2to1.data = {};
152bool SyncController::shouldHandleUpdate (
Sync* sync,
void* data,
size_t size)
const
154 if (sync == &sync1to2)
155 return update2to1 != SyncData { data, size };
156 else if (sync == &sync2to1)
157 return update1to2 != SyncData { data, size };
166 if (sync1to2.isDestinationThread (thread))
167 sync1to2.performNextUpdate ();
168 else if (sync2to1.isDestinationThread (thread))
169 sync2to1.performNextUpdate ();
177 if (sync1to2.isDestinationThread (thread))
178 sync1to2.performAllUpdates ();
179 else if (sync2to1.isDestinationThread (thread))
180 sync2to1.performAllUpdates ();
187#include "test/test_cello_sync.inl"
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