cons: Support epolling /dev/null
[akaros.git] / user / parlib / spinlock.c
index a2ff45d..a833ab8 100644 (file)
@@ -45,6 +45,8 @@ bool spinlock_locked(spinlock_t *lock)
  * lockholder's vcoreid in the lock, and all spinners ensure that vcore runs. */
 void spin_pdr_init(struct spin_pdr_lock *pdr_lock)
 {
+       /* See glibc-2.19-akaros/sysdeps/akaros/lowlevellock.h for details. */
+       parlib_static_assert(sizeof(struct spin_pdr_lock) == sizeof(int));
        pdr_lock->lock = SPINPDR_UNLOCKED;
 }
 
@@ -88,3 +90,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;
+       }
+}