Add remaining files
[juce-lv2.git] / juce / source / src / events / juce_AsyncUpdater.cpp
blobcb5f9873cbad0d31223ab7a4eb81c5dc44945a7d
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../core/juce_StandardHeader.h"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_AsyncUpdater.h"
31 #include "juce_CallbackMessage.h"
32 #include "juce_MessageManager.h"
35 //==============================================================================
36 class AsyncUpdaterMessage : public CallbackMessage
38 public:
39 AsyncUpdaterMessage (AsyncUpdater& owner_)
40 : owner (owner_)
44 void messageCallback()
46 if (shouldDeliver.compareAndSetBool (0, 1))
47 owner.handleAsyncUpdate();
50 Atomic<int> shouldDeliver;
52 private:
53 AsyncUpdater& owner;
56 //==============================================================================
57 AsyncUpdater::AsyncUpdater()
59 message = new AsyncUpdaterMessage (*this);
62 inline Atomic<int>& AsyncUpdater::getDeliveryFlag() const noexcept
64 return static_cast <AsyncUpdaterMessage*> (message.getObject())->shouldDeliver;
67 AsyncUpdater::~AsyncUpdater()
69 // You're deleting this object with a background thread while there's an update
70 // pending on the main event thread - that's pretty dodgy threading, as the callback could
71 // happen after this destructor has finished. You should either use a MessageManagerLock while
72 // deleting this object, or find some other way to avoid such a race condition.
73 jassert ((! isUpdatePending()) || MessageManager::getInstance()->currentThreadHasLockedMessageManager());
75 getDeliveryFlag().set (0);
78 void AsyncUpdater::triggerAsyncUpdate()
80 if (getDeliveryFlag().compareAndSetBool (1, 0))
81 message->post();
84 void AsyncUpdater::cancelPendingUpdate() noexcept
86 getDeliveryFlag().set (0);
89 void AsyncUpdater::handleUpdateNowIfNeeded()
91 // This can only be called by the event thread.
92 jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
94 if (getDeliveryFlag().exchange (0) != 0)
95 handleAsyncUpdate();
98 bool AsyncUpdater::isUpdatePending() const noexcept
100 return getDeliveryFlag().value != 0;
104 END_JUCE_NAMESPACE