fix dependency check logic cause config always disabled
[lunaix-os.git] / lunaix-os / includes / lunaix / ds / mutex.h
1 #ifndef __LUNAIX_MUTEX_H
2 #define __LUNAIX_MUTEX_H
3
4 #include <lunaix/types.h>
5 #include <stdatomic.h>
6
7 typedef struct mutex_s
8 {
9     atomic_uint lk;
10     pid_t owner;
11 } mutex_t;
12
13 static inline void
14 mutex_init(mutex_t* mutex)
15 {
16     mutex->lk = ATOMIC_VAR_INIT(0);
17 }
18
19 static inline int
20 mutex_on_hold(mutex_t* mutex)
21 {
22     return atomic_load(&mutex->lk);
23 }
24
25 void
26 mutex_lock(mutex_t* mutex);
27
28 void
29 mutex_unlock(mutex_t* mutex);
30
31 void
32 mutex_lock_nested(mutex_t* mutex);
33
34 void
35 mutex_unlock_nested(mutex_t* mutex);
36
37 void
38 mutex_unlock_for(mutex_t* mutex, pid_t pid);
39
40 bool
41 mutex_trylock(mutex_t* mutex);
42
43 #endif /* __LUNAIX_MUTEX_H */