Fix spinlock_trylock's return value
authorBarret Rhoden <brho@cs.berkeley.edu>
Thu, 8 Sep 2016 17:49:27 +0000 (13:49 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Fri, 16 Sep 2016 18:35:09 +0000 (14:35 -0400)
We should be returning TRUE when we successfully lock the lock, not EBUSY.
This is saner, and more in line with other locking APIs.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
tests/linux-lock-hacks.h
user/parlib/include/parlib/spinlock.h
user/parlib/spinlock.c

index cad52a1..4ba7c72 100644 (file)
@@ -2,33 +2,34 @@
  * lock_test.  It's a .h so that make tests doesn't build it. */
 
 #define ARCH_CL_SIZE 64
-#define SPINLOCK_INITIALIZER {0}
+#define SPINLOCK_INITIALIZER {FALSE}
 
 typedef struct {
-       int lock;
+       bool locked;
 } spinlock_t;
 
 void __attribute__((noinline)) spinlock_init(spinlock_t *lock)
 {
-       lock->lock = 0;
+       lock->locked = FALSE;
 }
 
-int __attribute__((noinline)) spinlock_trylock(spinlock_t *lock) 
+/* Returns TRUE if we grabbed the lock */
+bool __attribute__((noinline)) spinlock_trylock(spinlock_t *lock)
 {
-       if (lock->lock)
-               return EBUSY;
-       return __sync_lock_test_and_set(&lock->lock, EBUSY);
+       if (lock->locked)
+               return FALSE;
+       return !__sync_lock_test_and_set(&lock->locked, TRUE);
 }
 
 void __attribute__((noinline)) spinlock_lock(spinlock_t *lock) 
 {
-       while (spinlock_trylock(lock))
+       while (!spinlock_trylock(lock))
                cpu_relax();
 }
 
 void __attribute__((noinline)) spinlock_unlock(spinlock_t *lock) 
 {
-       __sync_lock_release(&lock->lock, 0);
+       __sync_lock_release(&lock->locked, FALSE);
 }
 
 #define MCS_LOCK_INIT {0}
index b03e745..13688e6 100644 (file)
 
 __BEGIN_DECLS
 
-#define SPINLOCK_INITIALIZER {0}
+#define SPINLOCK_INITIALIZER {FALSE}
 
 typedef struct {
-  int lock;
+       bool locked;
 } spinlock_t;
 
 void spinlock_init(spinlock_t *lock);
-int spinlock_trylock(spinlock_t *lock);
+bool spinlock_trylock(spinlock_t *lock);
 void spinlock_lock(spinlock_t *lock);
 void spinlock_unlock(spinlock_t *lock);
 bool spinlock_locked(spinlock_t *lock);
index 9ca395d..a2ff45d 100644 (file)
 
 void spinlock_init(spinlock_t *lock)
 {
-       lock->lock = 0;
+       lock->locked = FALSE;
 }
 
-int spinlock_trylock(spinlock_t *lock) 
+/* Returns TRUE if we grabbed the lock */
+bool spinlock_trylock(spinlock_t *lock)
 {
-       if (lock->lock)
-               return EBUSY;
-       return __sync_lock_test_and_set(&lock->lock, EBUSY);
+       if (lock->locked)
+               return FALSE;
+       return !__sync_lock_test_and_set(&lock->locked, TRUE);
 }
 
 void spinlock_lock(spinlock_t *lock) 
 {
-       while (spinlock_trylock(lock))
+       while (!spinlock_trylock(lock))
                cpu_relax();
 }
 
 void spinlock_unlock(spinlock_t *lock) 
 {
-       __sync_lock_release(&lock->lock, 0);
+       __sync_lock_release(&lock->locked, FALSE);
 }
 
 bool spinlock_locked(spinlock_t *lock)
 {
-       return lock->lock != 0;
+       return lock->locked;
 }
 
 /* Spin-PRD locks (preemption detection/recovery).  Idea is to CAS and put the