perf: Fix buggy bitops
[akaros.git] / kern / include / atomic.h
index dc1612d..e5d1d2d 100644 (file)
@@ -100,6 +100,7 @@ static inline void spinlock_debug(spinlock_t *lock)
 static inline void spinlock_init(spinlock_t *lock);
 static inline void spinlock_init_irqsave(spinlock_t *lock);
 static inline void spin_lock_irqsave(spinlock_t *lock);
+static inline bool spin_trylock_irqsave(spinlock_t *lock);
 static inline void spin_unlock_irqsave(spinlock_t *lock);
 static inline bool spin_lock_irq_enabled(spinlock_t *lock);
 
@@ -274,6 +275,21 @@ static inline void spin_lock_irqsave(spinlock_t *lock)
                lock->rlock |= SPINLOCK_IRQ_EN;
 }
 
+static inline bool spin_trylock_irqsave(spinlock_t *lock)
+{
+       uint32_t irq_en = irq_is_enabled();
+
+       disable_irq();
+       if (!spin_trylock(lock)) {
+               if (irq_en)
+                       enable_irq();
+               return FALSE;
+       }
+       if (irq_en)
+               lock->rlock |= SPINLOCK_IRQ_EN;
+       return TRUE;
+}
+
 // if the high bit of the lock is set, then re-enable interrupts
 // (note from asw: you're lucky this works, you little-endian jerks)
 static inline void spin_unlock_irqsave(spinlock_t *lock)