X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/baca54322c66983205edecd2ebb00d997878be50..270869139db617e29a35bb9ded41087bb702f9ac:/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 index 0000000..557f310 --- /dev/null +++ b/lunaix-os/includes/lunaix/ds/spinlock.h @@ -0,0 +1,51 @@ +#ifndef __LUNAIX_SPIN_H +#define __LUNAIX_SPIN_H + +#include + +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 */