|
@@ -2,6 +2,42 @@
|
|
|
|
|
|
#include <threads.h>
|
|
#include <threads.h>
|
|
|
|
|
|
|
|
+#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<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) {
|
|
check_return Core::Error Core::Thread::start(Function f, void* p) {
|
|
CORE_ASSERT_ALIGNED_DATA(thread, thrd_t);
|
|
CORE_ASSERT_ALIGNED_DATA(thread, thrd_t);
|
|
return thrd_create(thread.as<thrd_t>(), f, p) != thrd_success
|
|
return thrd_create(thread.as<thrd_t>(), f, p) != thrd_success
|
|
@@ -10,7 +46,14 @@ check_return Core::Error Core::Thread::start(Function f, void* p) {
|
|
}
|
|
}
|
|
|
|
|
|
check_return Core::Error Core::Thread::join(int* returnValue) {
|
|
check_return Core::Error Core::Thread::join(int* returnValue) {
|
|
- return thrd_join(*thread.as<thrd_t>(), returnValue) != thrd_success
|
|
+ int e = thrd_join(*thread.as<thrd_t>(), returnValue);
|
|
- ? Error::THREAD_ERROR
|
|
+ reset(thread.as<thrd_t>());
|
|
- : Error::NONE;
|
|
+ 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));
|
|
}
|
|
}
|