26MessageManager::MessageManager() noexcept
27 : messageThreadId (Thread::getCurrentThreadId())
33MessageManager::~MessageManager() noexcept
37 doPlatformSpecificShutdown();
39 jassert (instance ==
this);
43MessageManager* MessageManager::instance =
nullptr;
47 if (instance ==
nullptr)
50 doPlatformSpecificInitialisation();
63 deleteAndZero (instance);
67bool MessageManager::MessageBase::post()
69 auto* mm = MessageManager::instance;
71 if (mm ==
nullptr || mm->quitMessagePosted.get() != 0 || ! postMessageToSystemQueue (
this))
81#if JUCE_MODAL_LOOPS_PERMITTED && ! (JUCE_MAC || JUCE_IOS)
88 while (quitMessageReceived.
get() == 0)
92 if (! dispatchNextMessageOnSystemQueue (millisecondsToRunFor >= 0))
101 return quitMessageReceived.
get() == 0;
105#if ! (JUCE_MAC || JUCE_IOS || JUCE_ANDROID)
106class MessageManager::QuitMessage :
public MessageManager::MessageBase
111 void messageCallback()
override
113 if (
auto* mm = MessageManager::instance)
114 mm->quitMessageReceived =
true;
117 JUCE_DECLARE_NON_COPYABLE (QuitMessage)
124 while (quitMessageReceived.
get() == 0)
128 if (! dispatchNextMessageOnSystemQueue (
false))
137 (
new QuitMessage())->post();
138 quitMessagePosted =
true;
148 : func (f), parameter (param)
151 void messageCallback()
override
153 result = (*func) (parameter);
158 std::atomic<void*> result {
nullptr };
161 MessageCallbackFunction*
const func;
162 void*
const parameter;
170 return func (parameter);
179 message->finished.wait();
180 return message->result.load();
188void MessageManager::deliverBroadcastMessage (
const String& value)
190 if (broadcaster !=
nullptr)
191 broadcaster->sendActionMessage (value);
196 if (broadcaster ==
nullptr)
199 broadcaster->addActionListener (listener);
204 if (broadcaster !=
nullptr)
205 broadcaster->removeActionListener (listener);
218 if (messageThreadId != thisThread)
220 messageThreadId = thisThread;
223 doPlatformSpecificShutdown();
224 doPlatformSpecificInitialisation();
231 return thisThread == messageThreadId || thisThread == threadWithLock.
get();
237 return i->currentThreadHasLockedMessageManager();
245 return i->isThisTheMessageThread();
267 void messageCallback()
override
272 if (
auto* o = owner.get())
273 o->messageCallback();
292bool MessageManager::Lock::tryAcquire (
bool lockIsMandatory)
const noexcept
294 auto* mm = MessageManager::instance;
302 if (! lockIsMandatory && (abortWait.get() != 0))
308 if (mm->currentThreadHasLockedMessageManager())
313 blockingMessage = *
new BlockingMessage (
this);
317 jassert (! lockIsMandatory);
321 if (! blockingMessage->post())
324 jassert (! lockIsMandatory);
325 blockingMessage =
nullptr;
331 while (abortWait.get() == 0)
332 lockedEvent.wait (-1);
336 if (lockGained.get() != 0)
342 }
while (lockIsMandatory);
345 blockingMessage->releaseEvent.signal();
348 ScopedLock lock (blockingMessage->ownerCriticalSection);
351 blockingMessage->owner.set (
nullptr);
354 blockingMessage =
nullptr;
360 if (lockGained.compareAndSetBool (
false,
true))
362 auto* mm = MessageManager::instance;
364 jassert (mm ==
nullptr || mm->currentThreadHasLockedMessageManager());
368 mm->threadWithLock = {};
370 if (blockingMessage !=
nullptr)
372 blockingMessage->releaseEvent.signal();
373 blockingMessage =
nullptr;
378void MessageManager::Lock::messageCallback()
const
387 lockedEvent.signal();
392 : locked (attemptLock (threadToCheck, nullptr))
396 : locked (attemptLock (nullptr, jobToCheck))
401 jassert (threadToCheck ==
nullptr || jobToCheck ==
nullptr);
403 if (threadToCheck !=
nullptr)
406 if (jobToCheck !=
nullptr)
411 && (jobToCheck ==
nullptr || ! jobToCheck->
shouldExit()))
417 if (threadToCheck !=
nullptr)
425 if (jobToCheck !=
nullptr)
438void MessageManagerLock::exitSignalSent()
444JUCE_API void JUCE_CALLTYPE initialiseJuce_GUI();
445JUCE_API void JUCE_CALLTYPE initialiseJuce_GUI()
453JUCE_API void JUCE_CALLTYPE shutdownJuce_GUI();
454JUCE_API void JUCE_CALLTYPE shutdownJuce_GUI()
463static int numScopedInitInstances = 0;
Manages a list of ActionListeners, and can send them messages.
Interface class for delivery of events that are sent by an ActionBroadcaster.
static void deleteAll()
Deletes all extant objects.
Automatically locks and unlocks a mutex object.
static bool isStandaloneApp() noexcept
Returns true if this executable is running as an app (as opposed to being a plugin or other kind of s...
~MessageManagerLock() override
Releases the current thread's lock on the message manager.
MessageManagerLock(Thread *threadToCheckForExitSignal=nullptr)
Tries to acquire a lock on the message manager.
A lock you can use to lock the message manager.
Lock()
Creates a new critical section to exclusively access methods which can only be called when the messag...
void enter() const noexcept
Acquires the message manager lock.
void abort() const noexcept
Unblocks a thread which is waiting in tryEnter Call this method if you want to unblock a thread which...
bool tryEnter() const noexcept
Attempts to lock the meesage manager and exits if abort is called.
void exit() const noexcept
Releases the message manager lock.
Internal class used as the base class for all message objects.
This class is in charge of the application's event-dispatch loop.
bool runDispatchLoopUntil(int millisecondsToRunFor)
Synchronously dispatches messages until a given time has elapsed.
bool isThisTheMessageThread() const noexcept
Returns true if the caller-thread is the message thread.
static bool existsAndIsCurrentThread() noexcept
Returns true if there's an instance of the MessageManager, and if the current thread is running it.
void runDispatchLoop()
Runs the event dispatch loop until a stop message is posted.
void stopDispatchLoop()
Sends a signal that the dispatch loop should terminate.
bool currentThreadHasLockedMessageManager() const noexcept
Returns true if the caller thread has currently got the message manager locked.
void * callFunctionOnMessageThread(MessageCallbackFunction *callback, void *userData)
Calls a function using the message-thread.
void deregisterBroadcastListener(ActionListener *listener)
Deregisters a broadcast listener.
void setCurrentThreadAsMessageThread()
Called to tell the manager that the current thread is the one that's running the dispatch loop.
static bool existsAndIsLockedByCurrentThread() noexcept
Returns true if there's an instance of the MessageManager, and if the current thread has the lock on ...
void registerBroadcastListener(ActionListener *listener)
Registers a listener to get told about broadcast messages.
static void deleteInstance()
Deletes the global MessageManager instance.
static MessageManager * getInstanceWithoutCreating() noexcept
Returns the global instance of the MessageManager, or nullptr if it doesn't exist.
static MessageManager * getInstance()
Returns the global instance of the MessageManager.
A smart-pointer class which points to a reference-counted object.
~ScopedJuceInitialiser_GUI()
The destructor simply calls shutdownJuce_GUI().
ScopedJuceInitialiser_GUI()
The constructor simply calls initialiseJuce_GUI().
A task that is executed by a ThreadPool object.
void addListener(Thread::Listener *)
Add a listener to this thread job which will receive a callback when signalJobShouldExit was called o...
bool shouldExit() const noexcept
Returns true if something is trying to interrupt this job and make it stop.
void removeListener(Thread::Listener *)
Removes a listener added with addListener.
static void JUCE_CALLTYPE sleep(int milliseconds)
Suspends the execution of the current thread until the specified timeout period has elapsed (note tha...
static void JUCE_CALLTYPE setCurrentThreadName(const String &newThreadName)
Changes the name of the caller thread.
bool threadShouldExit() const
Checks whether the thread has been told to stop running.
static ThreadID JUCE_CALLTYPE getCurrentThreadId()
Returns an id that identifies the caller thread.
void addListener(Listener *)
Add a listener to this thread which will receive a callback when signalThreadShouldExit was called on...
void removeListener(Listener *)
Removes a listener added with addListener.
static int64 currentTimeMillis() noexcept
Returns the current system time.
Allows threads to wait for events triggered by other threads.
#define JUCE_API
This macro is added to all JUCE public class declarations.
A simple wrapper around std::atomic.
Type get() const noexcept
Atomically reads and returns the current value.