浏览代码

Use bool for thread errors

Kajetan Johannes Hammerle 3 周之前
父节点
当前提交
7d9fa929d2

+ 3 - 3
include/core/thread/Mutex.hpp

@@ -17,9 +17,9 @@ namespace Core {
         ~Mutex();
         Mutex& operator=(const Mutex& other) = delete;
         Mutex& operator=(Mutex&& other) = delete;
-        CError init();
-        CError lock();
-        CError unlock();
+        cbool init();
+        cbool lock();
+        cbool unlock();
     };
 }
 

+ 7 - 9
include/core/thread/Thread.hpp

@@ -1,28 +1,26 @@
 #ifndef CORE_THREAD_HPP
 #define CORE_THREAD_HPP
 
-#include "core/utils/AlignedData.hpp"
+#include <threads.h>
+
 #include "core/utils/Check.hpp"
-#include "core/utils/Error.hpp"
 
 namespace Core {
     class Thread final {
-        AlignedData<8, 8> thread;
+        thrd_t thread;
 
     public:
+        using Function = int (*)(void*);
+
         Thread();
         Thread(const Thread& other) = delete;
         Thread(Thread&& other);
         ~Thread();
         Thread& operator=(const Thread& other) = delete;
         Thread& operator=(Thread&& other);
-
         void swap(Thread& other);
-
-        using Function = int (*)(void*);
-
-        check_return Error start(Function f, void* p);
-        check_return Error join(int* returnValue = nullptr);
+        cbool start(Function f, void* p);
+        cbool join(int* returnValue = nullptr);
     };
 }
 

+ 1 - 0
include/core/utils/Check.hpp

@@ -19,5 +19,6 @@
 #endif
 
 #define CError check_return Core::Error
+#define cbool check_return bool
 
 #endif

+ 1 - 1
src/BitArray.cpp

@@ -141,7 +141,7 @@ CError Core::BitArray::resize(size_t newLength, size_t newBits) {
     }
     size_t arrayLength = getArrayLength(newLength, newBits);
     u64* newData = new(noThrow) u64[arrayLength];
