Optimize pg_atomic_exchange_u32 and pg_atomic_exchange_u64.
commit64b1fb5f03266f0ef4eef3ad2b7d97170bb05b78
authorNathan Bossart <nathan@postgresql.org>
Mon, 18 Dec 2023 16:53:32 +0000 (18 10:53 -0600)
committerNathan Bossart <nathan@postgresql.org>
Mon, 18 Dec 2023 16:53:32 +0000 (18 10:53 -0600)
treef86b9f72ff2981444588df8cce802e91a9dfa1d8
parent0d1adae6f739273046705acaf5314384e8a73a18
Optimize pg_atomic_exchange_u32 and pg_atomic_exchange_u64.

Presently, all platforms implement atomic exchanges by performing
an atomic compare-and-swap in a loop until it succeeds.  This can
be especially expensive when there is contention on the atomic
variable.  This commit optimizes atomic exchanges on many platforms
by using compiler intrinsics, which should compile into something
much less expensive than a compare-and-swap loop.  Since these
intrinsics have been available for some time, the inline assembly
implementations are omitted.

Suggested-by: Andres Freund
Reviewed-by: Andres Freund
Discussion: https://postgr.es/m/20231129212905.GA1258737%40nathanxps13
src/include/port/atomics/generic-gcc.h
src/include/port/atomics/generic-msvc.h
src/include/port/atomics/generic-sunpro.h