OpenShot Library | libopenshot-audio  0.2.0
juce_AsyncUpdater.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 
23 namespace juce
24 {
25 
27 {
28 public:
29  AsyncUpdaterMessage (AsyncUpdater& au) : owner (au) {}
30 
31  void messageCallback() override
32  {
33  if (shouldDeliver.compareAndSetBool (0, 1))
34  owner.handleAsyncUpdate();
35  }
36 
37  AsyncUpdater& owner;
38  Atomic<int> shouldDeliver;
39 
40  JUCE_DECLARE_NON_COPYABLE (AsyncUpdaterMessage)
41 };
42 
43 //==============================================================================
45 {
46  activeMessage = *new AsyncUpdaterMessage (*this);
47 }
48 
50 {
51  // You're deleting this object with a background thread while there's an update
52  // pending on the main event thread - that's pretty dodgy threading, as the callback could
53  // happen after this destructor has finished. You should either use a MessageManagerLock while
54  // deleting this object, or find some other way to avoid such a race condition.
55  jassert ((! isUpdatePending())
57  || MessageManager::getInstanceWithoutCreating()->currentThreadHasLockedMessageManager());
58 
59  activeMessage->shouldDeliver.set (0);
60 }
61 
63 {
64  // If you're calling this before (or after) the MessageManager is
65  // running, then you're not going to get any callbacks!
66  JUCE_ASSERT_MESSAGE_MANAGER_EXISTS
67 
68  if (activeMessage->shouldDeliver.compareAndSetBool (1, 0))
69  if (! activeMessage->post())
70  cancelPendingUpdate(); // if the message queue fails, this avoids getting
71  // trapped waiting for the message to arrive
72 }
73 
75 {
76  activeMessage->shouldDeliver.set (0);
77 }
78 
80 {
81  // This can only be called by the event thread.
82  JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
83 
84  if (activeMessage->shouldDeliver.exchange (0) != 0)
86 }
87 
88 bool AsyncUpdater::isUpdatePending() const noexcept
89 {
90  return activeMessage->shouldDeliver.value != 0;
91 }
92 
93 } // namespace juce
juce::AsyncUpdater::AsyncUpdaterMessage::messageCallback
void messageCallback() override
Called when the message is delivered.
Definition: juce_AsyncUpdater.cpp:31
juce::Atomic< int >
juce::AsyncUpdater::AsyncUpdater
AsyncUpdater()
Creates an AsyncUpdater object.
Definition: juce_AsyncUpdater.cpp:44
juce::AsyncUpdater::triggerAsyncUpdate
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
Definition: juce_AsyncUpdater.cpp:62
juce::AsyncUpdater::isUpdatePending
bool isUpdatePending() const noexcept
Returns true if there's an update callback in the pipeline.
Definition: juce_AsyncUpdater.cpp:88
juce::Atomic::compareAndSetBool
bool compareAndSetBool(Type newValue, Type valueToCompare) noexcept
Atomically compares this value with a target value, and if it is equal, sets this to be equal to a ne...
Definition: juce_Atomic.h:100
juce::AsyncUpdater::handleUpdateNowIfNeeded
void handleUpdateNowIfNeeded()
If an update has been triggered and is pending, this will invoke it synchronously.
Definition: juce_AsyncUpdater.cpp:79
juce::AsyncUpdater::~AsyncUpdater
virtual ~AsyncUpdater()
Destructor.
Definition: juce_AsyncUpdater.cpp:49
juce::CallbackMessage
A message that invokes a callback method when it gets delivered.
Definition: juce_CallbackMessage.h:52
juce::AsyncUpdater::cancelPendingUpdate
void cancelPendingUpdate() noexcept
This will stop any pending updates from happening.
Definition: juce_AsyncUpdater.cpp:74
juce::AsyncUpdater
Has a callback method that is triggered asynchronously.
Definition: juce_AsyncUpdater.h:42
juce::MessageManager::getInstanceWithoutCreating
static MessageManager * getInstanceWithoutCreating() noexcept
Returns the global instance of the MessageManager, or nullptr if it doesn't exist.
Definition: juce_MessageManager.cpp:56
juce::AsyncUpdater::AsyncUpdaterMessage
Definition: juce_AsyncUpdater.cpp:26
juce::AsyncUpdater::handleAsyncUpdate
virtual void handleAsyncUpdate()=0
Called back to do whatever your class needs to do.