CV-Bowen opened a new pull request, #21843:
URL: https://github.com/apache/nuttx/pull/21843

   ## 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
   ```
   
   <details><summary>Probe source (drop-in for 
testing/libc/atomic/atomic_main.c)</summary>
   
   ```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;
   }
   ```
   
   </details>
   
   <details><summary>Output</summary>
   
   ```
   # 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)
   ```
   
   </details>
   
   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to