Audio plugin host https://kx.studio/carla
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.

394 lines
12KB

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