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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 && ! JUCE_IOS
  27. runLoop = CFRunLoopGetMain();
  28. #else
  29. runLoop = CFRunLoopGetCurrent();
  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()
  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. CFRunLoopSourceSignal (runLoopSource);
  48. CFRunLoopWakeUp (runLoop);
  49. }
  50. private:
  51. ReferenceCountedArray <MessageManager::MessageBase, CriticalSection> messages;
  52. CFRunLoopRef runLoop;
  53. CFRunLoopSourceRef runLoopSource;
  54. bool deliverNextMessage()
  55. {
  56. const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
  57. if (nextMessage == nullptr)
  58. return false;
  59. JUCE_AUTORELEASEPOOL
  60. {
  61. JUCE_TRY
  62. {
  63. nextMessage->messageCallback();
  64. }
  65. JUCE_CATCH_EXCEPTION
  66. }
  67. return true;
  68. }
  69. void runLoopCallback()
  70. {
  71. for (int i = 4; --i >= 0;)
  72. if (! deliverNextMessage())
  73. return;
  74. CFRunLoopSourceSignal (runLoopSource);
  75. CFRunLoopWakeUp (runLoop);
  76. }
  77. static void runLoopSourceCallback (void* info)
  78. {
  79. static_cast <MessageQueue*> (info)->runLoopCallback();
  80. }
  81. };
  82. #endif // JUCE_OSX_MESSAGEQUEUE_H_INCLUDED