summaryrefslogtreecommitdiffstats
path: root/fs/bcachefs/two_state_shared_lock.h
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2022-11-23 12:22:39 -0500
committerKent Overstreet <kent.overstreet@linux.dev>2023-10-22 17:09:51 -0400
commit19fe87e00b6a601b2ec8251d0231f4c9b3bb5002 (patch)
tree0361c52ec7e58c284d54204c41f9e077737f8a43 /fs/bcachefs/two_state_shared_lock.h
parenta8b3a677e786fa869d220a6a78b5532a36dc2f4d (diff)
downloadlinux-stable-19fe87e00b6a601b2ec8251d0231f4c9b3bb5002.tar.gz
linux-stable-19fe87e00b6a601b2ec8251d0231f4c9b3bb5002.tar.bz2
linux-stable-19fe87e00b6a601b2ec8251d0231f4c9b3bb5002.zip
bcachefs: Inline bch2_two_state_(trylock|unlock)
Standard inlining of fast paths - these locks are now used by our new nocow mode. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'fs/bcachefs/two_state_shared_lock.h')
-rw-r--r--fs/bcachefs/two_state_shared_lock.h37
1 files changed, 34 insertions, 3 deletions
diff --git a/fs/bcachefs/two_state_shared_lock.h b/fs/bcachefs/two_state_shared_lock.h
index 1b4f108908a1..905801772002 100644
--- a/fs/bcachefs/two_state_shared_lock.h
+++ b/fs/bcachefs/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 */