ソースを参照

Imports for SpinLock

Kajetan Johannes Hammerle 11 ヶ月 前
コミット
ae88c7c8be
3 ファイル変更21 行追加14 行削除
  1. 7 0
      include/core/SpinLock.h
  2. 3 3
      src/SpinLock.c
  3. 11 11
      test/modules/SpinLockTests.c

+ 7 - 0
include/core/SpinLock.h

@@ -11,4 +11,11 @@ void coreInitSpinLock(CoreSpinLock* l);
 void coreLockSpinLock(CoreSpinLock* l);
 void coreUnlockSpinLock(CoreSpinLock* l);
 
+#ifdef IMPORT_CORE
+#define SpinLock CoreSpinLock
+#define initSpinLock coreInitSpinLock
+#define lockSpinLock coreLockSpinLock
+#define unlockSpinLock coreUnlockSpinLock
+#endif
+
 #endif

+ 3 - 3
src/SpinLock.c

@@ -2,11 +2,11 @@
 
 #include <threads.h>
 
-void coreInitSpinLock(CoreSpinLock* l) {
+void initSpinLock(SpinLock* l) {
     atomic_init(&l->lock, false);
 }
 
-void coreLockSpinLock(CoreSpinLock* l) {
+void lockSpinLock(SpinLock* l) {
     while(true) {
         bool expected = false;
         if(atomic_compare_exchange_weak(&l->lock, &expected, true)) {
@@ -17,6 +17,6 @@ void coreLockSpinLock(CoreSpinLock* l) {
     }
 }
 
-void coreUnlockSpinLock(CoreSpinLock* l) {
+void unlockSpinLock(SpinLock* l) {
     atomic_store(&l->lock, false);
 }

+ 11 - 11
test/modules/SpinLockTests.c

@@ -12,7 +12,7 @@ typedef struct {
 
 static int incrementMutexCounter(void* p) {
     MutexCounter* mcp = (MutexCounter*)p;
-    for(int i = 0; i < 10000; i++) {
+    for(int i = 0; i < 20000; i++) {
         mtx_lock(&mcp->m);
         mcp->counter++;
         mtx_unlock(&mcp->m);
@@ -21,7 +21,7 @@ static int incrementMutexCounter(void* p) {
 }
 
 static void testMutex() {
-    i64 n = -coreGetNanos();
+    i64 n = -getNanos();
 
     MutexCounter mc = {.counter = 0};
     mtx_init(&mc.m, mtx_plain);
@@ -30,40 +30,40 @@ static void testMutex() {
     thrd_create(t + 1, incrementMutexCounter, &mc);
     thrd_join(t[0], nullptr);
     thrd_join(t[1], nullptr);
-    CORE_TEST_INT(20000, mc.counter);
+    TEST_INT(40000, mc.counter);
 
-    n += coreGetNanos();
+    n += getNanos();
     printf("%ldns Mutex\n", n);
 }
 
 typedef struct {
-    CoreSpinLock s;
+    SpinLock s;
     int counter;
 } SpinLockCounter;
 
 static int incrementSpinLockCounter(void* p) {
     SpinLockCounter* mcp = (SpinLockCounter*)p;
     for(int i = 0; i < 20000; i++) {
-        coreLockSpinLock(&mcp->s);
+        lockSpinLock(&mcp->s);
         mcp->counter++;
-        coreUnlockSpinLock(&mcp->s);
+        unlockSpinLock(&mcp->s);
     }
     return 0;
 }
 
 static void testSpinLockLoop() {
-    i64 n = -coreGetNanos();
+    i64 n = -getNanos();
 
     SpinLockCounter sc = {.counter = 0};
-    coreInitSpinLock(&sc.s);
+    initSpinLock(&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(40000, sc.counter);
+    TEST_INT(40000, sc.counter);
 
-    n += coreGetNanos();
+    n += getNanos();
     printf("%ldns SpinLock\n", n);
 }