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.

juce_MessageManager.cpp 11KB

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