Commit 75ea1e4
committed
libretro-common: add retro_atomic.h portable atomics primitive
A header-only API exposing acquire/release atomic loads, stores and
acq_rel fetch_add/fetch_sub for int and size_t, with a backend cascade
that picks the best primitive each toolchain offers:
1. C11 <stdatomic.h> - modern GCC/Clang/MSVC at -std=c11
2. C++11 <atomic> - C++ TUs at -std=c++11 or _MSVC_LANG
>= 201103L
3. GCC __atomic_* - GCC 4.7+ / Clang 3.1+
(Clang impersonates GCC 4.2 in
__GNUC__, so the gate uses
defined(__clang__) || version check
to avoid falling through to __sync)
4. MSVC Win32 Interlocked* - VS2003+, OG Xbox, Xbox 360 XDK; on
ARM/ARM64 the plain forms lack
barriers (PostgreSQL hit this on
Win11/ARM64 in 2025), so RMWs are
bracketed with __dmb on those targets
5. Apple OSAtomic* - PPC / pre-10.7 fallback
6. GCC __sync_* - GCC 4.1-4.6
7. volatile fallback - last resort, single-core / x86 TSO
only; emits a #warning unless
suppressed
Capability flags exposed to callers:
HAVE_RETRO_ATOMIC always defined after include
RETRO_ATOMIC_LOCK_FREE defined iff a real backend selected
(NOT for the volatile fallback)
RETRO_ATOMIC_BACKEND_NAME string literal, for diagnostics
RETRO_ATOMIC_REQUIRE_LOCK_FREE caller-side opt-in: setting this
before include turns the volatile
fallback into a #error
No active TU includes the header yet; it is the foundation for a future
SPSC fifo primitive and consolidates the hand-rolled atomic shims
currently scattered across coreaudio*.c/m, xaudio.c, mmdevice_common.c,
opensl.c, and gfx_thumbnail.c.
Sample: libretro-common/samples/atomic/retro_atomic_test/
Single-threaded property checks of every macro plus a 1M-iteration
SPSC stress test (when HAVE_THREADS) using rthreads sthread_create.
Compile-time #error checks assert that every named real backend
implies RETRO_ATOMIC_LOCK_FREE and that the volatile fallback never
sets it.
CI: Linux-libretro-common-samples.yml
- retro_atomic_test added to the native run allowlist (gcc, with
the workflow's default ASan/UBSan)
- new step: C++ smoke test compiled with both g++ and clang++ at
-std=c++11/14/17 against the in-tree header
- new step: retro_atomic_test built with clang -fsanitize=thread and
run with TSAN_OPTIONS=halt_on_error=1; TSan instruments every
atomic load/store and would flag a missing barrier in the SPSC
stress that x86 TSO would otherwise hide
- new job: retro-atomic-cross, matrix [aarch64, armv7], cross-compiles
with gcc-aarch64-linux-gnu / gcc-arm-linux-gnueabihf, runs the
binary under qemu-user-static, and grep-inspects the emitted asm
for ldar/stlr/ldadd*_acq_rel (aarch64) or dmb/ldrex/strex (armv7);
the inspect step exits 1 if no barrier mnemonics are found, which
catches a silent regression to the volatile fallback
Verified locally:
- x86_64 native (gcc, clang) + ASan/UBSan + TSan
- AArch64 cross-compile + qemu, asm shows ldar/stlr/ldadd*_acq_rel
- ARMv7 cross-compile + qemu, asm shows dmb/ldrex/strex
- MIPSel cross-compile + qemu, asm shows ll/sc/sync
- C++11/14/17/20 native (g++ and clang++)
- C++98 (g++ and clang++) correctly falls through to GCC __atomic_*
- All 9 forced-backend shape tests (C11, GCC __atomic_*, __sync_*,
volatile, MSVC x86/x64/ARM64 mocked, Apple 32/64 mocked) plus
forced C++11
Not verified on real hardware:
- MSVC ARM64 (correct by construction from MS docs and PostgreSQL
precedent; awaits Windows-on-ARM CI)
- Real PowerPC SMP (Wii U, Xbox 360); reasoned from devkitPPC GCC
and Microsoft's Xbox 360 lockless guide
- __sync_* and Apple OSAtomic backends (dead code on every current
target; selection requires GCC < 4.7 or pre-10.7 Apple)1 parent 677454f commit 75ea1e4
4 files changed
Lines changed: 1300 additions & 1 deletion
File tree
- .github/workflows
- libretro-common
- include
- samples/atomic/retro_atomic_test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
| |||
196 | 197 | | |
197 | 198 | | |
198 | 199 | | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
0 commit comments