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_osx_MessageQueue.h 3.1KB

9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef JUCE_OSX_MESSAGEQUEUE_H_INCLUDED
  18. #define JUCE_OSX_MESSAGEQUEUE_H_INCLUDED
  19. //==============================================================================
  20. /* An internal message pump class used in OSX and iOS. */
  21. class MessageQueue
  22. {
  23. public:
  24. MessageQueue()
  25. {
  26. #if JUCE_IOS
  27. runLoop = CFRunLoopGetCurrent();
  28. #else
  29. runLoop = CFRunLoopGetMain();
  30. #endif
  31. CFRunLoopSourceContext sourceContext;
  32. zerostruct (sourceContext); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
  33. sourceContext.info = this;
  34. sourceContext.perform = runLoopSourceCallback;
  35. runLoopSource = CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext);
  36. CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  37. }
  38. ~MessageQueue() noexcept
  39. {
  40. CFRunLoopRemoveSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  41. CFRunLoopSourceInvalidate (runLoopSource);
  42. CFRelease (runLoopSource);
  43. }
  44. void post (MessageManager::MessageBase* const message)
  45. {
  46. messages.add (message);
  47. wakeUp();
  48. }
  49. private:
  50. ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> messages;
  51. CFRunLoopRef runLoop;
  52. CFRunLoopSourceRef runLoopSource;
  53. void wakeUp() noexcept
  54. {
  55. CFRunLoopSourceSignal (runLoopSource);
  56. CFRunLoopWakeUp (runLoop);
  57. }
  58. bool deliverNextMessage()
  59. {
  60. const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
  61. if (nextMessage == nullptr)
  62. return false;
  63. JUCE_AUTORELEASEPOOL
  64. {
  65. JUCE_TRY
  66. {
  67. nextMessage->messageCallback();
  68. }
  69. JUCE_CATCH_EXCEPTION
  70. }
  71. return true;
  72. }
  73. void runLoopCallback() noexcept
  74. {
  75. for (int i = 4; --i >= 0;)
  76. if (! deliverNextMessage())
  77. return;
  78. wakeUp();
  79. }
  80. static void runLoopSourceCallback (void* info) noexcept
  81. {
  82. static_cast<MessageQueue*> (info)->runLoopCallback();
  83. }
  84. };
  85. #endif // JUCE_OSX_MESSAGEQUEUE_H_INCLUDED