OpenShot Audio Library | OpenShotAudio: juce_Thread.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0 |

Loading...
Searching...
No Matches
juce_Thread.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
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.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
42 class JUCE_API Thread
43{
44public:
45 //==============================================================================
46 static constexpr size_t osDefaultStackSize { 0 };
47
48 //==============================================================================
53 enum class Priority
54 {
56 highest = 2,
57
59 high = 1,
60
62 normal = 0,
63
65 low = -1,
66
68 background = -2
69 };
70
71 //==============================================================================
76 struct RealtimeOptions
77 {
84 [[nodiscard]] RealtimeOptions withPriority (int newPriority) const
85 {
86 jassert (isPositiveAndNotGreaterThan (newPriority, 10));
87 return withMember (*this, &RealtimeOptions::priority, juce::jlimit (0, 10, newPriority));
88 }
89
96 [[nodiscard]] RealtimeOptions withProcessingTimeMs (double newProcessingTimeMs) const
97 {
98 jassert (newProcessingTimeMs > 0.0);
99 return withMember (*this, &RealtimeOptions::processingTimeMs, newProcessingTimeMs);
100 }
101
108 [[nodiscard]] RealtimeOptions withMaximumProcessingTimeMs (double newMaximumProcessingTimeMs) const
109 {
110 jassert (newMaximumProcessingTimeMs > 0.0);
111 return withMember (*this, &RealtimeOptions::maximumProcessingTimeMs, newMaximumProcessingTimeMs);
112 }
113
124 [[nodiscard]] RealtimeOptions withApproximateAudioProcessingTime (int samplesPerFrame, double sampleRate) const
125 {
126 jassert (samplesPerFrame > 0);
127 jassert (sampleRate > 0.0);
128
129 const auto approxFrameTimeMs = (samplesPerFrame / sampleRate) * 1000.0;
130 return withMaximumProcessingTimeMs (approxFrameTimeMs);
131 }
132
141 [[nodiscard]] RealtimeOptions withPeriodMs (double newPeriodMs) const
142 {
143 jassert (newPeriodMs > 0.0);
144 return withMember (*this, &RealtimeOptions::periodMs, newPeriodMs);
145 }
146
155 [[nodiscard]] RealtimeOptions withPeriodHz (double newPeriodHz) const
156 {
157 jassert (newPeriodHz > 0.0);
158 return withPeriodMs (1'000.0 / newPeriodHz);
159 }
160
165 [[nodiscard]] int getPriority() const
166 {
167 return priority;
168 }
169
175 [[nodiscard]] std::optional<double> getProcessingTimeMs() const
176 {
177 return processingTimeMs;
178 }
179
185 [[nodiscard]] std::optional<double> getMaximumProcessingTimeMs() const
186 {
187 return maximumProcessingTimeMs;
188 }
189
195 [[nodiscard]] std::optional<double> getPeriodMs() const
196 {
197 return periodMs;
198 }
199
200 private:
201 int priority { 5 };
202 std::optional<double> processingTimeMs;
203 std::optional<double> maximumProcessingTimeMs;
204 std::optional<double> periodMs{};
205 };
206
207 //==============================================================================
220 explicit Thread (const String& threadName, size_t threadStackSize = osDefaultStackSize);
221
229 virtual ~Thread();
230
231 //==============================================================================
240 virtual void run() = 0;
241
242 //==============================================================================
256 bool startThread();
257
274 bool startThread (Priority newPriority);
275
287 bool startRealtimeThread (const RealtimeOptions& options);
288
309 bool stopThread (int timeOutMilliseconds);
310
311 //==============================================================================
329 static bool launch (std::function<void()> functionToRun);
330
331 //==============================================================================
348 static bool launch (Priority priority, std::function<void()> functionToRun);
349
350 //==============================================================================
352 bool isThreadRunning() const;
353
365 void signalThreadShouldExit();
366
374 bool threadShouldExit() const;
375
382 static bool currentThreadShouldExit();
383
391 bool waitForThreadToExit (int timeOutMilliseconds) const;
392
393 //==============================================================================
395 class JUCE_API Listener
396 {
397 public:
398 virtual ~Listener() = default;
399
403 virtual void exitSignalSent() = 0;
404 };
405
411 void addListener (Listener*);
412
414 void removeListener (Listener*);
415
417 bool isRealtime() const;
418
419 //==============================================================================
427 void setAffinityMask (uint32 affinityMask);
428
435 static void JUCE_CALLTYPE setCurrentThreadAffinityMask (uint32 affinityMask);
436
437 //==============================================================================
445 static void JUCE_CALLTYPE sleep (int milliseconds);
446
452 static void JUCE_CALLTYPE yield();
453
454 //==============================================================================
462 bool wait (double timeOutMilliseconds) const;
463
470 void notify() const;
471
472 //==============================================================================
477 using ThreadID = void*;
478
486 static ThreadID JUCE_CALLTYPE getCurrentThreadId();
487
493 static Thread* JUCE_CALLTYPE getCurrentThread();
494
503 ThreadID getThreadId() const noexcept;
504
506 const String& getThreadName() const noexcept { return threadName; }
507
512 static void JUCE_CALLTYPE setCurrentThreadName (const String& newThreadName);
513
514 #if JUCE_ANDROID || DOXYGEN
515 //==============================================================================
553 static void initialiseJUCE (void* jniEnv, void* jContext);
554 #endif
555
556protected:
557 //==============================================================================
565 Priority getPriority() const;
566
579 bool setPriority (Priority newPriority);
580
581private:
582 //==============================================================================
583 const String threadName;
584 std::atomic<void*> threadHandle { nullptr };
585 std::atomic<ThreadID> threadId { nullptr };
586 std::optional<RealtimeOptions> realtimeOptions = {};
587 CriticalSection startStopLock;
588 WaitableEvent startSuspensionEvent, defaultEvent;
589 size_t threadStackSize;
590 uint32 affinityMask = 0;
591 bool deleteOnThreadEnd = false;
592 std::atomic<bool> shouldExit { false };
593 ListenerList<Listener, Array<Listener*, CriticalSection>> listeners;
594
595 #if JUCE_ANDROID || JUCE_LINUX || JUCE_BSD
596 std::atomic<Priority> priority;
597 #endif
598
599 #ifndef DOXYGEN
600 friend void JUCE_API juce_threadEntryPoint (void*);
601 #endif
602
603 bool startThreadInternal (Priority);
604 bool createNativeThread (Priority);
605 void closeThreadHandle();
606 void killThread();
607 void threadEntryPoint();
608
609 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Thread)
610};
611
612} // 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::Priority::low
@ low
Definition juce_Thread.h:65
juce::Thread::Priority::high
@ high
Definition juce_Thread.h:59
juce::Thread::Priority::background
@ background
Definition juce_Thread.h:68
juce::Thread::Priority::highest
@ highest
Definition juce_Thread.h:56
juce::Thread::Priority::normal
@ normal
Definition juce_Thread.h:62
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