summaryrefslogtreecommitdiff
path: root/libbcachefs/two_state_shared_lock.c
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2022-11-13 20:04:21 -0500
committerKent Overstreet <kent.overstreet@linux.dev>2022-11-13 20:07:22 -0500
commitc1e4d447f6dd0ee60495b651436d2055db7777ed (patch)
tree36a657f7018cecc6bad43e0e178ef1913154eba0 /libbcachefs/two_state_shared_lock.c
parent980f7437e2588d100456640cb863908a3cc6fc77 (diff)
Update bcachefs sources to 8d3fc97ca3 bcachefs: Fixes for building in userspace
Diffstat (limited to 'libbcachefs/two_state_shared_lock.c')
-rw-r--r--libbcachefs/two_state_shared_lock.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/libbcachefs/two_state_shared_lock.c b/libbcachefs/two_state_shared_lock.c
new file mode 100644
index 00000000..dc508d54
--- /dev/null
+++ b/libbcachefs/two_state_shared_lock.c
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "two_state_shared_lock.h"
+
+void bch2_two_state_unlock(two_state_lock_t *lock, int s)
+{
+ long i = s ? 1 : -1;
+
+ BUG_ON(atomic_long_read(&lock->v) == 0);
+
+ if (atomic_long_sub_return_release(i, &lock->v) == 0)
+ wake_up_all(&lock->wait);
+}
+
+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 *lock, int s)
+{
+ wait_event(lock->wait, bch2_two_state_trylock(lock, s));
+}