JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Thread.cpp
Go to the documentation of this file.
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//==============================================================================
26Thread::Thread (const String& name, size_t stackSize) : threadName (name),
27 threadStackSize (stackSize)
28{
29}
30
32{
33 if (deleteOnThreadEnd)
34 return;
35
36 /* If your thread class's destructor has been called without first stopping the thread, that
37 means that this partially destructed object is still performing some work - and that's
38 probably a Bad Thing!
39
40 To avoid this type of nastiness, always make sure you call stopThread() before or during
41 your subclass's destructor.
42 */
44
45 stopThread (-1);
46}
47
48//==============================================================================
49// Use a ref-counted object to hold this shared data, so that it can outlive its static
50// shared pointer when threads are still running during static shutdown.
60
61static char currentThreadHolderLock [sizeof (SpinLock)]; // (statically initialised to zeros).
62
63static SpinLock* castToSpinLockWithoutAliasingWarning (void* s)
64{
65 return static_cast<SpinLock*> (s);
66}
67
68static CurrentThreadHolder::Ptr getCurrentThreadHolder()
69{
70 static CurrentThreadHolder::Ptr currentThreadHolder;
71 SpinLock::ScopedLockType lock (*castToSpinLockWithoutAliasingWarning (currentThreadHolderLock));
72
73 if (currentThreadHolder == nullptr)
74 currentThreadHolder = new CurrentThreadHolder();
75
77}
78
79void Thread::threadEntryPoint()
80{
81 const CurrentThreadHolder::Ptr currentThreadHolder (getCurrentThreadHolder());
82 currentThreadHolder->value = this;
83
84 if (threadName.isNotEmpty())
85 setCurrentThreadName (threadName);
86
87 // This 'startSuspensionEvent' protects 'threadId' which is initialised after the platform's native 'CreateThread' method.
88 // This ensures it has been initialised correctly before it reaches this point.
89 if (startSuspensionEvent.wait (10000))
90 {
91 jassert (getCurrentThreadId() == threadId);
92
93 if (affinityMask != 0)
94 setCurrentThreadAffinityMask (affinityMask);
95
96 try
97 {
98 run();
99 }
100 catch (...)
101 {
102 jassertfalse; // Your run() method mustn't throw any exceptions!
103 }
104 }
105
106 currentThreadHolder->value.releaseCurrentThreadStorage();
107
108 // Once closeThreadHandle is called this class may be deleted by a different
109 // thread, so we need to store deleteOnThreadEnd in a local variable.
110 auto shouldDeleteThis = deleteOnThreadEnd;
111 closeThreadHandle();
112
114 delete this;
115}
116
117// used to wrap the incoming call from the platform-specific code
118void JUCE_API juce_threadEntryPoint (void* userData)
119{
120 static_cast<Thread*> (userData)->threadEntryPoint();
121}
122
123//==============================================================================
124bool Thread::startThreadInternal (Priority threadPriority)
125{
126 shouldExit = false;
127
128 // 'priority' is essentially useless on Linux as only realtime
129 // has any options but we need to set this here to satisfy
130 // later queries, otherwise we get inconsistent results across
131 // platforms.
132 #if JUCE_ANDROID || JUCE_LINUX || JUCE_BSD
133 priority = threadPriority;
134 #endif
135
136 if (createNativeThread (threadPriority))
137 {
138 startSuspensionEvent.signal();
139 return true;
140 }
141
142 return false;
143}
144
146{
148}
149
151{
152 const ScopedLock sl (startStopLock);
153
154 if (threadHandle == nullptr)
155 {
156 realtimeOptions.reset();
157 return startThreadInternal (threadPriority);
158 }
159
160 return false;
161}
162
164{
165 const ScopedLock sl (startStopLock);
166
167 if (threadHandle == nullptr)
168 {
169 realtimeOptions = std::make_optional (options);
170
171 if (startThreadInternal (Priority::normal))
172 return true;
173
174 realtimeOptions.reset();
175 }
176
177 return false;
178}
179
181{
182 return threadHandle != nullptr;
183}
184
186{
187 return getCurrentThreadHolder()->value.get();
188}
189
191{
192 return threadId;
193}
194
195//==============================================================================
197{
198 shouldExit = true;
199 listeners.call ([] (Listener& l) { l.exitSignalSent(); });
200}
201
203{
204 return shouldExit;
205}
206
208{
209 if (auto* currentThread = getCurrentThread())
210 return currentThread->threadShouldExit();
211
212 return false;
213}
214
216{
217 // Doh! So how exactly do you expect this thread to wait for itself to stop??
219
221
222 while (isThreadRunning())
223 {
225 return false;
226
227 sleep (2);
228 }
229
230 return true;
231}
232
234{
235 // agh! You can't stop the thread that's calling this method! How on earth
236 // would that work??
238
239 const ScopedLock sl (startStopLock);
240
241 if (isThreadRunning())
242 {
244 notify();
245
246 if (timeOutMilliseconds != 0)
248
249 if (isThreadRunning())
250 {
251 // very bad karma if this point is reached, as there are bound to be
252 // locks and events left in silly states when a thread is killed by force..
254 Logger::writeToLog ("!! killing thread by force !!");
255
256 killThread();
257
258 threadHandle = nullptr;
259 threadId = {};
260 return false;
261 }
262 }
263
264 return true;
265}
266
268{
269 listeners.add (listener);
270}
271
273{
274 listeners.remove (listener);
275}
276
278{
279 return realtimeOptions.has_value();
280}
281
283{
284 affinityMask = newAffinityMask;
285}
286
287//==============================================================================
289{
290 return defaultEvent.wait (timeOutMilliseconds);
291}
292
293void Thread::notify() const
294{
295 defaultEvent.signal();
296}
297
298//==============================================================================
299struct LambdaThread final : public Thread
300{
301 LambdaThread (std::function<void()>&& f) : Thread ("anonymous"), fn (std::move (f)) {}
302
303 void run() override
304 {
305 fn();
306 fn = nullptr; // free any objects that the lambda might contain while the thread is still active
307 }
308
310
312};
313
315{
316 return launch (Priority::normal, std::move (functionToRun));
317}
318
320{
321 auto anon = std::make_unique<LambdaThread> (std::move (functionToRun));
322 anon->deleteOnThreadEnd = true;
323
324 if (anon->startThread (priority))
325 {
326 anon.release();
327 return true;
328 }
329
330 return false;
331}
332
333//==============================================================================
334void SpinLock::enter() const noexcept
335{
336 if (! tryEnter())
337 {
338 for (int i = 20; --i >= 0;)
339 if (tryEnter())
340 return;
341
342 while (! tryEnter())
344 }
345}
346
347//==============================================================================
352
353//==============================================================================
354//==============================================================================
355#if JUCE_UNIT_TESTS
356
357class AtomicTests final : public UnitTest
358{
359public:
361 : UnitTest ("Atomics", UnitTestCategories::threads)
362 {}
363
364 void runTest() override
365 {
366 beginTest ("Misc");
367
368 char a1[7];
369 expect (numElementsInArray (a1) == 7);
370 int a2[3];
371 expect (numElementsInArray (a2) == 3);
372
373 expect (ByteOrder::swap ((uint16) 0x1122) == 0x2211);
374 expect (ByteOrder::swap ((uint32) 0x11223344) == 0x44332211);
375 expect (ByteOrder::swap ((uint64) 0x1122334455667788ULL) == (uint64) 0x8877665544332211LL);
376
377 beginTest ("Atomic int");
378 AtomicTester <int>::testInteger (*this);
379 beginTest ("Atomic unsigned int");
380 AtomicTester <unsigned int>::testInteger (*this);
381 beginTest ("Atomic int32");
382 AtomicTester <int32>::testInteger (*this);
383 beginTest ("Atomic uint32");
384 AtomicTester <uint32>::testInteger (*this);
385 beginTest ("Atomic long");
386 AtomicTester <long>::testInteger (*this);
387 beginTest ("Atomic int*");
388 AtomicTester <int*>::testInteger (*this);
389 beginTest ("Atomic float");
390 AtomicTester <float>::testFloat (*this);
391 #if ! JUCE_64BIT_ATOMICS_UNAVAILABLE // 64-bit intrinsics aren't available on some old platforms
392 beginTest ("Atomic int64");
393 AtomicTester <int64>::testInteger (*this);
394 beginTest ("Atomic uint64");
395 AtomicTester <uint64>::testInteger (*this);
396 beginTest ("Atomic double");
397 AtomicTester <double>::testFloat (*this);
398 #endif
399 beginTest ("Atomic pointer increment/decrement");
400 Atomic<int*> a (a2); int* b (a2);
401 expect (++a == ++b);
402
403 {
404 beginTest ("Atomic void*");
405 Atomic<void*> atomic;
406 void* c;
407
408 atomic.set ((void*) 10);
409 c = (void*) 10;
410
411 expect (atomic.value == c);
412 expect (atomic.get() == c);
413 }
414 }
415
416 template <typename Type>
417 class AtomicTester
418 {
419 public:
420 AtomicTester() = default;
421
422 static void testInteger (UnitTest& test)
423 {
424 Atomic<Type> a, b;
425 Type c;
426
427 a.set ((Type) 10);
428 c = (Type) 10;
429
430 test.expect (a.value == c);
431 test.expect (a.get() == c);
432
433 a += 15;
434 c += 15;
435 test.expect (a.get() == c);
436 a.memoryBarrier();
437
438 a -= 5;
439 c -= 5;
440 test.expect (a.get() == c);
441
442 test.expect (++a == ++c);
443 ++a;
444 ++c;
445 test.expect (--a == --c);
446 test.expect (a.get() == c);
447 a.memoryBarrier();
448
449 testFloat (test);
450 }
451
452
453
454 static void testFloat (UnitTest& test)
455 {
456 Atomic<Type> a, b;
457 a = (Type) 101;
458 a.memoryBarrier();
459
460 /* These are some simple test cases to check the atomics - let me know
461 if any of these assertions fail on your system!
462 */
463 test.expect (exactlyEqual (a.get(), (Type) 101));
464 test.expect (! a.compareAndSetBool ((Type) 300, (Type) 200));
465 test.expect (exactlyEqual (a.get(), (Type) 101));
466 test.expect (a.compareAndSetBool ((Type) 200, a.get()));
467 test.expect (exactlyEqual (a.get(), (Type) 200));
468
469 test.expect (exactlyEqual (a.exchange ((Type) 300), (Type) 200));
470 test.expect (exactlyEqual (a.get(), (Type) 300));
471
472 b = a;
473 test.expect (exactlyEqual (b.get(), a.get()));
474 }
475 };
476};
477
479
480//==============================================================================
481class ThreadLocalValueUnitTest final : public UnitTest,
482 private Thread
483{
484public:
486 : UnitTest ("ThreadLocalValue", UnitTestCategories::threads),
487 Thread ("ThreadLocalValue Thread")
488 {}
489
490 void runTest() override
491 {
492 beginTest ("values are thread local");
493
494 {
496
498
499 sharedThreadLocal.get()->get() = 1;
500
501 startThread();
502 signalThreadShouldExit();
503 waitForThreadToExit (-1);
504
505 mainThreadResult = sharedThreadLocal.get()->get();
506
507 expectEquals (mainThreadResult.get(), 1);
508 expectEquals (auxThreadResult.get(), 2);
509 }
510
511 beginTest ("values are per-instance");
512
513 {
515
516 a.get() = 1;
517 b.get() = 2;
518
519 expectEquals (a.get(), 1);
520 expectEquals (b.get(), 2);
521 }
522 }
523
524private:
527
528 void run() override
529 {
530 sharedThreadLocal.get()->get() = 2;
531 auxThreadResult = sharedThreadLocal.get()->get();
532 }
533};
534
536
537#endif
538
539} // namespace juce
static constexpr uint16 swap(uint16 value) noexcept
Swaps the upper and lower bytes of a 16-bit integer.
Automatically locks and unlocks a mutex object.
static void JUCE_CALLTYPE writeToLog(const String &message)
Writes a string to the current logger.
static bool JUCE_CALLTYPE isRunningUnderDebugger() noexcept
Returns true if this process is being hosted by a debugger.
A smart-pointer class which points to a reference-counted object.
ReferencedType * get() const noexcept
Returns the object that this pointer references.
A base class which provides methods for reference-counting.
A simple spin-lock class that can be used as a simple, low-overhead mutex for uncontended situations.
void enter() const noexcept
Acquires the lock.
bool tryEnter() const noexcept
Attempts to acquire the lock, returning true if this was successful.
GenericScopedLock< SpinLock > ScopedLockType
Provides the type of scoped lock to use for locking a SpinLock.
The JUCE String class!
Definition juce_String.h:53
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
Provides cross-platform support for thread-local objects.
Used to receive callbacks for thread exit calls.
Encapsulates a thread.
Definition juce_Thread.h:43
static void JUCE_CALLTYPE setCurrentThreadAffinityMask(uint32 affinityMask)
Changes the affinity mask for the caller thread.
void setAffinityMask(uint32 affinityMask)
Sets the affinity mask for the thread.
void * ThreadID
A value type used for thread IDs.
bool isRealtime() const
Returns true if this Thread represents a realtime thread.
virtual ~Thread()
Destructor.
static Thread *JUCE_CALLTYPE getCurrentThread()
Finds the thread object that is currently running.
bool wait(double timeOutMilliseconds) const
Suspends the execution of this thread until either the specified timeout period has elapsed,...
static bool launch(std::function< void()> functionToRun)
Invokes a lambda or function on its own thread with the default priority.
ThreadID getThreadId() const noexcept
Returns the ID of this thread.
bool waitForThreadToExit(int timeOutMilliseconds) const
Waits for the thread to stop.
static void JUCE_CALLTYPE yield()
Yields the current thread's CPU time-slot and allows a new thread to run.
Thread(const String &threadName, size_t threadStackSize=osDefaultStackSize)
Creates a thread.
virtual void run()=0
Must be implemented to perform the thread's actual code.
bool startThread()
Attempts to start a new thread with default ('Priority::normal') priority.
static bool currentThreadShouldExit()
Checks whether the current thread has been told to stop running.
bool threadShouldExit() const
Checks whether the thread has been told to stop running.
bool startRealtimeThread(const RealtimeOptions &options)
Starts the thread with realtime performance characteristics on platforms that support it.
static ThreadID JUCE_CALLTYPE getCurrentThreadId()
Returns an id that identifies the caller thread.
bool stopThread(int timeOutMilliseconds)
Attempts to stop the thread running.
void notify() const
Wakes up the thread.
static void JUCE_CALLTYPE setCurrentThreadName(const String &newThreadName)
Changes the name of the caller thread.
void addListener(Listener *)
Add a listener to this thread which will receive a callback when signalThreadShouldExit was called on...
void signalThreadShouldExit()
Sets a flag to tell the thread it should stop.
Priority
The different runtime priorities of non-realtime threads.
Definition juce_Thread.h:54
@ normal
The OS default.
bool isThreadRunning() const
Returns true if the thread is currently active.
void removeListener(Listener *)
Removes a listener added with addListener.
static void JUCE_CALLTYPE sleep(int milliseconds)
Suspends the execution of the current thread until the specified timeout period has elapsed (note tha...
static uint32 getMillisecondCounter() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
This is a base class for classes that perform a unit test.
void signal() const
Wakes up any threads that are currently waiting on this object.
bool wait(double timeOutMilliseconds=-1.0) const
Suspends the calling thread until the event has been signalled.
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
#define jassertfalse
This will always cause an assertion failure.
#define JUCE_CALLTYPE
This macro defines the C calling convention used as the standard for JUCE calls.
T make_optional(T... args)
JUCE Namespace.
bool juce_isRunningUnderDebugger() noexcept
< This macro is added to all JUCE public class declarations.
unsigned short uint16
A platform-independent 16-bit unsigned integer type.
unsigned long long uint64
A platform-independent 64-bit unsigned integer type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
constexpr int numElementsInArray(Type(&)[N]) noexcept
Handy function for getting the number of elements in a simple const C array.
void run() override
Must be implemented to perform the thread's actual code.
A selection of options available when creating realtime threads.
Definition juce_Thread.h:77