Second Extended Filesystem (ext2) and other improvements (#33)
[lunaix-os.git] / lunaix-os / includes / lunaix / ds / spinlock.h
diff --git a/lunaix-os/includes/lunaix/ds/spinlock.h b/lunaix-os/includes/lunaix/ds/spinlock.h
new file mode 100644 (file)
index 0000000..557f310
--- /dev/null
@@ -0,0 +1,51 @@
+#ifndef __LUNAIX_SPIN_H
+#define __LUNAIX_SPIN_H
+
+#include <lunaix/types.h>
+
+struct spinlock
+{
+    volatile bool flag;
+};
+
+typedef struct spinlock spinlock_t;
+
+/*
+    TODO we might use our own construct for atomic ops
+         But we will do itlater, currently this whole 
+         kernel is on a single long thread of fate, 
+         there won't be any hardware concurrent access 
+         happened here.
+*/
+
+static inline void
+spinlock_init(spinlock_t* lock)
+{
+    lock->flag = false;
+}
+
+static inline bool spinlock_try_acquire(spinlock_t* lock)
+{
+    if (lock->flag){
+        return false;
+    }
+
+    return (lock->flag = true);
+}
+
+static inline void spinlock_acquire(spinlock_t* lock)
+{
+    while (lock->flag);
+    lock->flag = true;
+}
+
+static inline void spinlock_release(spinlock_t* lock)
+{
+    lock->flag = false;
+}
+
+#define DEFINE_SPINLOCK_OPS(type, lock_accessor)                            \
+    static inline void lock(type obj) { spinlock_acquire(&obj->lock_accessor); }    \
+    static inline void unlock(type obj) { spinlock_release(&obj->lock_accessor); }    
+
+#endif /* __LUNAIX_SPIN_H */