changeset 508:64869af1f3ef

Add SpinLock
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Wed, 03 Jan 2018 17:34:24 +0900
parents a7127917c736
children 647716041772 62a77785cb2b
files src/parallel_execution/SpinLock.cbc
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/SpinLock.cbc	Wed Jan 03 17:34:24 2018 +0900
@@ -0,0 +1,32 @@
+#include "../context.h"
+#interface "Atomic.h"
+#interface "Lock.h"
+
+Lock* createSpinLock(struct Context* context) {
+    struct Lock* lock = new Lock();
+    struct SpinLock* spinLock = new SpinLock();
+    spinLock->lock = NULL;
+    spinLock->atomic = createAtomicReference(context);
+    lock->lock = (union Data*)spinLock;
+    lock->doLock = C_doLockSpinLock;
+    lock->doUnlock = C_doUnlockSpinLock;
+    return lock;
+}
+
+__code doLockSpinLock(struct SpinLock* lock, __code next(...)) {
+    struct Atomic* atomic = lock->atomic;
+    goto atomic->checkAndSet(&lock->lock, NULL, 1, doLockSpinLock1, doLockSpinLock);
+}
+
+__code doLockSpinLock1(struct SpinLock* lock, __code next(...)) {
+    lock->lockContext = context;
+    goto next(...);
+}
+
+__code doUnlockSpinLock(struct SpinLock* lock, __code next(...)) {
+    if (lock->lockContext == context) {
+        struct Atomic* atomic = lock->atomic;
+        goto atomic->checkAndSet(&lock->lock, 1, NULL, next(...), doUnlockSpinLock);
+    }
+    goto next(...);
+}