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