libc/atomic: refactor inline arch atomics, add atomic_ptr_t/atomic64/cmpxchg_release_acquire - #20043
Open
zhangyu-duck wants to merge 6 commits into
Open
libc/atomic: refactor inline arch atomics, add atomic_ptr_t/atomic64/cmpxchg_release_acquire#20043zhangyu-duck wants to merge 6 commits into
zhangyu-duck wants to merge 6 commits into
Conversation
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>
When CONFIG_LIBC_ATOMIC_ARCH is selected, atomic.h includes arch/atomic.h which provides static inline atomic_*_4 operations using arch-specific helpers. This eliminates function call overhead for NuttX atomic API. For Tricore, the inline functions use hardware helpers (tricore_atomic_swap, tricore_atomic_cmpswap) implemented with inline assembly (swap.w, cmpswap.w), avoiding the iLLD dependency. The arch_atomic.c retains __atomic_*_4 symbols with external linkage for GCC libatomic ABI compatibility. Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
|
1. Add include/nuttx/lib/arch_atomic.h with inline atomic_*_4/8 and
__sync_*_{1,2,4,8} operations using IRQ disable/enable or hwspinlock
under CONFIG_LIBC_ATOMIC_IRQ / CONFIG_LIBC_ATOMIC_HWSPINLOCK.
2. Include the new header from include/nuttx/atomic.h so the NuttX
atomic API becomes inline (no function call overhead).
3. Reduce libs/libc/machine/arch_atomic.c to only export __atomic_*
and __sync_* symbols for GCC libatomic ABI; delete the local
arch_atomic.h and arch_atomic64.c (superseded); update Make.defs
and CMakeLists.txt accordingly.
4. Fix a pre-existing race in SYNC_*_FETCH macros where the return
value was read from shared memory after releasing the lock; now
captured in a local variable before unlocking.
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
1. Add ARCH_HAVE_ATOMIC_4 guard in arch/tricore/include/atomic.h so
the tricore arch declares which sizes have hardware atomic support.
2. Extend include/nuttx/lib/arch_atomic.h to #undef arch overrides
and provide IRQ-based inline fallback for sizes the arch does not
support, so generic code covers all {1,2,4,8} sizes uniformly.
3. Update libs/libc/machine/arch_atomic.c to gate per-size symbol
export on ARCH_HAVE_ATOMIC_X and 64-bit on UINTPTR_MAX > UINT32_MAX;
delete libs/libc/machine/tricore/arch_atomic.c (now merged into
the generic machine/arch_atomic.c).
4. Drop tricore-specific arch_atomic.c references from
libs/libc/machine/tricore/{Make.defs,CMakeLists.txt}.
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
1. Add atomic_cmpxchg_release_acquire and atomic_try_cmpxchg_release_acquire for 32-bit (atomic_compare_exchange_4) using RELEASE on CAS success. 2. Add 64-bit counterparts atomic64_cmpxchg_release_acquire and atomic64_try_cmpxchg_release_acquire (atomic_compare_exchange_8). 3. Add pointer-width aliases atomic_ptr_cmpxchg_release_acquire and atomic_ptr_try_cmpxchg_release_acquire for both 64-bit and 32-bit pointer configurations, expanding the existing ptr_*_cmpxchg family. 4. Use ACQUIRE on CAS failure so the retry path re-acquires visibility of updates from other threads, matching the ordering used by DPDK RTS ring tail-commit CAS. Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
libc/machine/arch_atomic64.c implementing atomic_*_8 on a single spinlock. All helpers are weak_function so an arch with native 64-bit support overrides at link time Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
zhangyu-duck
force-pushed
the
atomic_part2
branch
from
September 3, 2026 08:08
55f6842 to
2802f3b
Compare
zhangyu-duck
marked this pull request as ready for review
September 4, 2026 01:49
zhangyu-duck
requested review from
masayuki2009 and
xiaoxiang781216
as code owners
September 4, 2026 01:49
| ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_or_ ## n, t, n, |) \ | ||
| ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_xor_ ## n, t, n, ^) | ||
|
|
||
| #define ARCH_HAVE_ATOMIC_4 |
| #include <nuttx/config.h> | ||
|
|
||
| #define ARCH_ATOMIC_SPECIFIER | ||
| #define ARCH_ATOMIC_SPECIFIER weak_function |
| #ifdef ARCH_HAVE_ATOMIC_4 | ||
| ARCH_ATOMIC_DEFINE(__atomic, uint32_t, 4) | ||
| #endif | ||
| #ifdef ARCH_HAVE_ATOMIC_8 |
| * but 64 bit atomics are rare enough that a single lock is not a bottleneck. | ||
| */ | ||
|
|
||
| static spinlock_t g_atomic64_lock = SP_UNLOCKED; |
Contributor
There was a problem hiding this comment.
should use irq_save/restore in UP, could share with 32bit implementation
|
|
||
| static inline irqstate_t atomic64_lock(void) | ||
| { | ||
| return spin_lock_irqsave(&g_atomic64_lock); |
Contributor
There was a problem hiding this comment.
let's inline the implementation too
| * Pre-processor Definitions | ||
| ****************************************************************************/ | ||
|
|
||
| #define ATOMIC64_STORE(func, t) \ |
Contributor
There was a problem hiding this comment.
let's reuse ATOMIC_DEFINE we can refine ATOMIC_DEFINE to accept the lock/unlock funciton
| #define ARCH_HAVE_ATOMIC_1 | ||
| #define ARCH_HAVE_ATOMIC_2 | ||
| #define ARCH_HAVE_ATOMIC_4 | ||
| #define ARCH_HAVE_ATOMIC_8 |
Contributor
There was a problem hiding this comment.
let's keep, but provide the different lock/unlock for 64:
- atomic for 64bit arch
- irq save/restore for UP&32bit
- spinlock for SMP&32bit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactor NuttX's atomic operations to support inline arch-specific implementations, add new atomic types/variants, and provide 64-bit atomic support via spinlock.
Commits
include/atomic: add atomic_ptr_t type and operations— add pointer-width atomic type and operationslibc/atomic: support inline arch atomic for tricore— tricore inlineatomic_*_4viaswap.w/cmpswap.wlibc/atomic: support inline arch atomic for generic IRQ path— generic IRQ/hwspinlock-based atomic path for archs without hardware atomicslibc/atomic: merge tricore arch_atomic.c into machine/arch_atomic.c— unify tricore atomic implementation into the genericmachine/arch_atomic.cinclude/atomic: add cmpxchg_release_acquire memory order variant— new cmpxchg memory order matching DPDK RTS ring tail-commit orderinglibc: realize atomic64 via a spinlock helper— 8-byte atomic symbols on a single spinlock for archs without native 64-bit supportImpact
atomic_ptr_ttype: pointer-width atomic type and operations (commit 1)atomic_*_4inlined viaswap.w/cmpswap.whardware instructions, no function call overhead (commit 2)arch_atomic.cmerged into genericmachine/arch_atomic.c(commit 4)atomic_cmpxchg_release_acquireandatomic_try_cmpxchg_release_acquirefor 32/64/pointer-width (commit 5)atomic_*_8,__atomic_*_8,sync_*_8,__sync_*_8symbols provided vialibs/libc/machine/arch_atomic64.con a single spinlock (g_atomic64_lock), usingspin_lock_irqsavefor SMP correctness and local ISR safety; all helpersweak_functionso archs with native 64-bit support can override at link time (commit 6)