mirror of
https://github.com/torvalds/linux.git
synced 2024-11-22 12:11:40 +00:00
68573b936d
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>
59 lines
1.3 KiB
C
59 lines
1.3 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef _BCACHEFS_TWO_STATE_LOCK_H
|
|
#define _BCACHEFS_TWO_STATE_LOCK_H
|
|
|
|
#include <linux/atomic.h>
|
|
#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:
|
|
*/
|
|
typedef struct {
|
|
atomic_long_t v;
|
|
wait_queue_head_t wait;
|
|
} two_state_lock_t;
|
|
|
|
static inline void two_state_lock_init(two_state_lock_t *lock)
|
|
{
|
|
atomic_long_set(&lock->v, 0);
|
|
init_waitqueue_head(&lock->wait);
|
|
}
|
|
|
|
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 old;
|
|
|
|
old = atomic_long_read(&lock->v);
|
|
do {
|
|
if (i > 0 ? old < 0 : old > 0)
|
|
return false;
|
|
} while (!atomic_long_try_cmpxchg_acquire(&lock->v, &old, old + i));
|
|
|
|
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 */
|