JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_MessageManager.h
Go to the documentation of this file.
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 MessageManagerLock;
27class ThreadPoolJob;
28class ActionListener;
29class ActionBroadcaster;
30
31//==============================================================================
33using MessageCallbackFunction = void* (void* userData);
34
35
36//==============================================================================
44class JUCE_API MessageManager final
45{
46public:
47 //==============================================================================
49 static MessageManager* getInstance();
50
52 static MessageManager* getInstanceWithoutCreating() noexcept;
53
57 static void deleteInstance();
58
59 //==============================================================================
67 void runDispatchLoop();
68
76 void stopDispatchLoop();
77
80 bool hasStopMessageBeenSent() const noexcept { return quitMessagePosted.get() != 0; }
81
82 #if JUCE_MODAL_LOOPS_PERMITTED
88 bool runDispatchLoopUntil (int millisecondsToRunFor);
89 #endif
90
91 //==============================================================================
97 static bool callAsync (std::function<void()> functionToCall);
98
117 void* callFunctionOnMessageThread (MessageCallbackFunction* callback, void* userData);
118
120 bool isThisTheMessageThread() const noexcept;
121
127 void setCurrentThreadAsMessageThread();
128
134 Thread::ThreadID getCurrentMessageThread() const noexcept { return messageThreadId; }
135
143 bool currentThreadHasLockedMessageManager() const noexcept;
144
148 static bool existsAndIsLockedByCurrentThread() noexcept;
149
153 static bool existsAndIsCurrentThread() noexcept;
154
155 //==============================================================================
162 static void broadcastMessage (const String& messageText);
163
171 void registerBroadcastListener (ActionListener* listener);
172
174 void deregisterBroadcastListener (ActionListener* listener);
175
176 //==============================================================================
181 class JUCE_API MessageBase : public ReferenceCountedObject
182 {
183 public:
184 MessageBase() = default;
185 ~MessageBase() override = default;
186
187 virtual void messageCallback() = 0;
188 bool post();
189
191
193 };
194
195 //==============================================================================
199 class JUCE_API Lock
200 {
201 public:
209 Lock();
210
212 ~Lock();
213
226 void enter() const noexcept;
227
271 bool tryEnter() const noexcept;
272
276 void exit() const noexcept;
277
284 void abort() const noexcept;
285
286 //==============================================================================
289
292
295
296 private:
297 struct BlockingMessage;
299
300 bool exclusiveTryAcquire (bool) const noexcept;
301 bool tryAcquire (bool) const noexcept;
302
303 void setAcquired (bool success) const noexcept;
304
305 //==============================================================================
306 // This mutex is used to make this lock type behave like a normal mutex.
307 // If multiple threads call enter() simultaneously, only one will succeed in gaining
308 // this mutex. The mutex is released again in exit().
309 mutable CriticalSection entryMutex;
310
311 // This mutex protects the other data members of the lock from concurrent access, which
312 // happens when the BlockingMessage calls setAcquired to indicate that the lock was gained.
313 mutable std::mutex mutex;
314 mutable ReferenceCountedObjectPtr<BlockingMessage> blockingMessage;
315 mutable std::condition_variable condvar;
316 mutable bool abortWait = false, acquired = false;
317 };
318
319 //==============================================================================
320 #ifndef DOXYGEN
321 // Internal methods - do not use!
322 void deliverBroadcastMessage (const String&);
323 ~MessageManager() noexcept;
324 #endif
325
326private:
327 //==============================================================================
328 MessageManager() noexcept;
329
330 static MessageManager* instance;
331
332 friend class MessageBase;
333 class QuitMessage;
334 friend class QuitMessage;
335 friend class MessageManagerLock;
336
337 std::unique_ptr<ActionBroadcaster> broadcaster;
338 Atomic<int> quitMessagePosted { 0 }, quitMessageReceived { 0 };
339 Thread::ThreadID messageThreadId;
340 Atomic<Thread::ThreadID> threadWithLock;
341 mutable std::mutex messageThreadIdMutex;
342
343 static bool postMessageToSystemQueue (MessageBase*);
344 static void* exitModalLoopCallback (void*);
345 static void doPlatformSpecificInitialisation();
346 static void doPlatformSpecificShutdown();
347
349};
350
351
352//==============================================================================
389class JUCE_API MessageManagerLock : private Thread::Listener
390{
391public:
392 //==============================================================================
434 MessageManagerLock (Thread* threadToCheckForExitSignal = nullptr);
435
436 //==============================================================================
442 MessageManagerLock (ThreadPoolJob* jobToCheckForExitSignal);
443
444 //==============================================================================
450 ~MessageManagerLock() override;
451
452 //==============================================================================
456 bool lockWasGained() const noexcept { return locked; }
457
458private:
459 //==============================================================================
461 bool locked;
462
463 //==============================================================================
464 bool attemptLock (Thread*, ThreadPoolJob*);
465 void exitSignalSent() override;
466
468};
469
470//==============================================================================
476#define JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED \
477 jassert (juce::MessageManager::existsAndIsLockedByCurrentThread());
478
484#define JUCE_ASSERT_MESSAGE_THREAD \
485 jassert (juce::MessageManager::existsAndIsCurrentThread());
486
490#define JUCE_ASSERT_MESSAGE_MANAGER_EXISTS \
491 jassert (juce::MessageManager::getInstanceWithoutCreating() != nullptr);
492
493
494} // namespace juce
abort
Manages a list of ActionListeners, and can send them messages.
Interface class for delivery of events that are sent by an ActionBroadcaster.
Automatically locks and unlocks a mutex object.
Automatically locks and unlocks a mutex object.
Automatically unlocks and re-locks a mutex object.
Used to make sure that the calling thread has exclusive access to the message loop.
bool lockWasGained() const noexcept
Returns true if the lock was successfully acquired.
A lock you can use to lock the message manager.
Internal class used as the base class for all message objects.
This class is in charge of the application's event-dispatch loop.
bool hasStopMessageBeenSent() const noexcept
Returns true if the stopDispatchLoop() method has been called.
A smart-pointer class which points to a reference-counted object.
A base class which provides methods for reference-counting.
The JUCE String class!
Definition juce_String.h:53
A task that is executed by a ThreadPool object.
Used to receive callbacks for thread exit calls.
Encapsulates a thread.
Definition juce_Thread.h:43
exit
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
JUCE Namespace.
void *(void *userData) MessageCallbackFunction
See MessageManager::callFunctionOnMessageThread() for use of this function type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
A simple wrapper around std::atomic.
Definition juce_Atomic.h:42