Loading...
Searching...
No Matches
juce_Thread.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - 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 JUCE_API Thread
{
public:
//==============================================================================
static constexpr size_t osDefaultStackSize { 0 };
//==============================================================================
enum class Priority
{
highest = 2,
high = 1,
normal = 0,
low = -1,
background = -2
};
//==============================================================================
struct RealtimeOptions
{
[[nodiscard]] RealtimeOptions withPriority (int newPriority) const
{
jassert (isPositiveAndNotGreaterThan (newPriority, 10));
return withMember (*this, &RealtimeOptions::priority, juce::jlimit (0, 10, newPriority));
}
[[nodiscard]] RealtimeOptions withProcessingTimeMs (double newProcessingTimeMs) const
{
jassert (newProcessingTimeMs > 0.0);
return withMember (*this, &RealtimeOptions::processingTimeMs, newProcessingTimeMs);
}
[[nodiscard]] RealtimeOptions withMaximumProcessingTimeMs (double newMaximumProcessingTimeMs) const
{
jassert (newMaximumProcessingTimeMs > 0.0);
return withMember (*this, &RealtimeOptions::maximumProcessingTimeMs, newMaximumProcessingTimeMs);
}
[[nodiscard]] RealtimeOptions withApproximateAudioProcessingTime (int samplesPerFrame, double sampleRate) const
{
jassert (samplesPerFrame > 0);
jassert (sampleRate > 0.0);
const auto approxFrameTimeMs = (samplesPerFrame / sampleRate) * 1000.0;
return withMaximumProcessingTimeMs (approxFrameTimeMs);
}
[[nodiscard]] RealtimeOptions withPeriodMs (double newPeriodMs) const
{
jassert (newPeriodMs > 0.0);
return withMember (*this, &RealtimeOptions::periodMs, newPeriodMs);
}
[[nodiscard]] RealtimeOptions withPeriodHz (double newPeriodHz) const
{
jassert (newPeriodHz > 0.0);
return withPeriodMs (1'000.0 / newPeriodHz);
}
[[nodiscard]] int getPriority() const
{
return priority;
}
[[nodiscard]] std::optional<double> getProcessingTimeMs() const
{
return processingTimeMs;
}
[[nodiscard]] std::optional<double> getMaximumProcessingTimeMs() const
{
return maximumProcessingTimeMs;
}
[[nodiscard]] std::optional<double> getPeriodMs() const
{
return periodMs;
}
private:
int priority { 5 };
std::optional<double> processingTimeMs;
std::optional<double> maximumProcessingTimeMs;
std::optional<double> periodMs{};
};
//==============================================================================
explicit Thread (const String& threadName, size_t threadStackSize = osDefaultStackSize);
virtual ~Thread();
//==============================================================================
virtual void run() = 0;
//==============================================================================
bool startThread();
bool startThread (Priority newPriority);
bool startRealtimeThread (const RealtimeOptions& options);
bool stopThread (int timeOutMilliseconds);
//==============================================================================
static bool launch (std::function<void()> functionToRun);
//==============================================================================
static bool launch (Priority priority, std::function<void()> functionToRun);
//==============================================================================
bool isThreadRunning() const;
void signalThreadShouldExit();
bool threadShouldExit() const;
static bool currentThreadShouldExit();
bool waitForThreadToExit (int timeOutMilliseconds) const;
//==============================================================================
class JUCE_API Listener
{
public:
virtual ~Listener() = default;
virtual void exitSignalSent() = 0;
};
void addListener (Listener*);
void removeListener (Listener*);
bool isRealtime() const;
//==============================================================================
void setAffinityMask (uint32 affinityMask);
static void JUCE_CALLTYPE setCurrentThreadAffinityMask (uint32 affinityMask);
//==============================================================================
static void JUCE_CALLTYPE sleep (int milliseconds);
static void JUCE_CALLTYPE yield();
//==============================================================================
bool wait (double timeOutMilliseconds) const;
void notify() const;
//==============================================================================
using ThreadID = void*;
static ThreadID JUCE_CALLTYPE getCurrentThreadId();
static Thread* JUCE_CALLTYPE getCurrentThread();
ThreadID getThreadId() const noexcept;
const String& getThreadName() const noexcept { return threadName; }
static void JUCE_CALLTYPE setCurrentThreadName (const String& newThreadName);
#if JUCE_ANDROID || DOXYGEN
//==============================================================================
static void initialiseJUCE (void* jniEnv, void* jContext);
#endif
protected:
//==============================================================================
Priority getPriority() const;
bool setPriority (Priority newPriority);
private:
//==============================================================================
const String threadName;
std::atomic<void*> threadHandle { nullptr };
std::atomic<ThreadID> threadId { nullptr };
std::optional<RealtimeOptions> realtimeOptions = {};
CriticalSection startStopLock;
WaitableEvent startSuspensionEvent, defaultEvent;
size_t threadStackSize;
uint32 affinityMask = 0;
bool deleteOnThreadEnd = false;
std::atomic<bool> shouldExit { false };
ListenerList<Listener, Array<Listener*, CriticalSection>> listeners;
#if JUCE_ANDROID || JUCE_LINUX || JUCE_BSD
std::atomic<Priority> priority;
#endif
#ifndef DOXYGEN
friend void JUCE_API juce_threadEntryPoint (void*);
#endif
bool startThreadInternal (Priority);
bool createNativeThread (Priority);
void closeThreadHandle();
void killThread();
void threadEntryPoint();
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Thread)
};
} // namespace juce
juce::String
Definition juce_String.h:53
juce::Thread::Listener
Definition juce_Thread.h:396
juce::Thread::Listener::exitSignalSent
virtual void exitSignalSent()=0
juce::Thread::setAffinityMask
void setAffinityMask(uint32 affinityMask)
Definition juce_Thread.cpp:282
juce::Thread::setCurrentThreadAffinityMask
static void JUCE_CALLTYPE setCurrentThreadAffinityMask(uint32 affinityMask)
juce::Thread::ThreadID
void * ThreadID
Definition juce_Thread.h:477
juce::Thread::sleep
static void JUCE_CALLTYPE sleep(int milliseconds)
juce::Thread::isRealtime
bool isRealtime() const
Definition juce_Thread.cpp:277
juce::Thread::getCurrentThread
static Thread *JUCE_CALLTYPE getCurrentThread()
Definition juce_Thread.cpp:185
juce::Thread::wait
bool wait(double timeOutMilliseconds) const
Definition juce_Thread.cpp:288
juce::Thread::launch
static bool launch(std::function< void()> functionToRun)
Definition juce_Thread.cpp:314
juce::Thread::getThreadId
ThreadID getThreadId() const noexcept
Definition juce_Thread.cpp:190
juce::Thread::waitForThreadToExit
bool waitForThreadToExit(int timeOutMilliseconds) const
Definition juce_Thread.cpp:215
juce::Thread::getPriority
Priority getPriority() const
juce::Thread::setCurrentThreadName
static void JUCE_CALLTYPE setCurrentThreadName(const String &newThreadName)
juce::Thread::setPriority
bool setPriority(Priority newPriority)
juce::Thread::Thread
Thread(const String &threadName, size_t threadStackSize=osDefaultStackSize)
Definition juce_Thread.cpp:26
juce::Thread::run
virtual void run()=0
juce::Thread::startThread
bool startThread()
Definition juce_Thread.cpp:145
juce::Thread::currentThreadShouldExit
static bool currentThreadShouldExit()
Definition juce_Thread.cpp:207
juce::Thread::threadShouldExit
bool threadShouldExit() const
Definition juce_Thread.cpp:202
juce::Thread::startRealtimeThread
bool startRealtimeThread(const RealtimeOptions &options)
Definition juce_Thread.cpp:163
juce::Thread::yield
static void JUCE_CALLTYPE yield()
juce::Thread::getCurrentThreadId
static ThreadID JUCE_CALLTYPE getCurrentThreadId()
juce::Thread::getThreadName
const String & getThreadName() const noexcept
Definition juce_Thread.h:506
juce::Thread::stopThread
bool stopThread(int timeOutMilliseconds)
Definition juce_Thread.cpp:233
juce::Thread::notify
void notify() const
Definition juce_Thread.cpp:293
juce::Thread::addListener
void addListener(Listener *)
Definition juce_Thread.cpp:267
juce::Thread::signalThreadShouldExit
void signalThreadShouldExit()
Definition juce_Thread.cpp:196
juce::Thread::Priority
Priority
Definition juce_Thread.h:54
juce::Thread::isThreadRunning
bool isThreadRunning() const
Definition juce_Thread.cpp:180
juce::Thread::removeListener
void removeListener(Listener *)
Definition juce_Thread.cpp:272
juce::Thread::RealtimeOptions
Definition juce_Thread.h:77
juce::Thread::RealtimeOptions::withPeriodHz
RealtimeOptions withPeriodHz(double newPeriodHz) const
Definition juce_Thread.h:155
juce::Thread::RealtimeOptions::getMaximumProcessingTimeMs
std::optional< double > getMaximumProcessingTimeMs() const
Definition juce_Thread.h:185
juce::Thread::RealtimeOptions::withPeriodMs
RealtimeOptions withPeriodMs(double newPeriodMs) const
Definition juce_Thread.h:141
juce::Thread::RealtimeOptions::withMaximumProcessingTimeMs
RealtimeOptions withMaximumProcessingTimeMs(double newMaximumProcessingTimeMs) const
Definition juce_Thread.h:108
juce::Thread::RealtimeOptions::getPeriodMs
std::optional< double > getPeriodMs() const
Definition juce_Thread.h:195
juce::Thread::RealtimeOptions::withApproximateAudioProcessingTime
RealtimeOptions withApproximateAudioProcessingTime(int samplesPerFrame, double sampleRate) const
Definition juce_Thread.h:124
juce::Thread::RealtimeOptions::getProcessingTimeMs
std::optional< double > getProcessingTimeMs() const
Definition juce_Thread.h:175
juce::Thread::RealtimeOptions::withPriority
RealtimeOptions withPriority(int newPriority) const
Definition juce_Thread.h:84
juce::Thread::RealtimeOptions::getPriority
int getPriority() const
Definition juce_Thread.h:165
juce::Thread::RealtimeOptions::withProcessingTimeMs
RealtimeOptions withProcessingTimeMs(double newProcessingTimeMs) const
Definition juce_Thread.h:96