Re: Trying out <stdatomic.h>

From: Greg Burd <greg(at)burd(dot)me>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Trying out <stdatomic.h>
Date: 2025-11-23 15:22:47
Message-ID: 336c6acf-39ce-4a06-870e-3055400a5d8d@app.fastmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On Mon, Nov 10, 2025, at 8:17 AM, Thomas Munro wrote:
> Hi,
>
> Here is an experimental patch to try out standard C (and C++) atomics
> to implement port/atomics.h, and also add more types and operations.
> It's mostly just redirecting our names to the standard ones, except
> for our barriers and slightly extended pg_atomic_flag, so there isn't
> much left of the code.
>
> Here also is a semi-independent patch to implement storage/spin.h with
> pg_atomic_flag. It keeps a small amount of the architecture-specific
> magic, but moves it out to src/port/spin_delay.h.
>
> I'm not saying they're correct, performant or portable yet, that'll
> require studying codegen and performance on a bunch of weird and
> wonderful systems, but they at least passes basic testing on the usual
> suspects and CI systems, except for Windows which needs a newer
> toolchain so I haven't tried yet. It should hopefully just work™ on
> VS 2022 (same version that provides <threads.h>, about which more
> soon).
>
> All that being the case, it's not far enough along to be a serious
> proposal, but I imagine others will be looking into things like this
> since we pulled the trigger on C11 and I figured I might as well share
> a basic working patch set to avoid duplicated effort... Hopefully it
> works well enough to kick the tyres and try to find the difficult
> problems, test it out on weird systems, etc etc.
>
> Attachments:
> * v1-0001-Add-some-missing-include-limits.h.patch
> * v1-0002-Redefine-port-atomics.h-on-top-of-stdatomic.h.patch
> * v1-0003-Use-atomics-API-to-implement-spinlocks.patch
> * v1-0004-Remove-configure-meson-checks-for-atomics.patch

Hello Thomas,

First off, thanks for working on this set of changes. I like your changes in general, except for removing Solaris/Illumos (but I get it). ;-P

As mentioned on a separate thread about fixing ARM64 support when building with MSVC on Win11 [1] I tried out this patch. The reply on that thread had an issue with _mm_pause() in spin_delay(), it turns out we need to use __yield() [2]. I went ahead and fixed that, so ignore that patch on the other thread [1]. The new patch attached that layers on top of your work and supports that platform, there was one minor change that was required:

#ifdef _MSC_VER

/*
* If using Visual C++ on Win64, inline assembly is unavailable. Use a
* _mm_pause intrinsic instead of rep nop. For ARM64, use the __yield()
* intrinsic which emits the YIELD instruction as a hint to the processor.
*/
#if defined(_M_ARM64)
__yield();
#elif defined(_WIN64)
_mm_pause();
#else
/* See comment for gcc code. Same code, MASM syntax */
__asm rep nop;
#endif
#endif /* _MSC_VER */

Visual Studio 2026 (Community)
cl (msvc 19.50.35718 "Microsoft (R) C/C++ Optimizing Compiler Version 19.50.35718 for ARM64")
link 14.50.35718.0

tests are passing, best.

-greg

[1] https://www.postgresql.org/message-id/3c576ad7-d2da-4137-b791-5821da7cc370%40app.fastmail.com
[2] https://learn.microsoft.com/en-us/cpp/intrinsics/arm64-intrinsics?view=msvc-180

Attachment Content-Type Size
v20251123-0001-WIP-ARM64-MSVC-stdatomic.patch application/octet-stream 4.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Joel Jacobson 2025-11-23 15:49:26 Re: Optimize LISTEN/NOTIFY
Previous Message Greg Burd 2025-11-23 15:02:29 Re: Trying out <stdatomic.h>