311 static thread_local Thread *_currentThread;
319 mutable Mutex _mutex;
322 bool _started =
false;
323 bool _finished =
false;
324 bool _adopted =
false;
325 bool _usesPthread =
false;
338 bool isJoinable()
const;
T value() const
Loads the current value with acquire semantics.
Definition atomic.h:46
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Per-thread event loop providing event dispatch, timers, and posted callables.
Definition eventloop.h:58
Dynamic array container wrapping std::vector.
Definition list.h:40
Mutual exclusion lock wrapping std::mutex.
Definition mutex.h:33
Base object for promeki.
Definition objectbase.h:129
ObjectBase * parent() const
Returns the parent object, if one. nullptr if none.
Definition objectbase.h:258
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Wrapper around std::thread with a built-in EventLoop.
Definition thread.h:48
virtual ~Thread()
Destructor. Waits for the thread if still running.
uint64_t nativeId() const
Returns the OS-native thread ID.
Definition thread.h:196
Error setAffinity(const Set< int > &cpus)
Restricts this thread to run on the specified CPU cores.
void start(size_t stackSize=0)
Starts the thread.
PROMEKI_SIGNAL(started)
Emitted when the thread starts running.
static Thread * adoptCurrentThread()
Adopts the calling thread as a Thread object.
std::thread::native_handle_type NativeHandle
Platform-specific native thread handle type.
Definition thread.h:52
bool isAdopted() const
Returns whether this is an adopted thread.
Definition thread.h:179
virtual void run()
Override for custom thread behavior.
EventLoop * threadEventLoop() const
Returns the EventLoop associated with this thread.
bool isCurrentThread() const
Returns whether the calling thread is this thread.
Definition thread.h:185
int priority() const
Returns the current priority of this thread.
String name() const
Returns the name of this thread.
void quit(int returnCode=0)
Requests the thread's event loop to quit.
Thread(ObjectBase *parent=nullptr)
Constructs a Thread (spawned mode).
static uint64_t currentNativeId()
Returns the OS-native thread ID of the calling thread.
static unsigned int idealThreadCount()
Returns the number of hardware threads available.
PROMEKI_SIGNAL(finished, int)
Emitted when the thread finishes (carries the event loop exit code).
SchedulePolicy schedulePolicy() const
Returns the current scheduling policy of this thread.
Error wait(unsigned int timeoutMs=0)
Waits for the thread to finish.
static int priorityMin(SchedulePolicy policy=SchedulePolicy::Default)
Returns the minimum priority for a scheduling policy.
Set< int > affinity() const
Returns the set of CPU cores this thread is allowed to run on.
static int priorityMax(SchedulePolicy policy=SchedulePolicy::Default)
Returns the maximum priority for a scheduling policy.
bool isRunning() const
Returns whether the thread is currently running.
Definition thread.h:173
void setName(const String &name)
Sets the name of this thread.
static Thread * currentThread()
Returns the Thread object for the calling thread.
Error setPriority(int priority, SchedulePolicy policy=SchedulePolicy::Default)
Sets the scheduling policy and priority for this thread.
int exitCode() const
Returns the exit code from the thread's event loop.
Definition thread.h:167
Condition variable wrapping std::condition_variable.
Definition waitcondition.h:26
SchedulePolicy
Scheduling policy for thread priority control.
Definition thread.h:26
@ Batch
Batch scheduling, Linux only (SCHED_BATCH).
@ Default
Normal time-sharing (SCHED_OTHER on POSIX).
@ RoundRobin
Real-time round-robin (SCHED_RR).
@ Fifo
Real-time first-in-first-out (SCHED_FIFO).
@ Idle
Idle scheduling, Linux only (SCHED_IDLE).
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19