#include "core/thread/Thread.hpp" #include #include #include "core/utils/Meta.hpp" #include "core/utils/Utility.hpp" static void reset(thrd_t* t) { memset(t, 0, sizeof(thrd_t)); } Core::Thread::Thread() : thread() { CORE_ASSERT_ALIGNED_DATA(thread, thrd_t); reset(thread.as()); } Core::Thread::Thread(Thread&& other) : thread() { swap(other); } static bool doesExist(thrd_t* t) { thrd_t zero{}; return memcmp(&zero, t, sizeof(thrd_t)) != 0; } 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) { return thrd_create(thread.as(), f, p) != thrd_success ? ErrorCode::THREAD_ERROR : ErrorCode::NONE; } check_return Core::Error Core::Thread::join(int* returnValue) { int e = thrd_join(*thread.as(), returnValue); reset(thread.as()); return e != thrd_success ? ErrorCode::THREAD_ERROR : ErrorCode::NONE; } void Core::Thread::swap(Thread& other) { thrd_t tmp; memcpy(&tmp, thread.as(), sizeof(thrd_t)); memcpy(thread.as(), other.thread.as(), sizeof(thrd_t)); memcpy(other.thread.as(), &tmp, sizeof(thrd_t)); }