# HG changeset patch # User Tatsuki IHA # Date 1514968464 -32400 # Node ID 64869af1f3efb4065f0dbdbafa775f7798777fd6 # Parent a7127917c73684b5453196168f0525680d52f189 Add SpinLock diff -r a7127917c736 -r 64869af1f3ef src/parallel_execution/SpinLock.cbc --- /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(...); +}