Add memory clobber to RISC-V set_stack_pointer
[akaros.git] / user / parlib / spinlock.c
index 59ea0dc..9f9a455 100644 (file)
 void spinlock_init(spinlock_t *lock)
 {
   assert(lock);
-  *lock = SPINLOCK_UNLOCKED;
+  lock->lock = 0;
 }
 
-
 int spinlock_trylock(spinlock_t *lock) 
 {
   assert(lock);
-  if (*lock == SPINLOCK_LOCKED)
-    return EBUSY;
-
-  return (int)atomic_cas(lock, (long)SPINLOCK_LOCKED, (long)SPINLOCK_UNLOCKED);
+  return __sync_lock_test_and_set(&lock->lock, EBUSY);
 }
 
-
 void spinlock_lock(spinlock_t *lock) 
 {
   assert(lock);
-  while (spinlock_trylock(lock) != (int)SPINLOCK_UNLOCKED)
+  while (spinlock_trylock(lock))
     cpu_relax();
 }
 
@@ -52,5 +47,5 @@ void spinlock_lock(spinlock_t *lock)
 void spinlock_unlock(spinlock_t *lock) 
 {
   assert(lock);
-  *lock = SPINLOCK_UNLOCKED;
+  __sync_lock_release(&lock->lock, 0);
 }