OpenShot Audio Library | OpenShotAudio  0.3.0
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 
23 namespace juce
24 {
25 
26 class MessageManagerLock;
27 class ThreadPoolJob;
28 class ActionListener;
29 class ActionBroadcaster;
30 
31 //==============================================================================
32 #if JUCE_MODULE_AVAILABLE_juce_opengl
33 class OpenGLContext;
34 #endif
35 
36 //==============================================================================
38 using MessageCallbackFunction = void* (void* userData);
39 
40 
41 //==============================================================================
49 class JUCE_API MessageManager final
50 {
51 public:
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 
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  //==============================================================================
294 
297 
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 
321 private:
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 {
386 public:
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 
453 private:
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
bool lockWasGained() const noexcept
bool hasStopMessageBeenSent() const noexcept
void * ThreadID
Definition: juce_Thread.h:304