1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Thread: Introduce a new Thread backend

This is a breaking change - see BREAKING-CHANGES.txt
This commit is contained in:
chroma 2022-10-18 11:47:18 +01:00
parent 621e14d092
commit d3cff375be
39 changed files with 849 additions and 603 deletions

View file

@ -25,8 +25,6 @@
#if JUCE_LINUX || JUCE_BSD
#include <thread>
namespace juce
{
@ -34,15 +32,15 @@ namespace juce
bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages);
/** @internal */
class MessageThread
class MessageThread : public Thread
{
public:
MessageThread()
MessageThread() : Thread ("JUCE Plugin Message Thread")
{
start();
}
~MessageThread()
~MessageThread() override
{
MessageManager::getInstance()->stopDispatchLoop();
stop();
@ -50,51 +48,37 @@ public:
void start()
{
if (isRunning())
stop();
startThread (Priority::high);
shouldExit = false;
thread = std::thread { [this]
{
Thread::setCurrentThreadPriority (7);
Thread::setCurrentThreadName ("JUCE Plugin Message Thread");
MessageManager::getInstance()->setCurrentThreadAsMessageThread();
XWindowSystem::getInstance();
threadInitialised.signal();
for (;;)
{
if (! dispatchNextMessageOnSystemQueue (true))
Thread::sleep (1);
if (shouldExit)
break;
}
} };
threadInitialised.wait();
// Wait for setCurrentThreadAsMessageThread() and getInstance to be executed
// before leaving this method
threadInitialised.wait (10000);
}
void stop()
{
if (! isRunning())
return;
shouldExit = true;
thread.join();
signalThreadShouldExit();
stopThread (-1);
}
bool isRunning() const noexcept { return thread.joinable(); }
bool isRunning() const noexcept { return isThreadRunning(); }
void run() override
{
MessageManager::getInstance()->setCurrentThreadAsMessageThread();
XWindowSystem::getInstance();
threadInitialised.signal();
while (! threadShouldExit())
{
if (! dispatchNextMessageOnSystemQueue (true))
Thread::sleep (1);
}
}
private:
WaitableEvent threadInitialised;
std::thread thread;
std::atomic<bool> shouldExit { false };
JUCE_DECLARE_NON_MOVEABLE (MessageThread)
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MessageThread)
};