1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class InternalMessageQueue
{
public:
InternalMessageQueue()
{
auto err = ::socketpair (AF_LOCAL, SOCK_STREAM, 0, msgpipe);
jassertquiet (err == 0);
LinuxEventLoop::registerFdCallback (getReadHandle(),
[this] (int fd)
{
while (auto msg = popNextMessage (fd))
{
JUCE_TRY
{
msg->messageCallback();
}
JUCE_CATCH_EXCEPTION
}
});
}
~InternalMessageQueue()
{
LinuxEventLoop::unregisterFdCallback (getReadHandle());
close (getReadHandle());
close (getWriteHandle());
clearSingletonInstance();
}
//==============================================================================
void postMessage (MessageManager::MessageBase* const msg) noexcept
{
ScopedLock sl (lock);
queue.add (msg);
if (bytesInSocket < maxBytesInSocketQueue)
{
bytesInSocket++;
ScopedUnlock ul (lock);
unsigned char x = 0xff;
auto numBytes = write (getWriteHandle(), &x, 1);
ignoreUnused (numBytes);
}
}
//==============================================================================
JUCE_DECLARE_SINGLETON (InternalMessageQueue, false)
private:
CriticalSection lock;
ReferenceCountedArray <MessageManager::MessageBase> queue;
int msgpipe[2];
int bytesInSocket = 0;
static constexpr int maxBytesInSocketQueue = 128;
int getWriteHandle() const noexcept { return msgpipe[0]; }
int getReadHandle() const noexcept { return msgpipe[1]; }
MessageManager::MessageBase::Ptr popNextMessage (int fd) noexcept
{
const ScopedLock sl (lock);
if (bytesInSocket > 0)
{
--bytesInSocket;
ScopedUnlock ul (lock);
unsigned char x;
auto numBytes = read (fd, &x, 1);
ignoreUnused (numBytes);
}
return queue.removeAndReturn (0);
}
};
JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
//==============================================================================
struct InternalRunLoop
{
public:
InternalRunLoop()
{
fdReadCallbacks.reserve (16);
}
void registerFdCallback (int fd, std::function<void (int)>&& cb, short eventMask)
{
const ScopedLock sl (lock);
if (shouldDeferModifyingReadCallbacks)
{
deferredReadCallbackModifications.emplace_back ([this, fd, cb, eventMask]() mutable
{
registerFdCallback (fd, std::move (cb), eventMask);
});
return;
}
fdReadCallbacks.push_back ({ fd, std::move (cb) });
pfds.push_back ({ fd, eventMask, 0 });
}
void unregisterFdCallback (int fd)
{
const ScopedLock sl (lock);
if (shouldDeferModifyingReadCallbacks)
{
deferredReadCallbackModifications.emplace_back ([this, fd] { unregisterFdCallback (fd); });
return;
}
{
auto removePredicate = [=] (const std::pair<int, std::function<void (int)>>& cb) { return cb.first == fd; };
fdReadCallbacks.erase (std::remove_if (std::begin (fdReadCallbacks), std::end (fdReadCallbacks), removePredicate),
std::end (fdReadCallbacks));
}
{
auto removePredicate = [=] (const pollfd& pfd) { return pfd.fd == fd; };
pfds.erase (std::remove_if (std::begin (pfds), std::end (pfds), removePredicate),
std::end (pfds));
}
}
bool dispatchPendingEvents()
{
const ScopedLock sl (lock);
if (poll (&pfds.front(), static_cast<nfds_t> (pfds.size()), 0) == 0)
return false;
bool eventWasSent = false;
for (auto& pfd : pfds)
{
if (pfd.revents == 0)
continue;
pfd.revents = 0;
auto fd = pfd.fd;
for (auto& fdAndCallback : fdReadCallbacks)
{
if (fdAndCallback.first == fd)
{
{
ScopedValueSetter<bool> insideFdReadCallback (shouldDeferModifyingReadCallbacks, true);
fdAndCallback.second (fd);
}
if (! deferredReadCallbackModifications.empty())
{
for (auto& deferredRegisterEvent : deferredReadCallbackModifications)
deferredRegisterEvent();
deferredReadCallbackModifications.clear();
// elements may have been removed from the fdReadCallbacks/pfds array so we really need
// to call poll again
return true;
}
eventWasSent = true;
}
}
}
return eventWasSent;
}
void sleepUntilNextEvent (int timeoutMs)
{
poll (&pfds.front(), static_cast<nfds_t> (pfds.size()), timeoutMs);
}
std::vector<std::pair<int, std::function<void (int)>>> getFdReadCallbacks()
{
const ScopedLock sl (lock);
return fdReadCallbacks;
}
//==============================================================================
JUCE_DECLARE_SINGLETON (InternalRunLoop, false)
private:
CriticalSection lock;
std::vector<std::pair<int, std::function<void (int)>>> fdReadCallbacks;
std::vector<pollfd> pfds;
bool shouldDeferModifyingReadCallbacks = false;
std::vector<std::function<void()>> deferredReadCallbackModifications;
};
JUCE_IMPLEMENT_SINGLETON (InternalRunLoop)
//==============================================================================
namespace LinuxErrorHandling
{
static bool keyboardBreakOccurred = false;
void keyboardBreakSignalHandler (int sig)
{
if (sig == SIGINT)
keyboardBreakOccurred = true;
}
void installKeyboardBreakHandler()
{
struct sigaction saction;
sigset_t maskSet;
sigemptyset (&maskSet);
saction.sa_handler = keyboardBreakSignalHandler;
saction.sa_mask = maskSet;
saction.sa_flags = 0;
sigaction (SIGINT, &saction, nullptr);
}
}
//==============================================================================
void MessageManager::doPlatformSpecificInitialisation()
{
if (JUCEApplicationBase::isStandaloneApp())
LinuxErrorHandling::installKeyboardBreakHandler();
InternalRunLoop::getInstance();
InternalMessageQueue::getInstance();
}
void MessageManager::doPlatformSpecificShutdown()
{
InternalMessageQueue::deleteInstance();
InternalRunLoop::deleteInstance();
}
bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
{
queue->postMessage (message);
return true;
}
return false;
}
void MessageManager::broadcastMessage (const String&)
{
// TODO
}
// this function expects that it will NEVER be called simultaneously for two concurrent threads
bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
{
for (;;)
{
if (LinuxErrorHandling::keyboardBreakOccurred)
JUCEApplicationBase::quit();
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
{
if (runLoop->dispatchPendingEvents())
break;
if (returnIfNoPendingMessages)
return false;
runLoop->sleepUntilNextEvent (2000);
}
}
return true;
}
//==============================================================================
void LinuxEventLoop::registerFdCallback (int fd, std::function<void (int)> readCallback, short eventMask)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->registerFdCallback (fd, std::move (readCallback), eventMask);
}
void LinuxEventLoop::unregisterFdCallback (int fd)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->unregisterFdCallback (fd);
}
} // namespace juce
JUCE_API std::vector<std::pair<int, std::function<void (int)>>> getFdReadCallbacks()
{
using namespace juce;
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
return runLoop->getFdReadCallbacks();
jassertfalse;
return {};
}
|