From: Wilco Dijkstra Date: Mon, 8 Sep 2025 17:46:29 +0000 (+0000) Subject: atomic: Use builtin atomics with USE_ATOMIC_COMPILER_BUILTINS X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2a035debbbd20402ada4543bf7e054f993ad0d69;p=thirdparty%2Fglibc.git atomic: Use builtin atomics with USE_ATOMIC_COMPILER_BUILTINS 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. Reviewed-by: Adhemerval Zanella --- diff --git a/include/atomic.h b/include/atomic.h index 16b36d80f1..98ba808ee9 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); })