27 threadStackSize (stackSize)
33 if (deleteOnThreadEnd)
61static char currentThreadHolderLock [
sizeof (
SpinLock)];
63static SpinLock* castToSpinLockWithoutAliasingWarning (
void* s)
68static CurrentThreadHolder::Ptr getCurrentThreadHolder()
79void Thread::threadEntryPoint()
89 if (startSuspensionEvent.
wait (10000))
93 if (affinityMask != 0)
118void JUCE_API juce_threadEntryPoint (
void* userData)
120 static_cast<Thread*
> (userData)->threadEntryPoint();
132 #if JUCE_ANDROID || JUCE_LINUX || JUCE_BSD
138 startSuspensionEvent.
signal();
154 if (threadHandle ==
nullptr)
156 realtimeOptions.reset();
167 if (threadHandle ==
nullptr)
174 realtimeOptions.reset();
182 return threadHandle !=
nullptr;
187 return getCurrentThreadHolder()->value.
get();
199 listeners.call ([] (
Listener&
l) {
l.exitSignalSent(); });
258 threadHandle =
nullptr;
269 listeners.add (listener);
274 listeners.remove (listener);
279 return realtimeOptions.has_value();
322 anon->deleteOnThreadEnd =
true;
324 if (
anon->startThread (priority))
338 for (
int i = 20; --i >= 0;)
364 void runTest()
override
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
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);
399 beginTest (
"Atomic pointer increment/decrement");
404 beginTest (
"Atomic void*");
408 atomic.set ((
void*) 10);
411 expect (atomic.value == c);
412 expect (atomic.get() == c);
416 template <
typename Type>
420 AtomicTester() =
default;
422 static void testInteger (UnitTest& test)
430 test.expect (a.value == c);
431 test.expect (a.get() == c);
435 test.expect (a.get() == c);
440 test.expect (a.get() == c);
442 test.expect (++a == ++c);
445 test.expect (--a == --c);
446 test.expect (a.get() == c);
454 static void testFloat (UnitTest& test)
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));
469 test.expect (exactlyEqual (a.exchange ((Type) 300), (Type) 200));
470 test.expect (exactlyEqual (a.get(), (Type) 300));
473 test.expect (exactlyEqual (b.get(), a.get()));
487 Thread (
"ThreadLocalValue Thread")
490 void runTest()
override
492 beginTest (
"values are thread local");
502 signalThreadShouldExit();
503 waitForThreadToExit (-1);
511 beginTest (
"values are per-instance");
519 expectEquals (a.get(), 1);
520 expectEquals (b.get(), 2);
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.
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.
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.
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.
T make_optional(T... args)
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...
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.