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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  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.reset (CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext));
  36. CFRunLoopAddSource (runLoop, runLoopSource.get(), kCFRunLoopCommonModes);
  37. }
  38. ~MessageQueue() noexcept
  39. {
  40. CFRunLoopRemoveSource (runLoop, runLoopSource.get(), kCFRunLoopCommonModes);
  41. CFRunLoopSourceInvalidate (runLoopSource.get());
  42. }
  43. void post (MessageManager::MessageBase* const message)
  44. {
  45. messages.add (message);
  46. wakeUp();
  47. }
  48. private:
  49. ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> messages;
  50. CFRunLoopRef runLoop;
  51. CFUniquePtr<CFRunLoopSourceRef> runLoopSource;
  52. void wakeUp() noexcept
  53. {
  54. CFRunLoopSourceSignal (runLoopSource.get());
  55. CFRunLoopWakeUp (runLoop);
  56. }
  57. bool deliverNextMessage()
  58. {
  59. const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
  60. if (nextMessage == nullptr)
  61. return false;
  62. JUCE_AUTORELEASEPOOL
  63. {
  64. JUCE_TRY
  65. {
  66. nextMessage->messageCallback();
  67. }
  68. JUCE_CATCH_EXCEPTION
  69. }
  70. return true;
  71. }
  72. void runLoopCallback() noexcept
  73. {
  74. for (int i = 4; --i >= 0;)
  75. if (! deliverNextMessage())
  76. return;
  77. wakeUp();
  78. }
  79. static void runLoopSourceCallback (void* info) noexcept
  80. {
  81. static_cast<MessageQueue*> (info)->runLoopCallback();
  82. }
  83. };
  84. } // namespace juce