11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
21PROMEKI_NAMESPACE_BEGIN
62class Thread :
public ObjectBase {
63 PROMEKI_OBJECT(Thread, ObjectBase)
66 using NativeHandle = BasicThread::NativeHandle;
76 static Thread *adoptCurrentThread();
82 static Thread *currentThread();
88 Thread(ObjectBase *parent =
nullptr);
106 Error start(
size_t stackSize = 0);
121 Error wait(
unsigned int timeoutMs = 0);
134 EventLoop *threadEventLoop()
const;
140 void quit(
int returnCode = 0);
149 int exitCode()
const {
return _exitCode.value(); }
155 bool isRunning()
const {
return _running.value(); }
161 bool isAdopted()
const {
return _adopted; }
167 bool isCurrentThread()
const {
return _currentThread ==
this; }
187 const BasicThread &basicThread()
const {
return _basic; }
198 uint64_t nativeId()
const {
return _nativeId.value(); }
212 std::thread::id id()
const {
return _stdId.value(); }
222 SchedulePolicy schedulePolicy()
const;
232 int priority()
const;
248 Error setPriority(
int priority, SchedulePolicy policy = SchedulePolicy::Default);
281 void setName(
const String &name);
292 Set<int> affinity()
const;
306 Error setAffinity(
const Set<int> &cpus);
310 PROMEKI_SIGNAL(started);
314 PROMEKI_SIGNAL(finished,
int);
326 static thread_local Thread *_currentThread;
329 Atomic<bool> _running;
330 Atomic<int> _exitCode;
331 Atomic<uint64_t> _nativeId;
332 Atomic<std::thread::id> _stdId;
334 EventLoop *_threadLoop =
nullptr;
335 mutable Mutex _mutex;
336 WaitCondition _startedCv;
337 WaitCondition _finishedCv;
338 bool _started =
false;
339 bool _finished =
false;
340 bool _adopted =
false;
351 NativeHandle nativeHandle()
const;
352 bool isJoinable()
const {
return _basic.isJoinable(); }