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.

118 lines
2.9KB

  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. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #if JUCE_LINUX || JUCE_BSD
  19. namespace juce
  20. {
  21. // Implemented in juce_linux_Messaging.cpp
  22. bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages);
  23. /** @internal */
  24. class MessageThread : public Thread
  25. {
  26. public:
  27. MessageThread() : Thread ("JUCE Plugin Message Thread")
  28. {
  29. start();
  30. }
  31. ~MessageThread() override
  32. {
  33. MessageManager::getInstance()->stopDispatchLoop();
  34. stop();
  35. }
  36. void start()
  37. {
  38. startThread (Priority::high);
  39. // Wait for setCurrentThreadAsMessageThread() and getInstance to be executed
  40. // before leaving this method
  41. threadInitialised.wait (10000);
  42. }
  43. void stop()
  44. {
  45. signalThreadShouldExit();
  46. stopThread (-1);
  47. }
  48. bool isRunning() const noexcept { return isThreadRunning(); }
  49. void run() override
  50. {
  51. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  52. XWindowSystem::getInstance();
  53. threadInitialised.signal();
  54. while (! threadShouldExit())
  55. {
  56. if (! dispatchNextMessageOnSystemQueue (true))
  57. Thread::sleep (1);
  58. }
  59. }
  60. private:
  61. WaitableEvent threadInitialised;
  62. JUCE_DECLARE_NON_MOVEABLE (MessageThread)
  63. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MessageThread)
  64. };
  65. //==============================================================================
  66. /** @internal */
  67. class HostDrivenEventLoop
  68. {
  69. public:
  70. HostDrivenEventLoop()
  71. {
  72. messageThread->stop();
  73. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  74. }
  75. void processPendingEvents()
  76. {
  77. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  78. for (;;)
  79. if (! dispatchNextMessageOnSystemQueue (true))
  80. return;
  81. }
  82. ~HostDrivenEventLoop()
  83. {
  84. messageThread->start();
  85. }
  86. private:
  87. SharedResourcePointer<MessageThread> messageThread;
  88. };
  89. } // namespace juce
  90. #endif