libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <thread>
12#include <promeki/core/set.h>
13#include <promeki/core/mutex.h>
15#include <promeki/core/atomic.h>
16
18
26enum class SchedulePolicy {
27 Default,
29 Fifo,
30 Batch,
31 Idle
32};
33
48class Thread : public ObjectBase {
49 PROMEKI_OBJECT(Thread, ObjectBase)
50 public:
52 using NativeHandle = std::thread::native_handle_type;
63
69
80
87
94
103 static unsigned int idealThreadCount();
104
110
112 virtual ~Thread();
113
124 void start(size_t stackSize = 0);
125
139 Error wait(unsigned int timeoutMs = 0);
140
153
158 void quit(int returnCode = 0);
159
167 int exitCode() const { return _exitCode.value(); }
168
173 bool isRunning() const { return _running.value(); }
174
179 bool isAdopted() const { return _adopted; }
180
185 bool isCurrentThread() const { return _currentThread == this; }
186
196 uint64_t nativeId() const { return _nativeId.value(); }
197
207
216 int priority() const;
217
234
243 String name() const;
244
266 void setName(const String &name);
267
278
292
296
300
301 protected:
308 virtual void run();
309
310 private:
311 static thread_local Thread *_currentThread;
312
313 std::thread _thread;
314 Atomic<bool> _running;
315 Atomic<int> _exitCode;
316 Atomic<uint64_t> _nativeId;
317 String _name;
318 EventLoop *_threadLoop = nullptr;
319 mutable Mutex _mutex;
320 WaitCondition _startedCv;
321 WaitCondition _finishedCv;
322 bool _started = false;
323 bool _finished = false;
324 bool _adopted = false;
325 bool _usesPthread = false;
326 NativeHandle _pthreadHandle{};
327
337 NativeHandle nativeHandle() const;
338 bool isJoinable() const;
339 void joinThread();
340 void applyOsName();
341 void threadEntry();
342};
343
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