[PATCH 1/8] atomic: Use builtin atomics with USE_ATOMIC_COMPILER_BUILTINS

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Sep 9 13:02:43 GMT 2025



On 09/09/25 08:12, Wilco Dijkstra wrote:
> 
> Use builtin atomics for atomic_compare_and_exchange_* and atomic_exchange_and_add if
> USE_ATOMIC_COMPILER_BUILTINS is enabled.  This allows removing target atomic-machine.h
> headers.
> 
> Passes buildmanyglibc, OK for commit?

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>> 
> ---
> 
> diff --git a/include/atomic.h b/include/atomic.h
> index 16b36d80f186cab8e2bede3a7d7e79907d3344f1..98ba808ee9e3bdd30a81d62bacfdeb8333c061d9 100644
> --- a/include/atomic.h
> +++ b/include/atomic.h
> @@ -82,6 +82,36 @@
>      __atg2_result;							      \
>    })
>  
> +#if USE_ATOMIC_COMPILER_BUILTINS
> +
> +# undef atomic_compare_and_exchange_val_acq
> +# define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
> +  ({									      \
> +     __typeof (*(mem)) __atg3_old = (oldval);				      \
> +     atomic_compare_exchange_acquire (mem, (void*)&__atg3_old, newval);	      \
> +     __atg3_old;							      \
> +  })
> +
> +# undef atomic_compare_and_exchange_val_rel
> +# define atomic_compare_and_exchange_val_rel(mem, newval, oldval)	      \
> +  ({									      \
> +     __typeof (*(mem)) __atg3_old = (oldval);				      \
> +     atomic_compare_exchange_release (mem, (void*)&__atg3_old, newval);	      \
> +     __atg3_old;							      \
> +  })
> +
> +# undef atomic_compare_and_exchange_bool_acq
> +# define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
> +  ({									      \
> +     __typeof (*(mem)) __atg3_old = (oldval);				      \
> +     !atomic_compare_exchange_acquire (mem, (void*)&__atg3_old, newval);      \
> +  })
> +
> +# undef atomic_exchange_and_add
> +# define atomic_exchange_and_add(mem, val) atomic_fetch_add_relaxed(mem,val)
> +
> +#endif
> +
>  
>  /* Atomically store NEWVAL in *MEM if *MEM is equal to OLDVAL.
>     Return the old *MEM value.  */
> @@ -603,6 +633,19 @@ void __atomic_link_error (void);
>    __atomic_compare_exchange_n ((mem), (expected), (desired), 1,		      \
>      __ATOMIC_RELEASE, __ATOMIC_RELAXED); })
>  
> +# define atomic_compare_exchange_relaxed(mem, expected, desired) \
> +  ({ __atomic_check_size((mem));					      \
> +  __atomic_compare_exchange_n ((mem), (expected), (desired), 0,		      \
> +    __ATOMIC_RELAXED, __ATOMIC_RELAXED); })
> +# define atomic_compare_exchange_acquire(mem, expected, desired) \
> +  ({ __atomic_check_size((mem));					      \
> +  __atomic_compare_exchange_n ((mem), (expected), (desired), 0,		      \
> +    __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); })
> +# define atomic_compare_exchange_release(mem, expected, desired) \
> +  ({ __atomic_check_size((mem));					      \
> +  __atomic_compare_exchange_n ((mem), (expected), (desired), 0,		      \
> +    __ATOMIC_RELEASE, __ATOMIC_RELAXED); })
> +
>  # define atomic_exchange_relaxed(mem, desired) \
>    ({ __atomic_check_size((mem));					      \
>    __atomic_exchange_n ((mem), (desired), __ATOMIC_RELAXED); })
> 



More information about the Libc-alpha mailing list