Add spin_pdr_trylock
authorBarret Rhoden <brho@cs.berkeley.edu>
Thu, 8 Sep 2016 17:52:44 +0000 (13:52 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Fri, 16 Sep 2016 18:35:09 +0000 (14:35 -0400)
Note I didn't use trylock for the spin_pdr_lock implementation.  If we know
we want to lock, no matter what, then we don't want to bother disabling and
enabling notifs repeatedly.

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

index 13688e6..18e21f9 100644 (file)
@@ -35,6 +35,7 @@ struct spin_pdr_lock {
 typedef struct spin_pdr_lock spinpdrlock_t;
 
 void spin_pdr_init(struct spin_pdr_lock *pdr_lock);
+bool spin_pdr_trylock(struct spin_pdr_lock *pdr_lock);
 void spin_pdr_lock(struct spin_pdr_lock *pdr_lock);
 void spin_pdr_unlock(struct spin_pdr_lock *pdr_lock);
 bool spin_pdr_locked(struct spin_pdr_lock *pdr_lock);
index a2ff45d..fbf6d25 100644 (file)
@@ -88,3 +88,21 @@ void spin_pdr_unlock(struct spin_pdr_lock *pdr_lock)
        /* Enable notifs, if we're an _M uthread */
        uth_enable_notifs();
 }
+
+bool spin_pdr_trylock(struct spin_pdr_lock *pdr_lock)
+{
+       uint32_t lock_val;
+
+       uth_disable_notifs();
+       lock_val = ACCESS_ONCE(pdr_lock->lock);
+       if (lock_val != SPINPDR_UNLOCKED) {
+               uth_enable_notifs();
+               return FALSE;
+       }
+       if (atomic_cas_u32(&pdr_lock->lock, lock_val, vcore_id())) {
+               return TRUE;
+       } else {
+               uth_enable_notifs();
+               return FALSE;
+       }
+}