]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/two_state_shared_lock.h
New upstream release
[bcachefs-tools-debian] / libbcachefs / two_state_shared_lock.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_TWO_STATE_LOCK_H
3 #define _BCACHEFS_TWO_STATE_LOCK_H
4
5 #include <linux/atomic.h>
6 #include <linux/sched.h>
7 #include <linux/wait.h>
8
9 /*
10  * Two-state lock - can be taken for add or block - both states are shared,
11  * like read side of rwsem, but conflict with other state:
12  */
13 typedef struct {
14         atomic_long_t           v;
15         wait_queue_head_t       wait;
16 } two_state_lock_t;
17
18 static inline void two_state_lock_init(two_state_lock_t *lock)
19 {
20         atomic_long_set(&lock->v, 0);
21         init_waitqueue_head(&lock->wait);
22 }
23
24 void bch2_two_state_unlock(two_state_lock_t *, int);
25 bool bch2_two_state_trylock(two_state_lock_t *, int);
26 void bch2_two_state_lock(two_state_lock_t *, int);
27
28 #endif /* _BCACHEFS_TWO_STATE_LOCK_H */