diff options
author | Kent Overstreet <kent.overstreet@linux.dev> | 2022-11-26 17:09:59 -0500 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2022-11-26 17:09:59 -0500 |
commit | 934a84dfaf719af82dadbbe0e2480baff03c905b (patch) | |
tree | 22deb77e752f90c842ac2f8dce91f5a602afd49d /libbcachefs/two_state_shared_lock.h | |
parent | 48eefee7495c6e145f3fcfe6ab83f9e8bc27a1ec (diff) |
Update bcachefs sources to 5963d1b1a4 bcacehfs: Fix bch2_get_alloc_in_memory_pos()
Diffstat (limited to 'libbcachefs/two_state_shared_lock.h')
-rw-r--r-- | libbcachefs/two_state_shared_lock.h | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/libbcachefs/two_state_shared_lock.h b/libbcachefs/two_state_shared_lock.h index 1b4f1089..90580177 100644 --- a/libbcachefs/two_state_shared_lock.h +++ b/libbcachefs/two_state_shared_lock.h @@ -6,6 +6,8 @@ #include <linux/sched.h> #include <linux/wait.h> +#include "util.h" + /* * Two-state lock - can be taken for add or block - both states are shared, * like read side of rwsem, but conflict with other state: @@ -21,8 +23,37 @@ static inline void two_state_lock_init(two_state_lock_t *lock) init_waitqueue_head(&lock->wait); } -void bch2_two_state_unlock(two_state_lock_t *, int); -bool bch2_two_state_trylock(two_state_lock_t *, int); -void bch2_two_state_lock(two_state_lock_t *, int); +static inline void bch2_two_state_unlock(two_state_lock_t *lock, int s) +{ + long i = s ? 1 : -1; + + EBUG_ON(atomic_long_read(&lock->v) == 0); + + if (atomic_long_sub_return_release(i, &lock->v) == 0) + wake_up_all(&lock->wait); +} + +static inline bool bch2_two_state_trylock(two_state_lock_t *lock, int s) +{ + long i = s ? 1 : -1; + long v = atomic_long_read(&lock->v), old; + + do { + old = v; + + if (i > 0 ? v < 0 : v > 0) + return false; + } while ((v = atomic_long_cmpxchg_acquire(&lock->v, + old, old + i)) != old); + return true; +} + +void __bch2_two_state_lock(two_state_lock_t *, int); + +static inline void bch2_two_state_lock(two_state_lock_t *lock, int s) +{ + if (!bch2_two_state_trylock(lock, s)) + __bch2_two_state_lock(lock, s); +} #endif /* _BCACHEFS_TWO_STATE_LOCK_H */ |