Serialize multiline printks
[akaros.git] / kern / include / atomic.h
index dc1612d..d7682c7 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <ros/common.h>
 #include <ros/atomic.h>
+#include <arch/membar.h>
 #include <arch/mmu.h>
 #include <arch/arch.h>
 #include <assert.h>
@@ -100,6 +101,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 +276,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)