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 dest.update (block);
57}
58
59void UpdateQueue::pushUpdate (juce::MemoryBlock&& update)
60{
61 // push the update data onto the queue
62 {
63 const juce::ScopedLock lock { mutex };
64 queue.push_back (update);
65 }
66 if (destThread == nullptr)
67 juce::MessageManager::callAsync (
68 [this] ()
69 {
70 jassert (juce::MessageManager::existsAndIsCurrentThread ());
71 performAllUpdates ();
72 });
73 else
74 // wake the consumer thread up if it's waiting. It's the duty
75 // of that thread to call either `performNextUpdate()` (iterating through
76 // pending updates on its own) or `performAllUpdates()` to apply any
77 // pending changes waiting in the queue.
78 destThread->notify ();
79}
80
81Sync::Sync (Object& producer, Object& consumer, juce::Thread* thread)
82: UpdateQueue (consumer, thread)
83, juce::ValueTreeSynchroniser { producer }
84{
85 // cannot sync to yourself!
86 jassert (static_cast<juce::ValueTree> (producer) !=
87 static_cast<juce::ValueTree> (consumer));
88}
89
90void Sync::stateChanged (const void* encodedChange, size_t encodedChangeSize)
91{
92 pushUpdate (juce::MemoryBlock (encodedChange, encodedChangeSize));
93}
94
95} // namespace cello
96
97#if RUN_UNIT_TESTS
98#include "test/test_cello_sync.inl"
99#endif
Definition cello_object.h:34
Sync(Object &producer, Object &consumer, juce::Thread *thread)
Construct a new Sync object.
Definition cello_sync.cpp:81
void performAllUpdates()
Execute each of the updates that are ready.
Definition cello_sync.cpp:38
int getPendingUpdateCount() const
Definition cello_sync.cpp:32
void performNextUpdate()
Pop the next event from the queue and apply the change to the destination value tree.
Definition cello_sync.cpp:44