libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <thread>
14#include <promeki/atomic.h>
15#include <promeki/basicthread.h>
16#include <promeki/mutex.h>
17#include <promeki/objectbase.h>
18#include <promeki/set.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
62class Thread : public ObjectBase {
63 PROMEKI_OBJECT(Thread, ObjectBase)
64 public:
66 using NativeHandle = BasicThread::NativeHandle;
76 static Thread *adoptCurrentThread();
77
82 static Thread *currentThread();
83
88 Thread(ObjectBase *parent = nullptr);
89
91 virtual ~Thread();
92
106 Error start(size_t stackSize = 0);
107
121 Error wait(unsigned int timeoutMs = 0);
122
134 EventLoop *threadEventLoop() const;
135
140 void quit(int returnCode = 0);
141
149 int exitCode() const { return _exitCode.value(); }
150
155 bool isRunning() const { return _running.value(); }
156
161 bool isAdopted() const { return _adopted; }
162
167 bool isCurrentThread() const { return _currentThread == this; }
168
187 const BasicThread &basicThread() const { return _basic; }
188
198 uint64_t nativeId() const { return _nativeId.value(); }
199
212 std::thread::id id() const { return _stdId.value(); }
213
222 SchedulePolicy schedulePolicy() const;
223
232 int priority() const;
233
248 Error setPriority(int priority, SchedulePolicy policy = SchedulePolicy::Default);
249
258 String name() const;
259
281 void setName(const String &name);
282
292 Set<int> affinity() const;
293
306 Error setAffinity(const Set<int> &cpus);
307
310 PROMEKI_SIGNAL(started);
311
314 PROMEKI_SIGNAL(finished, int);
315
316 protected:
323 virtual void run();
324
325 private:
326 static thread_local Thread *_currentThread;
327
328 BasicThread _basic;
329 Atomic<bool> _running;
330 Atomic<int> _exitCode;
331 Atomic<uint64_t> _nativeId;
332 Atomic<std::thread::id> _stdId;
333 String _name;
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;
341
351 NativeHandle nativeHandle() const;
352 bool isJoinable() const { return _basic.isJoinable(); }
353 void threadEntry();
354};
355
356PROMEKI_NAMESPACE_END
357
358#endif // PROMEKI_ENABLE_CORE