Re: [RFC PATCH 03/15] Provide atomic_t functions implemented with ISO-C++11 atomics
From: Peter Zijlstra
Date: Wed May 18 2016 - 13:32:30 EST
On Wed, May 18, 2016 at 04:10:59PM +0100, David Howells wrote:
> +/**
> + * __atomic_add_unless - add unless the number is already a given value
> + * @v: pointer of type atomic_t
> + * @a: the amount to add to v...
> + * @u: ...unless v is equal to u.
> + *
> + * Atomically adds @a to @v, so long as @v was not already @u.
> + * Returns the old value of @v.
> + */
> +static __always_inline int __atomic_add_unless(atomic_t *v,
> + int addend, int unless)
> +{
> + int c = atomic_read(v);
> +
> + while (likely(c != unless)) {
> + if (__atomic_compare_exchange_n(&v->counter,
> + &c, c + addend,
> + false,
> + __ATOMIC_SEQ_CST,
> + __ATOMIC_RELAXED))
> + break;
> + }
> + return c;
> +}
Does this generate 'sane' code for LL/SC archs? That is, a single LL/SC
loop and not a loop around an LL/SC cmpxchg.