|
@@ -2,7 +2,9 @@
|
|
|
|
|
|
#include "../Tests.hpp"
|
|
#include "../Tests.hpp"
|
|
#include "core/thread/Mutex.hpp"
|
|
#include "core/thread/Mutex.hpp"
|
|
|
|
+#include "core/thread/SpinLock.hpp"
|
|
#include "core/thread/Thread.hpp"
|
|
#include "core/thread/Thread.hpp"
|
|
|
|
+#include "core/utils/Clock.hpp"
|
|
|
|
|
|
static int runDone = 0;
|
|
static int runDone = 0;
|
|
|
|
|
|
@@ -94,15 +96,55 @@ static int incrementMutexCounter(void* p) {
|
|
}
|
|
}
|
|
|
|
|
|
static void testMutex() {
|
|
static void testMutex() {
|
|
|
|
+ Core::Clock::Nanos n;
|
|
|
|
+ CORE_TEST_ERROR(Core::Clock::getNanos(n));
|
|
|
|
+
|
|
MutexCounter mc;
|
|
MutexCounter mc;
|
|
CORE_TEST_ERROR(mc.m.init());
|
|
CORE_TEST_ERROR(mc.m.init());
|
|
- CORE_TEST_EQUAL(Core::ErrorCode::INVALID_STATE, mc.m.init());
|
|
|
|
Core::Thread t[2];
|
|
Core::Thread t[2];
|
|
CORE_TEST_ERROR(t[0].start(incrementMutexCounter, &mc));
|
|
CORE_TEST_ERROR(t[0].start(incrementMutexCounter, &mc));
|
|
CORE_TEST_ERROR(t[1].start(incrementMutexCounter, &mc));
|
|
CORE_TEST_ERROR(t[1].start(incrementMutexCounter, &mc));
|
|
CORE_TEST_ERROR(t[0].join(nullptr));
|
|
CORE_TEST_ERROR(t[0].join(nullptr));
|
|
CORE_TEST_ERROR(t[1].join(nullptr));
|
|
CORE_TEST_ERROR(t[1].join(nullptr));
|
|
CORE_TEST_EQUAL(20000, mc.counter);
|
|
CORE_TEST_EQUAL(20000, mc.counter);
|
|
|
|
+
|
|
|
|
+ Core::Clock::Nanos n2;
|
|
|
|
+ CORE_TEST_ERROR(Core::Clock::getNanos(n2));
|
|
|
|
+ Core::ArrayString<64> s;
|
|
|
|
+ s.append(n2 - n).append("ns Mutex").printLine();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+struct SpinLockCounter {
|
|
|
|
+ Core::SpinLock s{};
|
|
|
|
+ int counter = 0;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static int incrementSpinLockCounter(void* p) {
|
|
|
|
+ SpinLockCounter* mcp = static_cast<SpinLockCounter*>(p);
|
|
|
|
+ for(int i = 0; i < 10000; i++) {
|
|
|
|
+ mcp->s.lock();
|
|
|
|
+ mcp->counter++;
|
|
|
|
+ mcp->s.unlock();
|
|
|
|
+ }
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void testSpinLock() {
|
|
|
|
+ Core::Clock::Nanos n;
|
|
|
|
+ CORE_TEST_ERROR(Core::Clock::getNanos(n));
|
|
|
|
+
|
|
|
|
+ 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_EQUAL(20000, sc.counter);
|
|
|
|
+
|
|
|
|
+ Core::Clock::Nanos n2;
|
|
|
|
+ CORE_TEST_ERROR(Core::Clock::getNanos(n2));
|
|
|
|
+ Core::ArrayString<64> s;
|
|
|
|
+ s.append(n2 - n).append("ns SpinLock").printLine();
|
|
}
|
|
}
|
|
|
|
|
|
void Core::testThread() {
|
|
void Core::testThread() {
|
|
@@ -115,4 +157,5 @@ void Core::testThread() {
|
|
testMoveIntoActive();
|
|
testMoveIntoActive();
|
|
testDoubleJoin();
|
|
testDoubleJoin();
|
|
testMutex();
|
|
testMutex();
|
|
|
|
+ testSpinLock();
|
|
}
|
|
}
|