2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 //==============================================================================
27 /* An internal message pump class used in OSX and iOS. */
34 runLoop
= CFRunLoopGetCurrent();
36 runLoop
= CFRunLoopGetMain();
39 CFRunLoopSourceContext sourceContext
;
40 zerostruct (sourceContext
); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
41 sourceContext
.info
= this;
42 sourceContext
.perform
= runLoopSourceCallback
;
43 runLoopSource
.reset (CFRunLoopSourceCreate (kCFAllocatorDefault
, 1, &sourceContext
));
44 CFRunLoopAddSource (runLoop
, runLoopSource
.get(), kCFRunLoopCommonModes
);
47 ~MessageQueue() noexcept
49 CFRunLoopRemoveSource (runLoop
, runLoopSource
.get(), kCFRunLoopCommonModes
);
50 CFRunLoopSourceInvalidate (runLoopSource
.get());
53 void post (MessageManager::MessageBase
* const message
)
55 messages
.add (message
);
60 ReferenceCountedArray
<MessageManager::MessageBase
, CriticalSection
> messages
;
62 CFUniquePtr
<CFRunLoopSourceRef
> runLoopSource
;
64 void wakeUp() noexcept
66 CFRunLoopSourceSignal (runLoopSource
.get());
67 CFRunLoopWakeUp (runLoop
);
70 bool deliverNextMessage()
72 const MessageManager::MessageBase::Ptr
nextMessage (messages
.removeAndReturn (0));
74 if (nextMessage
== nullptr)
81 nextMessage
->messageCallback();
89 void runLoopCallback() noexcept
91 for (int i
= 4; --i
>= 0;)
92 if (! deliverNextMessage())
98 static void runLoopSourceCallback (void* info
) noexcept
100 static_cast<MessageQueue
*> (info
)->runLoopCallback();