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.

114 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_OSX_MESSAGEQUEUE_H_INCLUDED
  24. #define JUCE_OSX_MESSAGEQUEUE_H_INCLUDED
  25. //==============================================================================
  26. /* An internal message pump class used in OSX and iOS. */
  27. class MessageQueue
  28. {
  29. public:
  30. MessageQueue()
  31. {
  32. #if JUCE_IOS
  33. runLoop = CFRunLoopGetCurrent();
  34. #else
  35. runLoop = CFRunLoopGetMain();
  36. #endif
  37. CFRunLoopSourceContext sourceContext;
  38. zerostruct (sourceContext); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
  39. sourceContext.info = this;
  40. sourceContext.perform = runLoopSourceCallback;
  41. runLoopSource = CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext);
  42. CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  43. }
  44. ~MessageQueue() noexcept
  45. {
  46. CFRunLoopRemoveSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  47. CFRunLoopSourceInvalidate (runLoopSource);
  48. CFRelease (runLoopSource);
  49. }
  50. void post (MessageManager::MessageBase* const message)
  51. {
  52. messages.add (message);
  53. wakeUp();
  54. }
  55. private:
  56. ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> messages;
  57. CFRunLoopRef runLoop;
  58. CFRunLoopSourceRef runLoopSource;
  59. void wakeUp() noexcept
  60. {
  61. CFRunLoopSourceSignal (runLoopSource);
  62. CFRunLoopWakeUp (runLoop);
  63. }
  64. bool deliverNextMessage()
  65. {
  66. const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
  67. if (nextMessage == nullptr)
  68. return false;
  69. JUCE_AUTORELEASEPOOL
  70. {
  71. JUCE_TRY
  72. {
  73. nextMessage->messageCallback();
  74. }
  75. JUCE_CATCH_EXCEPTION
  76. }
  77. return true;
  78. }
  79. void runLoopCallback() noexcept
  80. {
  81. for (int i = 4; --i >= 0;)
  82. if (! deliverNextMessage())
  83. return;
  84. wakeUp();
  85. }
  86. static void runLoopSourceCallback (void* info) noexcept
  87. {
  88. static_cast<MessageQueue*> (info)->runLoopCallback();
  89. }
  90. };
  91. #endif // JUCE_OSX_MESSAGEQUEUE_H_INCLUDED