Skip to content

include/atomic: add atomic_ptr_t and release_acquire cmpxchg - #21843

Merged
casaroli merged 2 commits into
apache:masterfrom
CV-Bowen:up/P93-atomic
Nov 14, 2026
Merged

casaroli merged 2 commits into
apache:masterfrom
CV-Bowen:up/P93-atomic

Conversation

@CV-Bowen

Copy link
Copy Markdown
Contributor

Summary

Two additions to include/nuttx/atomic.h, needed by the lock-free ring
(include/nuttx/ring.h) that will follow in a separate PR.

  • include/atomic: add atomic_ptr_t type and operations: a pointer-width
    atomic type. It maps to atomic_t when UINTPTR_MAX <= UINT32_MAX
    and to atomic64_t otherwise, and every atomic_ptr_xxx() operation
    (set/read/add/sub/and/or/xor/xchg/cmpxchg/try_cmpxchg, with their
    memory-order variants) maps to the matching atomic_xxx() /
    atomic64_xxx() macro.
  • include/atomic: add cmpxchg_release_acquire memory order variant:
    atomic[64|_ptr]_[try_]cmpxchg_release_acquire(), a CAS that uses
    RELEASE on success and ACQUIRE on failure. The ring's tail-commit CAS
    (the DPDK RTS scheme) needs it: on failure the loop retries with the
    value another thread published, so it must acquire that thread's
    writes. The existing _release variant is RELAXED on failure.

Impact

  • New macros and one new typedef only; no existing macro changes.
  • include/nuttx/atomic.h now includes <stdint.h> for UINTPTR_MAX.
  • No Kconfig, build or ABI change. Both the toolchain builtins and the
    libs/libc/machine/arch_atomic.c fallback already take separate
    success/failure orders, so the new variant only passes different
    constants to the existing atomic_compare_exchange_[4|8]().

Testing

Host: Ubuntu 22.04.5 LTS x86_64; sim: gcc 13.1.0; qemu-armv7a: arm-none-eabi-gcc 10.3.1, QEMU 6.2.0; qemu-armv8a: aarch64-none-elf-gcc 12.2.1, QEMU 10.2.1

No in-tree user yet, so the macros were exercised by a small probe that
temporarily replaced apps/testing/libc/atomic/atomic_main.c (not part of
this PR). It checks sizeof(atomic_ptr_t) == sizeof(void *), every
atomic_ptr_* read-modify-write against its expected old/new value, and
for each *_cmpxchg_release_acquire() / *_try_cmpxchg_release_acquire()
both the success path (value replaced) and the failure path (value
untouched, expected updated to the current value).

Configured with CONFIG_TESTING_ATOMIC=y on top of:

configure atomic_ptr_t result
sim:nsh 8 bytes (atomic64_t) PASS, 0 warnings
qemu-armv7a:nsh 4 bytes (atomic_t) PASS, 0 warnings
qemu-armv8a:nsh_smp (-smp 4) 8 bytes (atomic64_t) PASS, 0 warnings
./nuttx
qemu-system-arm -cpu cortex-a7 -nographic \
  -M virt,virtualization=off,gic-version=2 -kernel nuttx
qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic \
  -machine virt,virtualization=on,gic-version=3 -net none \
  -chardev stdio,id=con,mux=on -serial chardev:con \
  -mon chardev=con,mode=readline -kernel nuttx
nsh> atomic
Probe source (drop-in for testing/libc/atomic/atomic_main.c)
#include <nuttx/config.h>
#include <stdio.h>
#include <stdint.h>
#include <nuttx/atomic.h>

static int g_fail;

#define CHECK(c) \
  do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); g_fail++; } } while (0)

