Merge remote-tracking branch 'origin/master' into isa/arm64
[lunaix-os.git] / lunaix-os / includes / lunaix / ds / spinlock.h
1 #ifndef __LUNAIX_SPIN_H
2 #define __LUNAIX_SPIN_H
3
4 #include <lunaix/types.h>
5
6 struct spinlock
7 {
8     volatile bool flag;
9 };
10
11 typedef struct spinlock spinlock_t;
12
13 /*
14     TODO we might use our own construct for atomic ops
15          But we will do itlater, currently this whole 
16          kernel is on a single long thread of fate, 
17          there won't be any hardware concurrent access 
18          happened here.
19 */
20
21 static inline void
22 spinlock_init(spinlock_t* lock)
23 {
24     lock->flag = false;
25 }
26
27 static inline bool spinlock_try_acquire(spinlock_t* lock)
28 {
29     if (lock->flag){
30         return false;
31     }
32
33     return (lock->flag = true);
34 }
35
36 static inline void spinlock_acquire(spinlock_t* lock)
37 {
38     while (lock->flag);
39     lock->flag = true;
40 }
41
42 static inline void spinlock_release(spinlock_t* lock)
43 {
44     lock->flag = false;
45 }
46
47 #define DEFINE_SPINLOCK_OPS(type, lock_accessor)                            \
48     static inline void lock(type obj) { spinlock_acquire(&obj->lock_accessor); }    \
49     static inline void unlock(type obj) { spinlock_release(&obj->lock_accessor); }    
50
51 #endif /* __LUNAIX_SPIN_H */