OpenShot Audio Library | OpenShotAudio: juce_LockingAsyncUpdater.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.6.0

Loading...
Searching...
No Matches
juce_LockingAsyncUpdater.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26class LockingAsyncUpdater::Impl : public CallbackMessage
27{
28public:
29 explicit Impl (std::function<void()> cb)
30 : callback (std::move (cb)) {}
31
32 void clear()
33 {
34 const ScopedLock lock (mutex);
35 deliver = false;
36 callback = nullptr;
37 }
38
39 void trigger()
40 {
41 {
42 const ScopedLock lock (mutex);
43
44 if (deliver)
45 return;
46
47 deliver = true;
48 }
49
50 if (! post())
51 cancel();
52 }
53
54 void cancel()
55 {
56 const ScopedLock lock (mutex);
57 deliver = false;
58 }
59
60 bool isPending()
61 {
62 const ScopedLock lock (mutex);
63 return deliver;
64 }
65
66 void messageCallback() override
67 {
68 const ScopedLock lock (mutex);
69
70 if (std::exchange (deliver, false))
71 NullCheckedInvocation::invoke (callback);
72 }
73
74private:
75 CriticalSection mutex;
76 std::function<void()> callback;
77 bool deliver = false;
78};
79
80//==============================================================================
81LockingAsyncUpdater::LockingAsyncUpdater (std::function<void()> callbackToUse)
82 : impl (new Impl (std::move (callbackToUse))) {}
83
84 LockingAsyncUpdater::LockingAsyncUpdater (LockingAsyncUpdater&& other) noexcept
85 : impl (std::exchange (other.impl, nullptr)) {}
86
87 LockingAsyncUpdater& LockingAsyncUpdater::operator= (LockingAsyncUpdater&& other) noexcept
88{
89 LockingAsyncUpdater temp { std::move (other) };
90 std::swap (temp.impl, impl);
91 return *this;
92}
93
94LockingAsyncUpdater::~LockingAsyncUpdater()
95{
96 if (impl != nullptr)
97 impl->clear();
98}
99
100 void LockingAsyncUpdater::triggerAsyncUpdate()
101{
102 if (impl != nullptr)
103 impl->trigger();
104 else
105 jassertfalse; // moved-from!
106}
107
108 void LockingAsyncUpdater::cancelPendingUpdate() noexcept
109{
110 if (impl != nullptr)
111 impl->cancel();
112 else
113 jassertfalse; // moved-from!
114}
115
116 void LockingAsyncUpdater::handleUpdateNowIfNeeded()
117{
118 if (impl != nullptr)
119 impl->messageCallback();
120 else
121 jassertfalse; // moved-from!
122}
123
124 bool LockingAsyncUpdater::isUpdatePending() const noexcept
125{
126 if (impl != nullptr)
127 return impl->isPending();
128
129 jassertfalse; // moved-from!
130 return false;
131}
132
133} // namespace juce
juce::CallbackMessage
Definition juce_CallbackMessage.h:49
juce::CallbackMessage::messageCallback
void messageCallback() override=0
juce::LockingAsyncUpdater::isUpdatePending
bool isUpdatePending() const noexcept
Definition juce_LockingAsyncUpdater.cpp:124
juce::LockingAsyncUpdater::triggerAsyncUpdate
void triggerAsyncUpdate()
Definition juce_LockingAsyncUpdater.cpp:100
juce::LockingAsyncUpdater::operator=
LockingAsyncUpdater & operator=(LockingAsyncUpdater &&other) noexcept
Definition juce_LockingAsyncUpdater.cpp:87
juce::LockingAsyncUpdater::cancelPendingUpdate
void cancelPendingUpdate() noexcept
Definition juce_LockingAsyncUpdater.cpp:108
juce::LockingAsyncUpdater::~LockingAsyncUpdater
~LockingAsyncUpdater()
Definition juce_LockingAsyncUpdater.cpp:94
juce::LockingAsyncUpdater::handleUpdateNowIfNeeded
void handleUpdateNowIfNeeded()
Definition juce_LockingAsyncUpdater.cpp:116
juce::LockingAsyncUpdater::LockingAsyncUpdater
LockingAsyncUpdater(std::function< void()> callbackToUse)
Definition juce_LockingAsyncUpdater.cpp:81