Re: using C11's _Atomic()
[email protected] (Tony Cook) Tue, 2 Jun 2026 09:55:42 +1000
| Newsgroups | perl.perl5.porters |
|---|---|
| Message-ID | <[email protected]> |
On Mon, Jun 01, 2026 at 11:24:11AM +0100, Dave Mitchell wrote: > On Thu, May 07, 2026 at 01:10:53AM +0200, Leon Timmermans wrote: > > Strictly it's probably UB, but practically any lock-free atomic type should > > compile down to its underlying type as its representation in either case, > > so I'm not overly worried until I've seen proof otherwise. > > So, with PR #24439, I've now got atomics (apparently) working where the > system's ability to use atomics is manually specified via > > Configure -Accflags=-DPERL_USE_ATOMIC > > The next step is to convert this to a Configure probe. This is an area I'm > very unfamiliar with, so I would like advice as to whether the proposal > below is technically sound, and also advice on how to add it to Configure, > including what naming conventions to use for all the Configure/config > variables and defines. > > I envisage a combined test C/C++ file like the following: > > #ifdef __cplusplus > # include <atomic> > # define PERL_ATOMIC(atype) std::atomic<atype> > #else > # include <stdatomic.h> > /* 2 indicates guaranteed to be lock-free */ > # if ATOMIC_INT_LOCK_FREE == 2 && ATOMIC_POINTER_LOCK_FREE == 2 > # define PERL_ATOMIC(atype) _Atomic(atype) > # endif > #endif C++ <atomic> defines the ATOMIC_INT_LOCK_FREE and ATOMIC_POINTER_LOCK_FREE macros with the same values as C does in <stdatomic.h>, so those could be tested here too. > This would be compiled and run *twice*, once using the C compiler, and > once using the C++ compiler (does Configure locate a C++ compiler?) Configure doesn't. When working on cpphdrcheck.t I found at least one working C compiler whose C++ friend didn't build trivial C++11 code, so I'm worried this may prevent Configure succeeding if the C++ compiler is broken. It might be better to just make the C++ side `#error` if ATOMIC_INT_LOCK_FREE and ATOMIC_POINTER_LOCK_FREE don't have appropriate values, which seems unlikely. Or static assert sizeof(int) == sizeof(PERL_ATOMIC(int)) and similarly for pointers. Tony