ISO C11's stdatomic.h
Robert Edmonds
edmonds at mycre.ws
Tue Sep 8 01:25:07 UTC 2015
Hi,
Standard C now supports architecture-independent atomic operations via
the stdatomic.h header, no inline assembly required. This is supported
by gcc >= 4.9:
ISO C11 atomics (the _Atomic type specifier and qualifier and the
<stdatomic.h> header) are now supported.
(https://gcc.gnu.org/gcc-4.9/changes.html)
and also by clang >= 3.6:
Clang now provides an implementation of the standard C11 header
<stdatomic.h>.
(http://llvm.org/releases/3.6.0/tools/clang/docs/ReleaseNotes.html)
If I understand correctly, the isc_atomic_* functions could be
implemented in terms of the atomic_* functions as:
#include <stdatomic.h>
static inline isc_int32_t
isc_atomic_xadd(isc_int32_t *p, int val) {
return atomic_fetch_add(p, val);
}
static inline isc_int64_t
isc_atomic_xaddq(isc_int64_t *p, isc_int64_t val) {
return atomic_fetch_add(p, val);
}
static inline void
isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
atomic_store(p, val);
}
static inline isc_int32_t
isc_atomic_cmpxchg(isc_int32_t *p, int cmpval, int val) {
atomic_compare_exchange_strong(p, &cmpval, val);
return cmpval;
}
(This was originally suggested by James Cowgill on the debian-mips
mailing list.)
There are a number of CPU architectures that are capable of atomic
operations that are not supported by the existing
lib/isc/*/include/isc/atomic.h implementation (e.g., arm*, ppc64el,
s390x). It would be nice to support these architectures, as well as
future architectures, preferably without anyone having to write any more
assembly code.
Bugs in atomic implementations tend to be critical, see e.g. ISC-Bugs
#19227, #23469, #25181. It would be nice to avoid these kinds of bugs
by relying on intrinsics provided by the compiler where possible.
--
Robert Edmonds
More information about the bind-workers
mailing list