|
@@ -0,0 +1,73 @@
|
|
|
+#include <stdio.h>
|
|
|
+#include <threads.h>
|
|
|
+
|
|
|
+#include "../Tests.h"
|
|
|
+#include "core/SpinLock.h"
|
|
|
+#include "core/Utility.h"
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ mtx_t m;
|
|
|
+ int counter;
|
|
|
+} MutexCounter;
|
|
|
+
|
|
|
+static int incrementMutexCounter(void* p) {
|
|
|
+ MutexCounter* mcp = (MutexCounter*)p;
|
|
|
+ for(int i = 0; i < 10000; i++) {
|
|
|
+ mtx_lock(&mcp->m);
|
|
|
+ mcp->counter++;
|
|
|
+ mtx_unlock(&mcp->m);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void testMutex() {
|
|
|
+ i64 n = -coreNanos();
|
|
|
+
|
|
|
+ MutexCounter mc = {0};
|
|
|
+ mtx_init(&mc.m, mtx_plain);
|
|
|
+ thrd_t t[2];
|
|
|
+ thrd_create(t + 0, incrementMutexCounter, &mc);
|
|
|
+ thrd_create(t + 1, incrementMutexCounter, &mc);
|
|
|
+ thrd_join(t[0], nullptr);
|
|
|
+ thrd_join(t[1], nullptr);
|
|
|
+ CORE_TEST_INT(20000, mc.counter);
|
|
|
+
|
|
|
+ n += coreNanos();
|
|
|
+ printf("%ldns Mutex\n", n);
|
|
|
+}
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ CoreSpinLock s;
|
|
|
+ int counter;
|
|
|
+} SpinLockCounter;
|
|
|
+
|
|
|
+static int incrementSpinLockCounter(void* p) {
|
|
|
+ SpinLockCounter* mcp = (SpinLockCounter*)p;
|
|
|
+ for(int i = 0; i < 10000; i++) {
|
|
|
+ coreLockSpinLock(&mcp->s);
|
|
|
+ mcp->counter++;
|
|
|
+ coreUnlockSpinLock(&mcp->s);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void testSpinLock() {
|
|
|
+ i64 n = -coreNanos();
|
|
|
+
|
|
|
+ SpinLockCounter sc = {0};
|
|
|
+ coreInitSpinLock(&sc.s);
|
|
|
+ thrd_t t[2];
|
|
|
+ thrd_create(t + 0, incrementSpinLockCounter, &sc);
|
|
|
+ thrd_create(t + 1, incrementSpinLockCounter, &sc);
|
|
|
+ thrd_join(t[0], nullptr);
|
|
|
+ thrd_join(t[1], nullptr);
|
|
|
+ CORE_TEST_INT(20000, sc.counter);
|
|
|
+
|
|
|
+ n += coreNanos();
|
|
|
+ printf("%ldns SpinLock\n", n);
|
|
|
+}
|
|
|
+
|
|
|
+void coreTestSpinLock() {
|
|
|
+ testMutex();
|
|
|
+ testSpinLock();
|
|
|
+}
|