#include "core/thread/Thread.hpp" #include #include "core/utils/Meta.hpp" #include "core/utils/Utility.hpp" Core::Thread::Thread() : thread() { } Core::Thread::Thread(Thread&& other) : thread() { swap(other); } static bool doesExist(thrd_t* t) { thrd_t zero{}; return !Core::memoryCompare(&zero, t, sizeof(thrd_t)); } static void reset(thrd_t* t) { Core::memorySet(t, 0, sizeof(thrd_t)); } Core::Thread::~Thread() { if(doesExist(thread.as())) { (void)join(nullptr); } } Core::Thread& Core::Thread::operator=(Thread&& other) { if(this != &other) { if(doesExist(thread.as())) { (void)join(nullptr); } reset(thread.as()); swap(other); } return *this; } check_return Core::Error Core::Thread::start(Function f, void* p) { CORE_ASSERT_ALIGNED_DATA(thread, thrd_t); return thrd_create(thread.as(), f, p) != thrd_success ? Error::THREAD_ERROR : Error::NONE; } check_return Core::Error Core::Thread::join(int* returnValue) { int e = thrd_join(*thread.as(), returnValue); reset(thread.as()); return e != thrd_success ? Error::THREAD_ERROR : Error::NONE; } void Core::Thread::swap(Thread& other) { thrd_t tmp; memoryCopy(&tmp, thread.as(), sizeof(thrd_t)); memoryCopy(thread.as(), other.thread.as(), sizeof(thrd_t)); memoryCopy(other.thread.as(), &tmp, sizeof(thrd_t)); }