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.

368 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. Message::Message() noexcept : messageRecipient (nullptr) {}
  19. Message::~Message() {}
  20. //==============================================================================
  21. CallbackMessage::CallbackMessage() noexcept {}
  22. CallbackMessage::~CallbackMessage() {}
  23. void CallbackMessage::post()
  24. {
  25. MessageManager* const mm = MessageManager::instance;
  26. if (mm != nullptr)
  27. mm->postMessageToQueue (this);
  28. }
  29. //==============================================================================
  30. class MessageManager::QuitMessage : public CallbackMessage
  31. {
  32. public:
  33. QuitMessage() {}
  34. void messageCallback()
  35. {
  36. MessageManager* const mm = MessageManager::instance;
  37. if (mm != nullptr)
  38. mm->quitMessageReceived = true;
  39. }
  40. private:
  41. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (QuitMessage);
  42. };
  43. //==============================================================================
  44. MessageManager* MessageManager::instance = nullptr;
  45. MessageManager::MessageManager() noexcept
  46. : quitMessagePosted (false),
  47. quitMessageReceived (false),
  48. messageThreadId (Thread::getCurrentThreadId()),
  49. threadWithLock (0)
  50. {
  51. if (JUCEApplicationBase::isStandaloneApp())
  52. Thread::setCurrentThreadName ("Juce Message Thread");
  53. }
  54. MessageManager::~MessageManager() noexcept
  55. {
  56. broadcaster = nullptr;
  57. doPlatformSpecificShutdown();
  58. // If you hit this assertion, then you've probably leaked some kind of MessageListener object..
  59. jassert (messageListeners.size() == 0);
  60. jassert (instance == this);
  61. instance = nullptr; // do this last in case this instance is still needed by doPlatformSpecificShutdown()
  62. }
  63. MessageManager* MessageManager::getInstance()
  64. {
  65. if (instance == nullptr)
  66. {
  67. instance = new MessageManager();
  68. doPlatformSpecificInitialisation();
  69. }
  70. return instance;
  71. }
  72. void MessageManager::deleteInstance()
  73. {
  74. deleteAndZero (instance);
  75. }
  76. void MessageManager::postMessageToQueue (Message* const message)
  77. {
  78. if (quitMessagePosted || ! postMessageToSystemQueue (message))
  79. Message::Ptr deleter (message); // (this will delete messages that were just created with a 0 ref count)
  80. }
  81. //==============================================================================
  82. void MessageManager::deliverMessage (Message* const message)
  83. {
  84. JUCE_TRY
  85. {
  86. MessageListener* const recipient = message->messageRecipient;
  87. if (recipient == nullptr)
  88. {
  89. CallbackMessage* const callbackMessage = dynamic_cast <CallbackMessage*> (message);
  90. if (callbackMessage != nullptr)
  91. callbackMessage->messageCallback();
  92. }
  93. else if (messageListeners.contains (recipient))
  94. {
  95. recipient->handleMessage (*message);
  96. }
  97. }
  98. JUCE_CATCH_EXCEPTION
  99. }
  100. //==============================================================================
  101. #if JUCE_MODAL_LOOPS_PERMITTED && ! (JUCE_MAC || JUCE_IOS)
  102. void MessageManager::runDispatchLoop()
  103. {
  104. jassert (isThisTheMessageThread()); // must only be called by the message thread
  105. runDispatchLoopUntil (-1);
  106. }
  107. void MessageManager::stopDispatchLoop()
  108. {
  109. (new QuitMessage())->post();
  110. }
  111. bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
  112. {
  113. jassert (isThisTheMessageThread()); // must only be called by the message thread
  114. const int64 endTime = Time::currentTimeMillis() + millisecondsToRunFor;
  115. while ((millisecondsToRunFor < 0 || endTime > Time::currentTimeMillis())
  116. && ! quitMessageReceived)
  117. {
  118. JUCE_TRY
  119. {
  120. if (! dispatchNextMessageOnSystemQueue (millisecondsToRunFor >= 0))
  121. {
  122. const int msToWait = (int) (endTime - Time::currentTimeMillis());
  123. if (msToWait > 0)
  124. Thread::sleep (jmin (5, msToWait));
  125. }
  126. }
  127. JUCE_CATCH_EXCEPTION
  128. }
  129. return ! quitMessageReceived;
  130. }
  131. #endif
  132. //==============================================================================
  133. class AsyncFunctionCallback : public CallbackMessage
  134. {
  135. public:
  136. AsyncFunctionCallback (MessageCallbackFunction* const f, void* const param)
  137. : result (nullptr), func (f), parameter (param)
  138. {}
  139. void messageCallback()
  140. {
  141. result = (*func) (parameter);
  142. finished.signal();
  143. }
  144. WaitableEvent finished;
  145. void* volatile result;
  146. private:
  147. MessageCallbackFunction* const func;
  148. void* const parameter;
  149. JUCE_DECLARE_NON_COPYABLE (AsyncFunctionCallback);
  150. };
  151. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* const func, void* const parameter)
  152. {
  153. if (MessageManager::getInstance()->isThisTheMessageThread())
  154. return func (parameter);
  155. const ReferenceCountedObjectPtr<AsyncFunctionCallback> message (new AsyncFunctionCallback (func, parameter));
  156. message->post();
  157. message->finished.wait();
  158. return message->result;
  159. }
  160. //==============================================================================
  161. void MessageManager::deliverBroadcastMessage (const String& value)
  162. {
  163. if (broadcaster != nullptr)
  164. broadcaster->sendActionMessage (value);
  165. }
  166. void MessageManager::registerBroadcastListener (ActionListener* const listener)
  167. {
  168. if (broadcaster == nullptr)
  169. broadcaster = new ActionBroadcaster();
  170. broadcaster->addActionListener (listener);
  171. }
  172. void MessageManager::deregisterBroadcastListener (ActionListener* const listener)
  173. {
  174. if (broadcaster != nullptr)
  175. broadcaster->removeActionListener (listener);
  176. }
  177. //==============================================================================
  178. bool MessageManager::isThisTheMessageThread() const noexcept
  179. {
  180. return Thread::getCurrentThreadId() == messageThreadId;
  181. }
  182. void MessageManager::setCurrentThreadAsMessageThread()
  183. {
  184. const Thread::ThreadID thisThread = Thread::getCurrentThreadId();
  185. if (messageThreadId != thisThread)
  186. {
  187. messageThreadId = thisThread;
  188. // This is needed on windows to make sure the message window is created by this thread
  189. doPlatformSpecificShutdown();
  190. doPlatformSpecificInitialisation();
  191. }
  192. }
  193. bool MessageManager::currentThreadHasLockedMessageManager() const noexcept
  194. {
  195. const Thread::ThreadID thisThread = Thread::getCurrentThreadId();
  196. return thisThread == messageThreadId || thisThread == threadWithLock;
  197. }
  198. //==============================================================================
  199. //==============================================================================
  200. /* The only safe way to lock the message thread while another thread does
  201. some work is by posting a special message, whose purpose is to tie up the event
  202. loop until the other thread has finished its business.
  203. Any other approach can get horribly deadlocked if the OS uses its own hidden locks which
  204. get locked before making an event callback, because if the same OS lock gets indirectly
  205. accessed from another thread inside a MM lock, you're screwed. (this is exactly what happens
  206. in Cocoa).
  207. */
  208. class MessageManagerLock::BlockingMessage : public CallbackMessage
  209. {
  210. public:
  211. BlockingMessage() {}
  212. void messageCallback()
  213. {
  214. lockedEvent.signal();
  215. releaseEvent.wait();
  216. }
  217. WaitableEvent lockedEvent, releaseEvent;
  218. private:
  219. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BlockingMessage);
  220. };
  221. //==============================================================================
  222. MessageManagerLock::MessageManagerLock (Thread* const threadToCheck)
  223. : blockingMessage(), locked (attemptLock (threadToCheck, nullptr))
  224. {
  225. }
  226. MessageManagerLock::MessageManagerLock (ThreadPoolJob* const jobToCheckForExitSignal)
  227. : blockingMessage(), locked (attemptLock (nullptr, jobToCheckForExitSignal))
  228. {
  229. }
  230. bool MessageManagerLock::attemptLock (Thread* const threadToCheck, ThreadPoolJob* const job)
  231. {
  232. MessageManager* const mm = MessageManager::instance;
  233. if (mm == nullptr)
  234. return false;
  235. if (mm->currentThreadHasLockedMessageManager())
  236. return true;
  237. if (threadToCheck == nullptr && job == nullptr)
  238. {
  239. mm->lockingLock.enter();
  240. }
  241. else
  242. {
  243. while (! mm->lockingLock.tryEnter())
  244. {
  245. if ((threadToCheck != nullptr && threadToCheck->threadShouldExit())
  246. || (job != nullptr && job->shouldExit()))
  247. return false;
  248. Thread::yield();
  249. }
  250. }
  251. blockingMessage = new BlockingMessage();
  252. blockingMessage->post();
  253. while (! blockingMessage->lockedEvent.wait (20))
  254. {
  255. if ((threadToCheck != nullptr && threadToCheck->threadShouldExit())
  256. || (job != nullptr && job->shouldExit()))
  257. {
  258. blockingMessage->releaseEvent.signal();
  259. blockingMessage = nullptr;
  260. mm->lockingLock.exit();
  261. return false;
  262. }
  263. }
  264. jassert (mm->threadWithLock == 0);
  265. mm->threadWithLock = Thread::getCurrentThreadId();
  266. return true;
  267. }
  268. MessageManagerLock::~MessageManagerLock() noexcept
  269. {
  270. if (blockingMessage != nullptr)
  271. {
  272. MessageManager* const mm = MessageManager::instance;
  273. jassert (mm == nullptr || mm->currentThreadHasLockedMessageManager());
  274. blockingMessage->releaseEvent.signal();
  275. blockingMessage = nullptr;
  276. if (mm != nullptr)
  277. {
  278. mm->threadWithLock = 0;
  279. mm->lockingLock.exit();
  280. }
  281. }
  282. }
  283. //==============================================================================
  284. JUCE_API void JUCE_CALLTYPE initialiseJuce_GUI()
  285. {
  286. JUCE_AUTORELEASEPOOL
  287. MessageManager::getInstance();
  288. }
  289. JUCE_API void JUCE_CALLTYPE shutdownJuce_GUI()
  290. {
  291. JUCE_AUTORELEASEPOOL
  292. DeletedAtShutdown::deleteAll();
  293. MessageManager::deleteInstance();
  294. }