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.

98 lines
2.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #if JUCE_LINUX || JUCE_BSD
  14. #include <thread>
  15. namespace juce
  16. {
  17. // Implemented in juce_linux_Messaging.cpp
  18. bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages);
  19. /** @internal */
  20. class MessageThread
  21. {
  22. public:
  23. MessageThread()
  24. {
  25. start();
  26. }
  27. ~MessageThread()
  28. {
  29. MessageManager::getInstance()->stopDispatchLoop();
  30. stop();
  31. }
  32. void start()
  33. {
  34. if (isRunning())
  35. stop();
  36. shouldExit = false;
  37. thread = std::thread { [this]
  38. {
  39. Thread::setCurrentThreadPriority (7);
  40. Thread::setCurrentThreadName ("JUCE Plugin Message Thread");
  41. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  42. XWindowSystem::getInstance();
  43. threadInitialised.signal();
  44. for (;;)
  45. {
  46. if (! dispatchNextMessageOnSystemQueue (true))
  47. Thread::sleep (1);
  48. if (shouldExit)
  49. break;
  50. }
  51. } };
  52. threadInitialised.wait();
  53. }
  54. void stop()
  55. {
  56. if (! isRunning())
  57. return;
  58. shouldExit = true;
  59. thread.join();
  60. }
  61. bool isRunning() const noexcept { return thread.joinable(); }
  62. private:
  63. WaitableEvent threadInitialised;
  64. std::thread thread;
  65. std::atomic<bool> shouldExit { false };
  66. JUCE_DECLARE_NON_MOVEABLE (MessageThread)
  67. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MessageThread)
  68. };
  69. } // namespace juce
  70. #endif