nuttx: rework atomic operations - #19802
Draft
zhangyu-duck wants to merge 16 commits into
Draft
Conversation
The atomic implementation in machine/arch_atomic.c is now achieved by switching interrupts (up_irq_save/up_irq_restore) instead of using a spinlock. This version does not support SMP. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Remove the defined(__has_include) check from the outer guard. Add a fallback macro in compiler.h that defines __has_include(x) as 0 when the compiler does not natively support it. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
The memory order '__ATOMIC_xxx' is not a name specified by the standard. To achieve uniformity, if it is not defined, it will be defined one by one in the order of 0 to 5. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Refine the atomic Kconfig to support multiple backends: LIBC_ATOMIC_TOOLCHAIN (compiler builtins), LIBC_ATOMIC_ARCH (arch instructions), LIBC_ATOMIC_HWSPINLOCK (hardware spinlock), and LIBC_ATOMIC_IRQ (interrupt disable). Rename arch_atomic_irq.c to arch_atomic.c since it supports both IRQ and hwspinlock backends. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Use atomic_read and atomic_fetch_add_relaxed to access tg_nchildren, which is now an atomic type. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Replace atomic_load/atomic_store with atomic_read/atomic_set in call sites to use the unified interface defined in <nuttx/atomic.h>. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
The following types are not supported: 1. ARCH_ARMV6M, ARCH_ARM7TDMI, and ARCH_ARM926EJS architectures are not supported by the architecture itself. 2. Single-core CPUs, such as ARCH_CHIP_BM3823. 3. Special chips are not supported, such as ARCH_CHIP_RV32M1(RISV32). Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Change the hwspinlock implementation to be based on IRQ disable/restore instead of spinlock_irqsave, so it can be used in atomic lock/unlock functions that may be called before the scheduler is initialized. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Add hardware spinlock driver implementations for cxd56, rp2040, and lc823450 chips. These drivers provide the hwspinlock_ops_s interface used by the atomic hwspinlock backend. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Implement atomic_lock/atomic_unlock using hwspinlock when CONFIG_LIBC_ATOMIC_HWSPINLOCK is selected, and using up_irq_save/ up_irq_restore when CONFIG_LIBC_ATOMIC_IRQ is selected. Rename arch_atomic_irq.c to arch_atomic.c. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Add per-chip atomic hwspinlock device definitions for cxd56, rp2040, and lc823450. Select LIBC_ATOMIC_HWSPINLOCK for SMP and LIBC_ATOMIC_IRQ for !SMP on these chips. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Add #include <nuttx/atomic.h> to source files that use atomic operations but were missing the include, to pass the Apache CI build. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
1. for tasking, map __c11_atomic_xxx as tasking_atomic_xxx 2. for msvc, map _Interlocked_xxx as msvc_atomic_xxx 3. if no special map, use gcc/clang as default as they are most widely used. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
The reason for using builtin atomic is that in C++, when include <atomic> in <nuttx/atomic.h> easily conflicts with third-party function libraries. We wanted to completely separate the implementation of <nuttx/atomic.h>. There are two points: 1. use builtin function directly. 2. Without the standard library implementation, need implement "atomic_fetch_xxx", leading conflicts with the standard library used by third-party programs, introducing redefinition issues and requiring name changes. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
Rename atomic_fetch_add/sub/or/and/xor to atomic_add/sub/or/and/xor to avoid conflicts with the C/C++ standard library naming. The atomic_fetch_xxx naming is reserved by the standard; keeping it causes function name conflicts when source files indirectly include both <nuttx/atomic.h> and <atomic>/<stdatomic.h>. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
1. use _atomic as wrapper because if _Atomic empty, may affects the compilation of other files: 2. for clang builtin function, it donot accept param with keyword "_Atomic" Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
zhangyu-duck
requested review from
Donny9,
GUIDINGLI,
anchao,
davids5,
gustavonihei,
hartmannathan,
jerpelea,
johannes-nivus,
masayuki2009,
pkarashchenko,
pussuw,
tmedicci,
xiaoxiang781216 and
yamt
as code owners
August 12, 2026 07:51
zhangyu-duck
requested review from
acassis,
eren-terzioglu,
fdcavalcanti,
liuguo09 and
lupyuen
as code owners
August 12, 2026 07:51
zhangyu-duck
marked this pull request as draft
August 12, 2026 07:52
jerpelea
requested changes
Aug 12, 2026
jerpelea
left a comment
Contributor
There was a problem hiding this comment.
please follow contribution guidelines and provide proper PR description, impact and testing
zhangyu-duck
force-pushed
the
atomic-upstream
branch
from
August 12, 2026 08:07
4d94b05 to
29c889b
Compare
|
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
This PR reworks the atomic operation implementation in <nuttx/atomic.h> to provide a unified, portable, and standard-conflict-free atomic interface.
Key changes:
also conflicts with third-party C++ libraries that include — the standard library expects template types while NuttX atomic_t is int32_t, causing compile errors. The new implementation calls compiler builtins (_atomic* / _c11_atomic*
/ _Interlocked*) directly, similar to Zephyr.
causes function name conflicts and compile errors. Other non-standard API names (e.g., atomic_read, atomic_set, atomic_xchg) remain unchanged.
- LIBC_ATOMIC_TOOLCHAIN — compiler builtins (default, lock-free)
- LIBC_ATOMIC_ARCH — arch-native atomic instructions (lock-free)
- LIBC_ATOMIC_HWSPINLOCK — hardware spinlock wrapping critical section (cross-core, for chips without atomic instructions but with hwspinlock peripheral)
- LIBC_ATOMIC_IRQ — IRQ disable wrapping critical section (single-core only, fallback for ARMv6-M / ARM7TDMI / ARM926EJS etc.)