OpenShot Audio Library | OpenShotAudio: juce_MessageManager.h Source File

OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
juce_MessageManager.h
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
23namespace juce
24{
25
26class MessageManagerLock;
27class ThreadPoolJob;
28class ActionListener;
29class ActionBroadcaster;
30
31//==============================================================================
32#if JUCE_MODULE_AVAILABLE_juce_opengl
33class OpenGLContext;
34#endif
35
36//==============================================================================
38using MessageCallbackFunction = void* (void* userData);
39
40
41//==============================================================================
49 class JUCE_API MessageManager final
50{
51public:
52 //==============================================================================
54 static MessageManager* getInstance();
55
57 static MessageManager* getInstanceWithoutCreating() noexcept;
58
62 static void deleteInstance();
63
64 //==============================================================================
72 void runDispatchLoop();
73
81 void stopDispatchLoop();
82
85 bool hasStopMessageBeenSent() const noexcept { return quitMessagePosted.get() != 0; }
86
87 #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
93 bool runDispatchLoopUntil (int millisecondsToRunFor);
94 #endif
95
96 //==============================================================================
102 static bool callAsync (std::function<void()> functionToCall);
103
122 void* callFunctionOnMessageThread (MessageCallbackFunction* callback, void* userData);
123
125 bool isThisTheMessageThread() const noexcept;
126
132 void setCurrentThreadAsMessageThread();
133
139 Thread::ThreadID getCurrentMessageThread() const noexcept { return messageThreadId; }
140
148 bool currentThreadHasLockedMessageManager() const noexcept;
149
153 static bool existsAndIsLockedByCurrentThread() noexcept;
154
158 static bool existsAndIsCurrentThread() noexcept;
159
160 //==============================================================================
167 static void broadcastMessage (const String& messageText);
168
176 void registerBroadcastListener (ActionListener* listener);
177
179 void deregisterBroadcastListener (ActionListener* listener);
180
181 //==============================================================================
186 class JUCE_API MessageBase : public ReferenceCountedObject
187 {
188 public:
189 MessageBase() = default;
190 ~MessageBase() override = default;
191
192 virtual void messageCallback() = 0;
193 bool post();
194
195 using Ptr = ReferenceCountedObjectPtr<MessageBase>;
196
197 JUCE_DECLARE_NON_COPYABLE (MessageBase)
198 };
199
200 //==============================================================================
204 class JUCE_API Lock
205 {
206 public:
214 Lock();
215
217 ~Lock();
218
231 void enter() const noexcept;
232
276 bool tryEnter() const noexcept;
277
281 void exit() const noexcept;
282
289 void abort() const noexcept;
290
291 //==============================================================================
293 using ScopedLockType = GenericScopedLock<Lock>;
294
296 using ScopedUnlockType = GenericScopedUnlock<Lock>;
297
299 using ScopedTryLockType = GenericScopedTryLock<Lock>;
300
301 private:
302 struct BlockingMessage;
303 friend class ReferenceCountedObjectPtr<BlockingMessage>;
304
305 bool tryAcquire (bool) const noexcept;
306 void messageCallback() const;
307
308 //==============================================================================
309 mutable ReferenceCountedObjectPtr<BlockingMessage> blockingMessage;
310 WaitableEvent lockedEvent;
311 mutable Atomic<int> abortWait, lockGained;
312 };
313
314 //==============================================================================
315 #ifndef DOXYGEN
316 // Internal methods - do not use!
317 void deliverBroadcastMessage (const String&);
318 ~MessageManager() noexcept;
319 #endif
320
321private:
322 //==============================================================================
323 MessageManager() noexcept;
324
325 static MessageManager* instance;
326
327 friend class MessageBase;
328 class QuitMessage;
329 friend class QuitMessage;
330 friend class MessageManagerLock;
331
332 std::unique_ptr<ActionBroadcaster> broadcaster;
333 Atomic<int> quitMessagePosted { 0 }, quitMessageReceived { 0 };
334 Thread::ThreadID messageThreadId;
335 Atomic<Thread::ThreadID> threadWithLock;
336
337 static bool postMessageToSystemQueue (MessageBase*);
338 static void* exitModalLoopCallback (void*);
339 static void doPlatformSpecificInitialisation();
340 static void doPlatformSpecificShutdown();
341 static bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages);
342
343 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MessageManager)
344};
345
346
347//==============================================================================
384 class JUCE_API MessageManagerLock : private Thread::Listener
385{
386public:
387 //==============================================================================
429 MessageManagerLock (Thread* threadToCheckForExitSignal = nullptr);
430
431 //==============================================================================
437 MessageManagerLock (ThreadPoolJob* jobToCheckForExitSignal);
438
439 //==============================================================================
445 ~MessageManagerLock() override;
446
447 //==============================================================================
451 bool lockWasGained() const noexcept { return locked; }
452
453private:
454 //==============================================================================
455 MessageManager::Lock mmLock;
456 bool locked;
457
458 //==============================================================================
459 bool attemptLock (Thread*, ThreadPoolJob*);
460 void exitSignalSent() override;
461
462 JUCE_DECLARE_NON_COPYABLE (MessageManagerLock)
463};
464
465//==============================================================================
471#define JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED \
472 jassert (juce::MessageManager::existsAndIsLockedByCurrentThread());
473
479#define JUCE_ASSERT_MESSAGE_THREAD \
480 jassert (juce::MessageManager::existsAndIsCurrentThread());
481
485#define JUCE_ASSERT_MESSAGE_MANAGER_EXISTS \
486 jassert (juce::MessageManager::getInstanceWithoutCreating() != nullptr);
487
488
489} // namespace juce
juce::ActionBroadcaster
Definition: juce_ActionBroadcaster.h:37
juce::ActionListener
Definition: juce_ActionListener.h:35
juce::GenericScopedLock
Definition: juce_ScopedLock.h:55
juce::GenericScopedTryLock
Definition: juce_ScopedLock.h:198
juce::GenericScopedUnlock
Definition: juce_ScopedLock.h:126
juce::MessageManagerLock
Definition: juce_MessageManager.h:385
juce::MessageManagerLock::lockWasGained
bool lockWasGained() const noexcept
Definition: juce_MessageManager.h:451
juce::MessageManager::Lock
Definition: juce_MessageManager.h:205
juce::MessageManager::MessageBase
Definition: juce_MessageManager.h:187
juce::MessageManager
Definition: juce_MessageManager.h:50
juce::MessageManager::hasStopMessageBeenSent
bool hasStopMessageBeenSent() const noexcept
Definition: juce_MessageManager.h:85
juce::ReferenceCountedObjectPtr
Definition: juce_ReferenceCountedObject.h:247
juce::ReferenceCountedObject
Definition: juce_ReferenceCountedObject.h:66
juce::String
Definition: juce_String.h:39
juce::ThreadPoolJob
Definition: juce_ThreadPool.h:45
juce::Thread::Listener
Definition: juce_Thread.h:181
juce::Thread
Definition: juce_Thread.h:43
juce::WaitableEvent
Definition: juce_WaitableEvent.h:36
juce::Atomic
Definition: juce_Atomic.h:42