A platform thread. More...
#include <Thread.h>
Public Member Functions | |
| ~Thread () | |
| Joins the thread if it's still running, same as calling join(). | |
| Thread (const Thread &)=delete | |
| Thread & | operator= (const Thread &)=delete |
| bool | start (ThreadEntry entry, void *arg=nullptr, uint32_t stackSize=8192, ThreadPriority priority=ThreadPriority::Normal, void(*wake)(void)=nullptr) |
| Starts entry(arg) running on a new thread. | |
| void | join () |
| Blocks until the thread exits. | |
| void | cancel () |
| Requests the thread stop at its next safe point without blocking. | |
| void | suspend () |
| Suspends thread execution. | |
| void | resume () |
| Resumes a suspended thread. | |
| bool | isSuspended () const |
| bool | isRunning () const |
| void | requestStop () |
| Sets the flag entry() should check (via stopRequested()) to know it should return, and invokes the wake callback passed to start(), if any. | |
| bool | stopRequested () const |
Static Public Member Functions | |
| static void | JoinAll () |
| Requests every currently-registered, still-running Thread stop (per its own wake callback, if any) and joins each in turn. | |
Protected Attributes | |
| void * | handle = nullptr |
| Backend-assigned thread handle. | |
| volatile bool | stopFlag = false |
| Set by requestStop(); polled by entry() via stopRequested() | |
| void(* | wakeFn )(void) = nullptr |
| Optional callback to break entry() out of a wait; set by start() | |
| Thread * | registryNext = nullptr |
| Intrusive next-pointer for the JoinAll() registry. | |
A platform thread.
Owns at most one running/joinable backend thread at a time - start() on an already-running Thread fails rather than leaking the previous one.
Every Thread that successfully start()s registers itself in a process- wide list (removed again on join()) so that JoinAll() can be used as a single, generic app-exit safety net: it stops and joins every thread still outstanding, regardless of which subsystem started it or whether that subsystem remembered to tear it down itself. This does not replace a subsystem's own halt/resume pausing during normal operation - it's the final guarantee that nothing is left running (and touching platform/ driver state) once shutdown begins in earnest.
| Thread::~Thread | ( | ) |
Joins the thread if it's still running, same as calling join().
Definition at line 14 of file Thread.cpp.
| void Thread::cancel | ( | ) |
Requests the thread stop at its next safe point without blocking.
This is a best-effort operation and may leak backend resources (see ThreadDriver::cancelThread) - prefer signalling the thread to exit and calling join() where the entry function can cooperate.
Definition at line 99 of file Thread.cpp.
|
inline |
| bool Thread::isSuspended | ( | ) | const |
Definition at line 121 of file Thread.cpp.
| void Thread::join | ( | ) |
Blocks until the thread exits.
Definition at line 46 of file Thread.cpp.
|
static |
Requests every currently-registered, still-running Thread stop (per its own wake callback, if any) and joins each in turn.
Intended to be called once, late in app shutdown - after this returns, no background thread started via Thread::start() can still be touching platform/driver state.
Definition at line 78 of file Thread.cpp.
| void Thread::requestStop | ( | ) |
Sets the flag entry() should check (via stopRequested()) to know it should return, and invokes the wake callback passed to start(), if any.
Does not block - call join() afterward to wait for entry() to actually return. Safe to call from any thread.
Definition at line 71 of file Thread.cpp.
| void Thread::resume | ( | ) |
Resumes a suspended thread.
Definition at line 115 of file Thread.cpp.
| bool Thread::start | ( | ThreadEntry | entry, |
| void * | arg = nullptr, | ||
| uint32_t | stackSize = 8192, | ||
| ThreadPriority | priority = ThreadPriority::Normal, | ||
| void(* | wake )(void) = nullptr ) |
Starts entry(arg) running on a new thread.
| entry | Thread entry point |
| arg | Argument passed to entry |
| stackSize | Stack size in bytes for the new thread |
| priority | Backend-defined thread priority |
| wake | Optional callback JoinAll()/requestStop() invokes to break entry() out of whatever wait it may be parked in once a stop has been requested - eg. signalling the cond it sleeps on. Not needed if entry() only ever polls stopRequested() between short, bounded waits. |
Definition at line 19 of file Thread.cpp.
|
inline |
| void Thread::suspend | ( | ) |
Suspends thread execution.
The thread itself keeps running once resumed - suspend is not a substitute for stopping it.
Definition at line 109 of file Thread.cpp.
|
protected |
|
protected |
|
protected |
Set by requestStop(); polled by entry() via stopRequested()
|
protected |