int main(int argc, FAR char *argv[])
{
  static int a;
  static int b;
  atomic_ptr_t p;
  atomic_t v;
  atomic64_t v64;
  uintptr_t exp;
  int32_t e32;
  int64_t e64;

  printf("sizeof(atomic_ptr_t)=%zu sizeof(void *)=%zu\n",
         sizeof(atomic_ptr_t), sizeof(void *));
  CHECK(sizeof(atomic_ptr_t) == sizeof(void *));

  atomic_ptr_set(&p, (uintptr_t)&a);
  CHECK(atomic_ptr_read(&p) == (uintptr_t)&a);
  atomic_ptr_set_release(&p, (uintptr_t)&b);
  CHECK(atomic_ptr_read_acquire(&p) == (uintptr_t)&b);

  CHECK(atomic_ptr_xchg(&p, (uintptr_t)&a) == (uintptr_t)&b);
  CHECK(atomic_ptr_read(&p) == (uintptr_t)&a);

  atomic_ptr_set(&p, 0x10);
  CHECK(atomic_ptr_add(&p, 0x8) == 0x10);
  CHECK(atomic_ptr_sub(&p, 0x4) == 0x18);
  CHECK(atomic_ptr_or(&p, 0x100) == 0x14);
  CHECK(atomic_ptr_and(&p, 0x104) == 0x114);
  CHECK(atomic_ptr_xor(&p, 0x1) == 0x104);
  CHECK(atomic_ptr_read(&p) == 0x105);

  /* CAS success: value replaced, expected untouched */

  atomic_ptr_set(&p, (uintptr_t)&a);
  exp = (uintptr_t)&a;
  CHECK(atomic_ptr_cmpxchg(&p, &exp, (uintptr_t)&b));
  CHECK(atomic_ptr_read(&p) == (uintptr_t)&b);

  /* CAS failure: value untouched, expected gets the current value */

  exp = (uintptr_t)&a;
  CHECK(!atomic_ptr_cmpxchg_release_acquire(&p, &exp, (uintptr_t)&a));
  CHECK(exp == (uintptr_t)&b);
  CHECK(atomic_ptr_cmpxchg_release_acquire(&p, &exp, (uintptr_t)&a));
  CHECK(atomic_ptr_read(&p) == (uintptr_t)&a);

  exp = (uintptr_t)&a;
  while (!atomic_ptr_try_cmpxchg_release_acquire(&p, &exp, (uintptr_t)&b));
  CHECK(atomic_ptr_read(&p) == (uintptr_t)&b);

  atomic_set(&v, 5);
  e32 = 4;
  CHECK(!atomic_cmpxchg_release_acquire(&v, &e32, 9));
  CHECK(e32 == 5 && atomic_read(&v) == 5);
  CHECK(atomic_cmpxchg_release_acquire(&v, &e32, 9));
  CHECK(atomic_read(&v) == 9);
  e32 = 9;
  while (!atomic_try_cmpxchg_release_acquire(&v, &e32, 11));
  CHECK(atomic_read(&v) == 11);

  atomic64_set(&v64, 0x100000000ll);
  e64 = 1;
  CHECK(!atomic64_cmpxchg_release_acquire(&v64, &e64, 2));
  CHECK(e64 == 0x100000000ll);
  CHECK(atomic64_cmpxchg_release_acquire(&v64, &e64, 0x200000000ll));
  CHECK(atomic64_read(&v64) == 0x200000000ll);
  e64 = 0x200000000ll;
  while (!atomic64_try_cmpxchg_release_acquire(&v64, &e64, 3));
  CHECK(atomic64_read(&v64) == 3);

  printf("atomic_ptr probe: %s (%d failures)\n",
         g_fail ? "FAIL" : "PASS", g_fail);
  return g_fail;
}
Output
# sim
sizeof(atomic_ptr_t)=8 sizeof(void *)=8
atomic_ptr probe: PASS (0 failures)
# v7a
sizeof(atomic_ptr_t)=4 sizeof(void *)=4
atomic_ptr probe: PASS (0 failures)
# v8a
sizeof(atomic_ptr_t)=8 sizeof(void *)=8
atomic_ptr probe: PASS (0 failures)

Not run: a CONFIG_LIBC_ATOMIC_TOOLCHAIN=n target (e.g. Cortex-M0).
That path goes through the same atomic_compare_exchange_[4|8]() that the
existing _acquire/_release variants already use.

No regression in rpmsg: sim:rpserver/rpproxy and qemu-armv8a:rpserver/rpproxy boot and pass the fixed rpmsg cases (dump, ping, rpmsgfs; 15/15). qemu-armv8a was built with LIBC_ENVPATH and BINFMT_CHECKEXEC disabled, which current master needs for nxinit to start nsh there; unrelated to this change.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Nov 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Add atomic_ptr_t that maps to atomic_t on 32-bit platforms and
atomic64_t on 64-bit platforms based on UINTPTR_MAX. All atomic_ptr_xx
operations (set, read, add, sub, and, or, xor, xchg, cmpxchg,
try_cmpxchg with memory order variants) are mapped to the corresponding
atomic_xx or atomic64_xx macros accordingly.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
Add atomic_cmpxchg_release_acquire and atomic_try_cmpxchg_release_acquire
macros for 32-bit, 64-bit, and pointer-width atomic operations.

This variant uses RELEASE on CAS success and ACQUIRE on CAS failure,
matching the memory ordering used by DPDK's RTS ring tail-commit CAS
where the failure path must re-acquire visibility of updates from
other threads before retrying.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
@casaroli
casaroli merged commit 43d9de6 into apache:master Nov 14, 2026
52 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants