The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. MessageManager::MessageManager() noexcept
  18. : messageThreadId (Thread::getCurrentThreadId())
  19. {
  20. if (JUCEApplicationBase::isStandaloneApp())
  21. Thread::setCurrentThreadName ("Juce Message Thread");
  22. }
  23. MessageManager::~MessageManager() noexcept
  24. {
  25. broadcaster = nullptr;
  26. doPlatformSpecificShutdown();
  27. jassert (instance == this);
  28. instance = nullptr; // do this last in case this instance is still needed by doPlatformSpecificShutdown()
  29. }
  30. MessageManager* MessageManager::instance = nullptr;
  31. MessageManager* MessageManager::getInstance()
  32. {
  33. if (instance == nullptr)
  34. {
  35. instance = new MessageManager();
  36. doPlatformSpecificInitialisation();
  37. }
  38. return instance;
  39. }
  40. MessageManager* MessageManager::getInstanceWithoutCreating() noexcept
  41. {
  42. return instance;
  43. }
  44. void MessageManager::deleteInstance()
  45. {
  46. deleteAndZero (instance);
  47. }
  48. //==============================================================================
  49. bool MessageManager::MessageBase::post()
  50. {
  51. auto* mm = MessageManager::instance;
  52. if (mm == nullptr || mm->quitMessagePosted || ! postMessageToSystemQueue (this))
  53. {
  54. Ptr deleter (this); // (this will delete messages that were just created with a 0 ref count)
  55. return false;
  56. }
  57. return true;
  58. }
  59. //==============================================================================
  60. #if JUCE_MODAL_LOOPS_PERMITTED && ! (JUCE_MAC || JUCE_IOS)
  61. bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
  62. {
  63. jassert (isThisTheMessageThread()); // must only be called by the message thread
  64. const int64 endTime = Time::currentTimeMillis() + millisecondsToRunFor;
  65. while (! quitMessageReceived)
  66. {
  67. JUCE_TRY
  68. {
  69. if (! dispatchNextMessageOnSystemQueue (millisecondsToRunFor >= 0))
  70. Thread::sleep (1);
  71. }
  72. JUCE_CATCH_EXCEPTION
  73. if (millisecondsToRunFor >= 0 && Time::currentTimeMillis() >= endTime)
  74. break;
  75. }
  76. return ! quitMessageReceived;
  77. }
  78. #endif
  79. #if ! (JUCE_MAC || JUCE_IOS || JUCE_ANDROID)
  80. class MessageManager::QuitMessage : public MessageManager::MessageBase
  81. {
  82. public:
  83. QuitMessage() {}
  84. void messageCallback() override
  85. {
  86. if (auto* mm = MessageManager::instance)
  87. mm->quitMessageReceived = true;
  88. }
  89. JUCE_DECLARE_NON_COPYABLE (QuitMessage)
  90. };
  91. void MessageManager::runDispatchLoop()
  92. {
  93. jassert (isThisTheMessageThread()); // must only be called by the message thread
  94. while (! quitMessageReceived)
  95. {
  96. JUCE_TRY
  97. {
  98. if (! dispatchNextMessageOnSystemQueue (false))
  99. Thread::sleep (1);
  100. }
  101. JUCE_CATCH_EXCEPTION
  102. }
  103. }
  104. void MessageManager::stopDispatchLoop()
  105. {
  106. (new QuitMessage())->post();
  107. quitMessagePosted = true;
  108. }
  109. #endif
  110. //==============================================================================
  111. class AsyncFunctionCallback : public MessageManager::MessageBase
  112. {
  113. public:
  114. AsyncFunctionCallback (MessageCallbackFunction* const f, void* const param)
  115. : func (f), parameter (param)
  116. {}
  117. void messageCallback() override
  118. {
  119. result = (*func) (parameter);
  120. finished.signal();
  121. }
  122. WaitableEvent finished;
  123. void* volatile result = nullptr;
  124. private:
  125. MessageCallbackFunction* const func;
  126. void* const parameter;
  127. JUCE_DECLARE_NON_COPYABLE (AsyncFunctionCallback)
  128. };
  129. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* const func, void* const parameter)
  130. {
  131. if (isThisTheMessageThread())
  132. return func (parameter);
  133. // If this thread has the message manager locked, then this will deadlock!
  134. jassert (! currentThreadHasLockedMessageManager());
  135. const ReferenceCountedObjectPtr<AsyncFunctionCallback> message (new AsyncFunctionCallback (func, parameter));
  136. if (message->post())
  137. {
  138. message->finished.wait();
  139. return message->result;
  140. }
  141. jassertfalse; // the OS message queue failed to send the message!
  142. return nullptr;
  143. }
  144. //==============================================================================
  145. void MessageManager::deliverBroadcastMessage (const String& value)
  146. {
  147. if (broadcaster != nullptr)
  148. broadcaster->sendActionMessage (value);
  149. }
  150. void MessageManager::registerBroadcastListener (ActionListener* const listener)
  151. {
  152. if (broadcaster == nullptr)
  153. broadcaster = new ActionBroadcaster();
  154. broadcaster->addActionListener (listener);
  155. }
  156. void MessageManager::deregisterBroadcastListener (ActionListener* const listener)
  157. {
  158. if (broadcaster != nullptr)
  159. broadcaster->removeActionListener (listener);
  160. }
  161. //==============================================================================
  162. bool MessageManager::isThisTheMessageThread() const noexcept
  163. {
  164. return Thread::getCurrentThreadId() == messageThreadId;
  165. }
  166. void MessageManager::setCurrentThreadAsMessageThread()
  167. {
  168. const Thread::ThreadID thisThread = Thread::getCurrentThreadId();
  169. if (messageThreadId != thisThread)
  170. {
  171. messageThreadId = thisThread;
  172. // This is needed on windows to make sure the message window is created by this thread
  173. doPlatformSpecificShutdown();
  174. doPlatformSpecificInitialisation();
  175. }
  176. }
  177. bool MessageManager::currentThreadHasLockedMessageManager() const noexcept
  178. {
  179. const Thread::ThreadID thisThread = Thread::getCurrentThreadId();
  180. return thisThread == messageThreadId || thisThread == threadWithLock;
  181. }
  182. //==============================================================================
  183. //==============================================================================
  184. /* The only safe way to lock the message thread while another thread does
  185. some work is by posting a special message, whose purpose is to tie up the event
  186. loop until the other thread has finished its business.
  187. Any other approach can get horribly deadlocked if the OS uses its own hidden locks which
  188. get locked before making an event callback, because if the same OS lock gets indirectly
  189. accessed from another thread inside a MM lock, you're screwed. (this is exactly what happens
  190. in Cocoa).
  191. */
  192. class MessageManagerLock::BlockingMessage : public MessageManager::MessageBase
  193. {
  194. public:
  195. BlockingMessage() noexcept {}
  196. void messageCallback() override
  197. {
  198. lockedEvent.signal();
  199. releaseEvent.wait();
  200. }
  201. WaitableEvent lockedEvent, releaseEvent;
  202. JUCE_DECLARE_NON_COPYABLE (BlockingMessage)
  203. };
  204. //==============================================================================
  205. MessageManagerLock::MessageManagerLock (Thread* const threadToCheck)
  206. : blockingMessage(), checker (threadToCheck, nullptr),
  207. locked (attemptLock (threadToCheck != nullptr ? &checker : nullptr))
  208. {
  209. }
  210. MessageManagerLock::MessageManagerLock (ThreadPoolJob* const jobToCheckForExitSignal)
  211. : blockingMessage(), checker (nullptr, jobToCheckForExitSignal),
  212. locked (attemptLock (jobToCheckForExitSignal != nullptr ? &checker : nullptr))
  213. {
  214. }
  215. MessageManagerLock::MessageManagerLock (BailOutChecker& bailOutChecker)
  216. : blockingMessage(), checker (nullptr, nullptr),
  217. locked (attemptLock (&bailOutChecker))
  218. {
  219. }
  220. bool MessageManagerLock::attemptLock (BailOutChecker* bailOutChecker)
  221. {
  222. auto* mm = MessageManager::instance;
  223. if (mm == nullptr)
  224. return false;
  225. if (mm->currentThreadHasLockedMessageManager())
  226. return true;
  227. if (bailOutChecker == nullptr)
  228. {
  229. mm->lockingLock.enter();
  230. }
  231. else
  232. {
  233. while (! mm->lockingLock.tryEnter())
  234. {
  235. if (bailOutChecker->shouldAbortAcquiringLock())
  236. return false;
  237. Thread::yield();
  238. }
  239. }
  240. blockingMessage = new BlockingMessage();
  241. if (! blockingMessage->post())
  242. {
  243. blockingMessage = nullptr;
  244. return false;
  245. }
  246. while (! blockingMessage->lockedEvent.wait (20))
  247. {
  248. if (bailOutChecker != nullptr && bailOutChecker->shouldAbortAcquiringLock())
  249. {
  250. blockingMessage->releaseEvent.signal();
  251. blockingMessage = nullptr;
  252. mm->lockingLock.exit();
  253. return false;
  254. }
  255. }
  256. jassert (mm->threadWithLock == 0);
  257. mm->threadWithLock = Thread::getCurrentThreadId();
  258. return true;
  259. }
  260. MessageManagerLock::~MessageManagerLock() noexcept
  261. {
  262. if (blockingMessage != nullptr)
  263. {
  264. auto* mm = MessageManager::instance;
  265. jassert (mm == nullptr || mm->currentThreadHasLockedMessageManager());
  266. blockingMessage->releaseEvent.signal();
  267. blockingMessage = nullptr;
  268. if (mm != nullptr)
  269. {
  270. mm->threadWithLock = 0;
  271. mm->lockingLock.exit();
  272. }
  273. }
  274. }
  275. //==============================================================================
  276. MessageManagerLock::ThreadChecker::ThreadChecker (Thread* const threadToUse,
  277. ThreadPoolJob* const threadJobToUse)
  278. : threadToCheck (threadToUse), job (threadJobToUse)
  279. {
  280. }
  281. bool MessageManagerLock::ThreadChecker::shouldAbortAcquiringLock()
  282. {
  283. return (threadToCheck != nullptr && threadToCheck->threadShouldExit())
  284. || (job != nullptr && job->shouldExit());
  285. }
  286. //==============================================================================
  287. JUCE_API void JUCE_CALLTYPE initialiseJuce_GUI();
  288. JUCE_API void JUCE_CALLTYPE initialiseJuce_GUI()
  289. {
  290. JUCE_AUTORELEASEPOOL
  291. {
  292. MessageManager::getInstance();
  293. }
  294. }
  295. JUCE_API void JUCE_CALLTYPE shutdownJuce_GUI();
  296. JUCE_API void JUCE_CALLTYPE shutdownJuce_GUI()
  297. {
  298. JUCE_AUTORELEASEPOOL
  299. {
  300. DeletedAtShutdown::deleteAll();
  301. MessageManager::deleteInstance();
  302. }
  303. }
  304. static int numScopedInitInstances = 0;
  305. ScopedJuceInitialiser_GUI::ScopedJuceInitialiser_GUI() { if (numScopedInitInstances++ == 0) initialiseJuce_GUI(); }
  306. ScopedJuceInitialiser_GUI::~ScopedJuceInitialiser_GUI() { if (--numScopedInitInstances == 0) shutdownJuce_GUI(); }