summaryrefslogtreecommitdiffstats
path: root/fs/bcachefs/two_state_shared_lock.h
diff options
context:
space:
mode:
authorUros Bizjak <ubizjak@gmail.com>2024-05-23 11:19:26 +0200
committerKent Overstreet <kent.overstreet@linux.dev>2024-07-15 01:00:12 +0200
commit68573b936d3fceda9cd5cce3a577e035d19ad426 (patch)
tree872f291bf9b8d6f439c9d0a5bd372d42557e8e6b /fs/bcachefs/two_state_shared_lock.h
parentbcachefs: add might_sleep() annotations for fsck_err() (diff)
downloadlinux-68573b936d3fceda9cd5cce3a577e035d19ad426.tar.xz
linux-68573b936d3fceda9cd5cce3a577e035d19ad426.zip
bcachefs: Use try_cmpxchg() family of functions instead of cmpxchg()
Use try_cmpxchg() family of functions instead of cmpxchg (*ptr, old, new) == old. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg). Also, try_cmpxchg() implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop. No functional change intended. Signed-off-by: Uros Bizjak <ubizjak@gmail.com> Cc: Kent Overstreet <kent.overstreet@linux.dev> Cc: Brian Foster <bfoster@redhat.com> 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.h11
1 files changed, 5 insertions, 6 deletions
diff --git a/fs/bcachefs/two_state_shared_lock.h b/fs/bcachefs/two_state_shared_lock.h
index 905801772002..7f647846b511 100644
--- a/fs/bcachefs/two_state_shared_lock.h
+++ b/fs/bcachefs/two_state_shared_lock.h
@@ -36,15 +36,14 @@ static inline void bch2_two_state_unlock(two_state_lock_t *lock, int s)
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;
+ long old;
+ old = atomic_long_read(&lock->v);
do {
- old = v;
-
- if (i > 0 ? v < 0 : v > 0)
+ if (i > 0 ? old < 0 : old > 0)
return false;
- } while ((v = atomic_long_cmpxchg_acquire(&lock->v,
- old, old + i)) != old);
+ } while (!atomic_long_try_cmpxchg_acquire(&lock->v, &old, old + i));
+
return true;
}