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.

522 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. MessageManager::MessageManager() noexcept
  20. : messageThreadId (Thread::getCurrentThreadId())
  21. {
  22. JUCE_VERSION_ID
  23. if (JUCEApplicationBase::isStandaloneApp())
  24. Thread::setCurrentThreadName ("JUCE Message Thread");
  25. }
  26. MessageManager::~MessageManager() noexcept
  27. {
  28. broadcaster.reset();
  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. auto* mm = MessageManager::instance;
  55. if (mm == nullptr || mm->quitMessagePosted.get() != 0 || ! 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_MAC || JUCE_IOS || JUCE_ANDROID)
  64. // implemented in platform-specific code (juce_Messaging_linux.cpp and juce_Messaging_windows.cpp)
  65. namespace detail
  66. {
  67. bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages);
  68. } // namespace detail
  69. class MessageManager::QuitMessage : public MessageManager::MessageBase
  70. {
  71. public:
  72. QuitMessage() {}
  73. void messageCallback() override
  74. {
  75. if (auto* mm = MessageManager::instance)
  76. mm->quitMessageReceived = true;
  77. }
  78. JUCE_DECLARE_NON_COPYABLE (QuitMessage)
  79. };
  80. void MessageManager::runDispatchLoop()
  81. {
  82. jassert (isThisTheMessageThread()); // must only be called by the message thread
  83. while (quitMessageReceived.get() == 0)
  84. {
  85. JUCE_TRY
  86. {
  87. if (! detail::dispatchNextMessageOnSystemQueue (false))
  88. Thread::sleep (1);
  89. }
  90. JUCE_CATCH_EXCEPTION
  91. }
  92. }
  93. void MessageManager::stopDispatchLoop()
  94. {
  95. (new QuitMessage())->post();
  96. quitMessagePosted = true;
  97. }
  98. #if JUCE_MODAL_LOOPS_PERMITTED
  99. bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
  100. {
  101. jassert (isThisTheMessageThread()); // must only be called by the message thread
  102. auto endTime = Time::currentTimeMillis() + millisecondsToRunFor;
  103. while (quitMessageReceived.get() == 0)
  104. {
  105. JUCE_TRY
  106. {
  107. if (! detail::dispatchNextMessageOnSystemQueue (millisecondsToRunFor >= 0))
  108. Thread::sleep (1);
  109. }
  110. JUCE_CATCH_EXCEPTION
  111. if (millisecondsToRunFor >= 0 && Time::currentTimeMillis() >= endTime)
  112. break;
  113. }
  114. return quitMessageReceived.get() == 0;
  115. }
  116. #endif
  117. #endif
  118. //==============================================================================
  119. class AsyncFunctionCallback : public MessageManager::MessageBase
  120. {
  121. public:
  122. AsyncFunctionCallback (MessageCallbackFunction* const f, void* const param)
  123. : func (f), parameter (param)
  124. {}
  125. void messageCallback() override
  126. {
  127. result = (*func) (parameter);
  128. finished.signal();
  129. }
  130. WaitableEvent finished;
  131. std::atomic<void*> result { nullptr };
  132. private:
  133. MessageCallbackFunction* const func;
  134. void* const parameter;
  135. JUCE_DECLARE_NON_COPYABLE (AsyncFunctionCallback)
  136. };
  137. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* func, void* parameter)
  138. {
  139. if (isThisTheMessageThread())
  140. return func (parameter);
  141. // If this thread has the message manager locked, then this will deadlock!
  142. jassert (! currentThreadHasLockedMessageManager());
  143. const ReferenceCountedObjectPtr<AsyncFunctionCallback> message (new AsyncFunctionCallback (func, parameter));
  144. if (message->post())
  145. {
  146. message->finished.wait();
  147. return message->result.load();
  148. }
  149. jassertfalse; // the OS message queue failed to send the message!
  150. return nullptr;
  151. }
  152. bool MessageManager::callAsync (std::function<void()> fn)
  153. {
  154. struct AsyncCallInvoker : public MessageBase
  155. {
  156. AsyncCallInvoker (std::function<void()> f) : callback (std::move (f)) {}
  157. void messageCallback() override { callback(); }
  158. std::function<void()> callback;
  159. };
  160. return (new AsyncCallInvoker (std::move (fn)))->post();
  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.reset (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. const std::lock_guard<std::mutex> lock { messageThreadIdMutex };
  183. return Thread::getCurrentThreadId() == messageThreadId;
  184. }
  185. void MessageManager::setCurrentThreadAsMessageThread()
  186. {
  187. auto thisThread = Thread::getCurrentThreadId();
  188. const std::lock_guard<std::mutex> lock { messageThreadIdMutex };
  189. if (std::exchange (messageThreadId, thisThread) != thisThread)
  190. {
  191. #if JUCE_WINDOWS
  192. // This is needed on windows to make sure the message window is created by this thread
  193. doPlatformSpecificShutdown();
  194. doPlatformSpecificInitialisation();
  195. #endif
  196. }
  197. }
  198. bool MessageManager::currentThreadHasLockedMessageManager() const noexcept
  199. {
  200. auto thisThread = Thread::getCurrentThreadId();
  201. return thisThread == messageThreadId || thisThread == threadWithLock.get();
  202. }
  203. bool MessageManager::existsAndIsLockedByCurrentThread() noexcept
  204. {
  205. if (auto i = getInstanceWithoutCreating())
  206. return i->currentThreadHasLockedMessageManager();
  207. return false;
  208. }
  209. bool MessageManager::existsAndIsCurrentThread() noexcept
  210. {
  211. if (auto i = getInstanceWithoutCreating())
  212. return i->isThisTheMessageThread();
  213. return false;
  214. }
  215. //==============================================================================
  216. //==============================================================================
  217. /* The only safe way to lock the message thread while another thread does
  218. some work is by posting a special message, whose purpose is to tie up the event
  219. loop until the other thread has finished its business.
  220. Any other approach can get horribly deadlocked if the OS uses its own hidden locks which
  221. get locked before making an event callback, because if the same OS lock gets indirectly
  222. accessed from another thread inside a MM lock, you're screwed. (this is exactly what happens
  223. in Cocoa).
  224. */
  225. struct MessageManager::Lock::BlockingMessage : public MessageManager::MessageBase
  226. {
  227. explicit BlockingMessage (const MessageManager::Lock* parent) noexcept
  228. : owner (parent) {}
  229. void messageCallback() override
  230. {
  231. std::unique_lock lock { mutex };
  232. if (owner != nullptr)
  233. owner->setAcquired (true);
  234. condvar.wait (lock, [&] { return owner == nullptr; });
  235. }
  236. void stopWaiting()
  237. {
  238. const ScopeGuard scope { [&] { condvar.notify_one(); } };
  239. const std::scoped_lock lock { mutex };
  240. owner = nullptr;
  241. }
  242. private:
  243. std::mutex mutex;
  244. std::condition_variable condvar;
  245. const MessageManager::Lock* owner = nullptr;
  246. JUCE_DECLARE_NON_COPYABLE (BlockingMessage)
  247. };
  248. //==============================================================================
  249. MessageManager::Lock::Lock() {}
  250. MessageManager::Lock::~Lock() { exit(); }
  251. void MessageManager::Lock::enter() const noexcept { exclusiveTryAcquire (true); }
  252. bool MessageManager::Lock::tryEnter() const noexcept { return exclusiveTryAcquire (false); }
  253. bool MessageManager::Lock::exclusiveTryAcquire (bool lockIsMandatory) const noexcept
  254. {
  255. if (lockIsMandatory)
  256. entryMutex.enter();
  257. else if (! entryMutex.tryEnter())
  258. return false;
  259. const auto result = tryAcquire (lockIsMandatory);
  260. if (! result)
  261. entryMutex.exit();
  262. return result;
  263. }
  264. bool MessageManager::Lock::tryAcquire (bool lockIsMandatory) const noexcept
  265. {
  266. auto* mm = MessageManager::instance;
  267. if (mm == nullptr)
  268. {
  269. jassertfalse;
  270. return false;
  271. }
  272. if (! lockIsMandatory && [&]
  273. {
  274. const std::scoped_lock lock { mutex };
  275. return std::exchange (abortWait, false);
  276. }())
  277. {
  278. return false;
  279. }
  280. if (mm->currentThreadHasLockedMessageManager())
  281. return true;
  282. try
  283. {
  284. blockingMessage = *new BlockingMessage (this);
  285. }
  286. catch (...)
  287. {
  288. jassert (! lockIsMandatory);
  289. return false;
  290. }
  291. if (! blockingMessage->post())
  292. {
  293. // post of message failed while trying to get the lock
  294. jassert (! lockIsMandatory);
  295. blockingMessage = nullptr;
  296. return false;
  297. }
  298. for (;;)
  299. {
  300. {
  301. std::unique_lock lock { mutex };
  302. condvar.wait (lock, [&] { return std::exchange (abortWait, false); });
  303. }
  304. if (acquired)
  305. {
  306. mm->threadWithLock = Thread::getCurrentThreadId();
  307. return true;
  308. }
  309. if (! lockIsMandatory)
  310. break;
  311. }
  312. // we didn't get the lock
  313. blockingMessage->stopWaiting();
  314. blockingMessage = nullptr;
  315. return false;
  316. }
  317. void MessageManager::Lock::exit() const noexcept
  318. {
  319. const auto wasAcquired = [&]
  320. {
  321. const std::scoped_lock lock { mutex };
  322. return acquired;
  323. }();
  324. if (! wasAcquired)
  325. return;
  326. const ScopeGuard unlocker { [&] { entryMutex.exit(); } };
  327. if (blockingMessage == nullptr)
  328. return;
  329. const ScopeGuard scope { [&]
  330. {
  331. blockingMessage = nullptr;
  332. acquired = false;
  333. } };
  334. blockingMessage->stopWaiting();
  335. if (auto* mm = MessageManager::instance)
  336. {
  337. jassert (mm->currentThreadHasLockedMessageManager());
  338. mm->threadWithLock = {};
  339. }
  340. }
  341. void MessageManager::Lock::abort() const noexcept
  342. {
  343. setAcquired (false);
  344. }
  345. void MessageManager::Lock::setAcquired (bool x) const noexcept
  346. {
  347. const ScopeGuard scope { [&] { condvar.notify_one(); } };
  348. const std::scoped_lock lock { mutex };
  349. abortWait = true;
  350. acquired = x;
  351. }
  352. //==============================================================================
  353. MessageManagerLock::MessageManagerLock (Thread* threadToCheck)
  354. : locked (attemptLock (threadToCheck, nullptr))
  355. {}
  356. MessageManagerLock::MessageManagerLock (ThreadPoolJob* jobToCheck)
  357. : locked (attemptLock (nullptr, jobToCheck))
  358. {}
  359. bool MessageManagerLock::attemptLock (Thread* threadToCheck, ThreadPoolJob* jobToCheck)
  360. {
  361. jassert (threadToCheck == nullptr || jobToCheck == nullptr);
  362. if (threadToCheck != nullptr)
  363. threadToCheck->addListener (this);
  364. if (jobToCheck != nullptr)
  365. jobToCheck->addListener (this);
  366. // tryEnter may have a spurious abort (return false) so keep checking the condition
  367. while ((threadToCheck == nullptr || ! threadToCheck->threadShouldExit())
  368. && (jobToCheck == nullptr || ! jobToCheck->shouldExit()))
  369. {
  370. if (mmLock.tryEnter())
  371. break;
  372. }
  373. if (threadToCheck != nullptr)
  374. {
  375. threadToCheck->removeListener (this);
  376. if (threadToCheck->threadShouldExit())
  377. return false;
  378. }
  379. if (jobToCheck != nullptr)
  380. {
  381. jobToCheck->removeListener (this);
  382. if (jobToCheck->shouldExit())
  383. return false;
  384. }
  385. return true;
  386. }
  387. MessageManagerLock::~MessageManagerLock() { mmLock.exit(); }
  388. void MessageManagerLock::exitSignalSent()
  389. {
  390. mmLock.abort();
  391. }
  392. //==============================================================================
  393. JUCE_API void JUCE_CALLTYPE initialiseJuce_GUI()
  394. {
  395. JUCE_AUTORELEASEPOOL
  396. {
  397. MessageManager::getInstance();
  398. }
  399. }
  400. JUCE_API void JUCE_CALLTYPE shutdownJuce_GUI()
  401. {
  402. JUCE_AUTORELEASEPOOL
  403. {
  404. DeletedAtShutdown::deleteAll();
  405. MessageManager::deleteInstance();
  406. }
  407. }
  408. static int numScopedInitInstances = 0;
  409. ScopedJuceInitialiser_GUI::ScopedJuceInitialiser_GUI() { if (numScopedInitInstances++ == 0) initialiseJuce_GUI(); }
  410. ScopedJuceInitialiser_GUI::~ScopedJuceInitialiser_GUI() { if (--numScopedInitInstances == 0) shutdownJuce_GUI(); }
  411. } // namespace juce