Skip to content

Support C++23 atomic shims#941

Open
bc-lee wants to merge 1 commit into
swiftlang:mainfrom
bc-lee:feature/cpp23-atomic-shims
Open

Support C++23 atomic shims#941
bc-lee wants to merge 1 commit into
swiftlang:mainfrom
bc-lee:feature/cpp23-atomic-shims

Conversation

@bc-lee

@bc-lee bc-lee commented Apr 27, 2026

Copy link
Copy Markdown

In C++23, libc++'s <stdatomic.h> exposes std::atomic-based APIs instead of the C atomic interfaces that libdispatch's shim expects. That makes the existing _Atomic storage incompatible with atomic_load_explicit(), atomic_fetch_*(), and related calls when libdispatch is compiled as C++.

Route libdispatch's internal atomic operations through private _dispatch_atomic_* aliases. Clang C++23 keeps _Atomic storage and uses __c11_atomic_* builtins directly. GCC C++23 uses plain storage with __atomic_* builtins so the shim does not depend on public <stdatomic.h> names in that mode. C and older C++ modes continue to use <stdatomic.h>.

Fixes #940.

In C++23, libc++'s <stdatomic.h> exposes std::atomic-based APIs
instead of the C atomic interfaces that libdispatch's shim expects.
That makes the existing _Atomic storage incompatible with
atomic_load_explicit(), atomic_fetch_*(), and related calls when
libdispatch is compiled as C++.

Route libdispatch's internal atomic operations through private
_dispatch_atomic_* aliases. Clang C++23 keeps _Atomic storage and uses
__c11_atomic_* builtins directly. GCC C++23 uses plain storage with
__atomic_* builtins so the shim does not depend on public <stdatomic.h>
names in that mode. C and older C++ modes continue to use <stdatomic.h>.
@compnerd

Copy link
Copy Markdown
Member

ibdispatch is a C library, not a C++ library, so I'm not sure I understand why this should be needed.

@bc-lee

bc-lee commented Apr 27, 2026

Copy link
Copy Markdown
Author

libdispatch is primarily a C library, but this repo does compile C++ code, including src/block.cpp. That file includes internal.h -> shims.h -> shims/atomic.h, so a C++23 <stdatomic.h> incompatibility is relevant if that translation unit is built in C++23 mode.

@bc-lee

bc-lee commented May 25, 2026

Copy link
Copy Markdown
Author

The C23/C++23 atomic incompatibility is real. It was also mentioned in the Hacker News comment:

C _Atomic(T) and C++ std::atomic<T>. C++23 has C compatible header stdatomic.h that defines _Atomic(T), but it's still problematic

This repository exposes that problem in practice.

As I noted earlier, although libdispatch is primarily a C library, this repository does compile C++ code, including src/block.cpp. That means C++23 <stdatomic.h> behavior is relevant here.

I suggest that the maintainers reconsider this issue/PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C++23 <stdatomic.h> is incompatible with libdispatch's _Atomic shim

2 participants