11#include <libco/libco.h>
19#define __JAFFAR_COMMON_DETHREADER_STATE \
20 namespace jaffarCommon \
22 namespace dethreader \
24 Runtime* __runtime = nullptr; \
31#define __JAFFAR_COMMON_DETHREADER_STACK_SIZE 4 * 1024 * 1024
44extern Runtime* __runtime;
100 void operator=(
const Thread&) =
delete;
113 _coroutine = co_create(stackSize, Thread::coroutineWrapper);
126 __JAFFAR_COMMON__INLINE__
void run()
const
131 auto timeDelta = timing::timeDeltaMicroseconds(timing::now(), _sleepStartTime);
134 if (timeDelta < _sleepDuration)
return;
142 co_switch(_coroutine);
148 __JAFFAR_COMMON__INLINE__
void yield()
166 __JAFFAR_COMMON__INLINE__
void sleep(
const size_t sleepDuration)
168 _sleepDuration = sleepDuration;
170 _sleepStartTime = timing::now();
179 __JAFFAR_COMMON__INLINE__
bool joinable() {
return true; }
211 __JAFFAR_COMMON__INLINE__
void setReturnReason(
const returnReason_t returnReason) { _returnReason = returnReason; }
216 __JAFFAR_COMMON__INLINE__
static void coroutineWrapper()
219 currentThread->_fc();
237 cothread_t _coroutine;
247 size_t _sleepDuration;
265 const auto threadId = _uniqueThreadIdCounter;
266 _threadQueue.push(std::make_unique<Thread>(fc, threadId));
267 _uniqueThreadIdCounter++;
292 __JAFFAR_COMMON__INLINE__
void run()
295 _coroutine = co_active();
298 while (_threadQueue.empty() ==
false)
301 auto thread = std::move(_threadQueue.front());
314 _threadQueue.push(std::move(thread));
316 _finishedThreads.insert(thread->getThreadId());
344 __JAFFAR_COMMON__INLINE__
const std::set<threadId_t>&
getFinishedThreads()
const {
return _finishedThreads; }
355 std::set<threadId_t> _finishedThreads;
360 Thread* _currentThread =
nullptr;
365 std::queue<std::unique_ptr<Thread>> _threadQueue;
370 cothread_t _coroutine;
395__JAFFAR_COMMON__INLINE__
void yield()
406__JAFFAR_COMMON__INLINE__
void sleep(
const size_t sleepDuration)
Definition dethreader.hpp:68
threadId_t _threadWaitedFor
Definition dethreader.hpp:203
Thread(const threadFc_t fc, const threadId_t id)
Definition dethreader.hpp:110
__JAFFAR_COMMON__INLINE__ void join(const threadId_t threadId)
Definition dethreader.hpp:186
returnReason_t
Definition dethreader.hpp:74
@ none
Definition dethreader.hpp:78
@ finished
Definition dethreader.hpp:83
@ waiting
Definition dethreader.hpp:93
@ sleeping
Definition dethreader.hpp:88
__JAFFAR_COMMON__INLINE__ returnReason_t getReturnReason() const
Definition dethreader.hpp:159
__JAFFAR_COMMON__INLINE__ bool joinable()
Definition dethreader.hpp:179
__JAFFAR_COMMON__INLINE__ void yield()
Definition dethreader.hpp:148
~Thread()
Definition dethreader.hpp:119
__JAFFAR_COMMON__INLINE__ threadId_t getThreadId() const
Definition dethreader.hpp:198
__JAFFAR_COMMON__INLINE__ void sleep(const size_t sleepDuration)
Definition dethreader.hpp:166
__JAFFAR_COMMON__INLINE__ void run() const
Definition dethreader.hpp:126
Definition dethreader.hpp:62
__JAFFAR_COMMON__INLINE__ void setCurrentThread(Thread *const thread)
Definition dethreader.hpp:325
__JAFFAR_COMMON__INLINE__ void run()
Definition dethreader.hpp:292
__JAFFAR_COMMON__INLINE__ void initialize()
Definition dethreader.hpp:274
__JAFFAR_COMMON__INLINE__ Thread * getCurrentThread() const
Definition dethreader.hpp:332
__JAFFAR_COMMON__INLINE__ const std::set< threadId_t > & getFinishedThreads() const
Definition dethreader.hpp:344
__JAFFAR_COMMON__INLINE__ threadId_t createThread(const threadFc_t fc)
Definition dethreader.hpp:263
__JAFFAR_COMMON__INLINE__ void yieldToRuntime()
Definition dethreader.hpp:337
__JAFFAR_COMMON__INLINE__ void finalize()
Definition dethreader.hpp:283
uint64_t threadId_t
Definition dethreader.hpp:54
__JAFFAR_COMMON__INLINE__ void sleep(const size_t sleepDuration)
Definition dethreader.hpp:406
#define __JAFFAR_COMMON_DETHREADER_STACK_SIZE
Definition dethreader.hpp:31
__JAFFAR_COMMON__INLINE__ threadId_t createThread(const threadFc_t fc)
Definition dethreader.hpp:386
__JAFFAR_COMMON__INLINE__ void join(const threadId_t threadId)
Definition dethreader.hpp:417
__JAFFAR_COMMON__INLINE__ Runtime::Thread * getCurrentThread()
Definition dethreader.hpp:378
__JAFFAR_COMMON__INLINE__ void yield()
Definition dethreader.hpp:395
std::function< void()> threadFc_t
Definition dethreader.hpp:49
Contains common functions for exception throwing.
#define JAFFAR_THROW_LOGIC(...)
Definition exceptions.hpp:27
Contains common functions related to time measurement.
std::chrono::high_resolution_clock::time_point timePoint
Definition timing.hpp:21