-    memset(newData, 0, arrayLength * sizeof(int));
+    memset(newData, 0, arrayLength * sizeof(u64));
 
     size_t end = Math::min(getLength(), newLength);
     for(size_t i = 0; i < end; i++) {

+ 6 - 9
src/Mutex.cpp

@@ -10,17 +10,14 @@ Core::Mutex::~Mutex() {
     mtx_destroy(&mutex);
 }
 
-CError Core::Mutex::init() {
-    return mtx_init(&mutex, mtx_plain) != thrd_success ? ErrorCode::MUTEX_ERROR
-                                                       : ErrorCode::NONE;
+cbool Core::Mutex::init() {
+    return mtx_init(&mutex, mtx_plain) != thrd_success;
 }
 
-CError Core::Mutex::lock() {
-    return mtx_lock(&mutex) != thrd_success ? ErrorCode::MUTEX_ERROR
-                                            : ErrorCode::NONE;
+cbool Core::Mutex::lock() {
+    return mtx_lock(&mutex) != thrd_success;
 }
 
-CError Core::Mutex::unlock() {
-    return mtx_unlock(&mutex) != thrd_success ? ErrorCode::MUTEX_ERROR
-                                              : ErrorCode::NONE;
+cbool Core::Mutex::unlock() {
+    return mtx_unlock(&mutex) != thrd_success;
 }

+ 3 - 6
src/New.cpp

@@ -20,17 +20,14 @@ void operator delete[](void* p) noexcept {
     free(p);
 }
 
-void operator delete(void* p, size_t bytes) noexcept {
-    (void)bytes;
+void operator delete(void* p, size_t) noexcept {
     operator delete(p);
 }
 
-void operator delete[](void* p, size_t bytes) noexcept {
-    (void)bytes;
+void operator delete[](void* p, size_t) noexcept {
     free(p);
 }
 
-void* operator new(size_t bytes, void* p) noexcept {
-    (void)bytes;
+void* operator new(size_t, void* p) noexcept {
     return p;
 }

+ 20 - 24
src/Thread.cpp

@@ -1,61 +1,57 @@
 #include "core/thread/Thread.hpp"
 
 #include <string.h>
-#include <threads.h>
 
-#include "core/utils/Meta.hpp"
-#include "core/utils/Utility.hpp"
-
-static void reset(thrd_t* t) {
-    memset(t, 0, sizeof(thrd_t));
+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<thrd_t>());
+    reset(thread);
 }
 
 Core::Thread::Thread(Thread&& other) : thread() {
     swap(other);
 }
 
-static bool doesExist(thrd_t* t) {
+static bool doesExist(thrd_t& t) {
     thrd_t zero{};
-    return memcmp(&zero, t, sizeof(thrd_t)) != 0;
+    return memcmp(&zero, &t, sizeof(thrd_t)) != 0;
 }
 
 Core::Thread::~Thread() {
-    if(doesExist(thread.as<thrd_t>())) {
+    if(doesExist(thread)) {
         (void)join(nullptr);
     }
 }
 
 Core::Thread& Core::Thread::operator=(Thread&& other) {
     if(this != &other) {
-        if(doesExist(thread.as<thrd_t>())) {
+        if(doesExist(thread)) {
             (void)join(nullptr);
         }
-        reset(thread.as<thrd_t>());
+        reset(thread);
         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
-               ? ErrorCode::THREAD_ERROR
-               : ErrorCode::NONE;
+cbool Core::Thread::start(Function f, void* p) {
+    if(doesExist(thread)) {
+        return true;
+    }
+    return thrd_create(&thread, f, p) != thrd_success;
 }
 
-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 ? ErrorCode::THREAD_ERROR : ErrorCode::NONE;
+cbool Core::Thread::join(int* returnValue) {
+    int e = thrd_join(thread, returnValue);
+    reset(thread);
+    return e != thrd_success;
 }
 
 void Core::Thread::swap(Thread& other) {
     thrd_t tmp;
-    memcpy(&tmp, thread.as<thrd_t>(), sizeof(thrd_t));
-    memcpy(thread.as<thrd_t>(), other.thread.as<thrd_t>(), sizeof(thrd_t));
-    memcpy(other.thread.as<thrd_t>(), &tmp, sizeof(thrd_t));
+    memcpy(&tmp, &thread, sizeof(thrd_t));
+    memcpy(&thread, &other.thread, sizeof(thrd_t));
+    memcpy(&other.thread, &tmp, sizeof(thrd_t));
 }

+ 2 - 3
test/modules/HashMapTests.cpp

@@ -142,8 +142,7 @@ struct ProbingTest final : public HashMapTest {
     }
 };
 
-template<typename T>
-static void testEmplace() {
+static void testEmplaceProbing() {
     {
         Core::ProbingHashMap<int, ProbingTest> map;
 
@@ -335,7 +334,6 @@ static void testMap(bool light) {
     testAddReplace<T>();
     testClear<T>();
     testOverflow<T>(light);
-    testEmplace<T>();
     testToString<T>();
     testCopy<T>();
     testMove<T>();
@@ -393,5 +391,6 @@ void Core::testHashMap(bool light) {
     testMap<ProbingIntMap>(light);
     testMap<IntMap>(light);
     testEmplace();
+    testEmplaceProbing();
     testRemove();
 }

+ 30 - 23
test/modules/ThreadTests.cpp

@@ -20,9 +20,9 @@ static int run(void*) {
 static void testStart() {
     runDone = 0;
     Core::Thread t;
-    CORE_TEST_ERROR(t.start(run, nullptr));
+    CORE_TEST_FALSE(t.start(run, nullptr));
     int returnValue = 0;
-    CORE_TEST_ERROR(t.join(&returnValue));
+    CORE_TEST_FALSE(t.join(&returnValue));
     CORE_TEST_EQUAL(1, runDone);
     CORE_TEST_EQUAL(7, returnValue);
 }
@@ -30,54 +30,54 @@ static void testStart() {
 static void testLambda() {
     IntHolder i(0);
     Core::Thread t;
-    CORE_TEST_ERROR(t.start(
+    CORE_TEST_FALSE(t.start(
         [](void* p) {
             IntHolder* ip = static_cast<IntHolder*>(p);
             ip->value = 2;
             return 0;
         },
         &i));
-    CORE_TEST_ERROR(t.join(nullptr));
+    CORE_TEST_FALSE(t.join(nullptr));
     CORE_TEST_EQUAL(2, i.value);
 }
 
 static void testJoinWithoutStart() {
     Core::Thread t;
-    CORE_TEST_EQUAL(Core::ErrorCode::THREAD_ERROR, t.join(nullptr));
+    CORE_TEST_TRUE(t.join(nullptr));
 }
 
 static void testAutoJoin() {
     Core::Thread t;
-    CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
+    CORE_TEST_FALSE(t.start([](void*) { return 0; }, nullptr));
 }
 
 static void testMove() {
     Core::Thread t;
-    CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
+    CORE_TEST_FALSE(t.start([](void*) { return 0; }, nullptr));
     Core::Thread m = Core::move(t);
-    CORE_TEST_ERROR(m.join());
+    CORE_TEST_FALSE(m.join());
 }
 
 static void testMoveAssignment() {
     Core::Thread t;
-    CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
+    CORE_TEST_FALSE(t.start([](void*) { return 0; }, nullptr));
     Core::Thread m;
     m = Core::move(t);
-    CORE_TEST_ERROR(m.join());
+    CORE_TEST_FALSE(m.join());
 }
 
 static void testMoveIntoActive() {
     Core::Thread t;
-    CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
+    CORE_TEST_FALSE(t.start([](void*) { return 0; }, nullptr));
     Core::Thread m;
     t = Core::move(m);
 }
 
 static void testDoubleJoin() {
     Core::Thread t;
-    CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
-    CORE_TEST_ERROR(t.join(nullptr));
-    CORE_TEST_EQUAL(Core::ErrorCode::THREAD_ERROR, t.join(nullptr));
+    CORE_TEST_FALSE(t.start([](void*) { return 0; }, nullptr));
+    CORE_TEST_FALSE(t.join(nullptr));
+    CORE_TEST_TRUE(t.join(nullptr));
 }
 
 struct MutexCounter {
@@ -100,12 +100,12 @@ static void testMutex() {
     CORE_TEST_ERROR(Core::Clock::getNanos(n));
 
     MutexCounter mc;
-    CORE_TEST_ERROR(mc.m.init());
+    CORE_TEST_FALSE(mc.m.init());
     Core::Thread t[2];
-    CORE_TEST_ERROR(t[0].start(incrementMutexCounter, &mc));
-    CORE_TEST_ERROR(t[1].start(incrementMutexCounter, &mc));
-    CORE_TEST_ERROR(t[0].join(nullptr));
-    CORE_TEST_ERROR(t[1].join(nullptr));
+    CORE_TEST_FALSE(t[0].start(incrementMutexCounter, &mc));
+    CORE_TEST_FALSE(t[1].start(incrementMutexCounter, &mc));
+    CORE_TEST_FALSE(t[0].join(nullptr));
+    CORE_TEST_FALSE(t[1].join(nullptr));
     CORE_TEST_EQUAL(20000, mc.counter);
 
     Core::Clock::Nanos n2;
@@ -135,10 +135,10 @@ static void testSpinLock() {
 
     SpinLockCounter sc;
     Core::Thread t[2];
-    CORE_TEST_ERROR(t[0].start(incrementSpinLockCounter, &sc));
-    CORE_TEST_ERROR(t[1].start(incrementSpinLockCounter, &sc));
-    CORE_TEST_ERROR(t[0].join(nullptr));
-    CORE_TEST_ERROR(t[1].join(nullptr));
+    CORE_TEST_FALSE(t[0].start(incrementSpinLockCounter, &sc));
+    CORE_TEST_FALSE(t[1].start(incrementSpinLockCounter, &sc));
+    CORE_TEST_FALSE(t[0].join(nullptr));
+    CORE_TEST_FALSE(t[1].join(nullptr));
     CORE_TEST_EQUAL(20000, sc.counter);
 
     Core::Clock::Nanos n2;
@@ -147,6 +147,12 @@ static void testSpinLock() {
     s.append(n2 - n).append("ns SpinLock").printLine();
 }
 
+static void testDoubleStart() {
+    Core::Thread t;
+    CORE_TEST_FALSE(t.start([](void*) { return 0; }, nullptr));
+    CORE_TEST_TRUE(t.start([](void*) { return 0; }, nullptr));
+}
+
 void Core::testThread() {
     testStart();
     testLambda();
@@ -158,4 +164,5 @@ void Core::testThread() {
     testDoubleJoin();
     testMutex();
     testSpinLock();
+    testDoubleStart();
 }