Skip to content

Commit

Permalink
provide atomic64_try_cmpxchg() for 32bit
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Overstreet <[email protected]>
  • Loading branch information
Kent Overstreet committed Jul 12, 2024
1 parent 0f9b409 commit 4be409a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/linux/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ void atomic64_sub(s64, atomic64_t *);

s64 atomic64_xchg(atomic64_t *, s64);
s64 atomic64_cmpxchg(atomic64_t *, s64, s64);
bool atomic64_try_cmpxchg(atomic64_t *, s64 *, s64);

#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
#define atomic64_inc(v) atomic64_add(1LL, (v))
Expand Down
15 changes: 15 additions & 0 deletions linux/atomic64.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
return val;
}

bool atomic64_try_cmpxchg(atomic64_t *v, s64 *o, s64 n)
{
unsigned long flags;
raw_spinlock_t *lock = lock_addr(v);

raw_spin_lock_irqsave(lock, flags);
bool ret = v->counter == *o;
if (ret)
v->counter = n;
else
*o = v->counter;
raw_spin_unlock_irqrestore(lock, flags);
return ret;
}

long long atomic64_xchg(atomic64_t *v, long long new)
{
unsigned long flags;
Expand Down

0 comments on commit 4be409a

Please sign in to comment.