feat(core): Add 8-bit and 16-bit atomic operation support#11244
feat(core): Add 8-bit and 16-bit atomic operation support#11244wdfk-prog wants to merge 1 commit intoRT-Thread:masterfrom
Conversation
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
There was a problem hiding this comment.
Pull request overview
This PR extends RT-Thread’s atomic API to support 8-bit and 16-bit operations, including ARM hardware implementations and software fallbacks.
本次 PR 扩展 RT-Thread 原子操作接口,新增 8/16 位原子操作支持,包含 ARM 硬件实现与软件回退实现。
Changes / 变更:
- Add Kconfig switches to describe arch-level availability of 8/16-bit HW atomics (selected for multiple ARM Cortex variants).
新增 Kconfig 选项用于描述架构是否支持 8/16 位硬件原子操作(并对多种 ARM Cortex 变体进行选择)。 - Add ARM (ldrex/strex-based) implementations for 8/16-bit load/store and and/or.
为 ARM 增加基于 ldrex/strex 的 8/16 位 load/store 以及 and/or 实现。 - Add new atomic types and public API/macros for 8/16-bit operations, with intended soft fallbacks.
新增 8/16 位原子类型与对外 API/宏,并引入软件回退路径。
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
libcpu/Kconfig |
Introduces ARCH_USING_HW_ATOMIC_8/16 and selects them for several ARM Cortex configs. |
libcpu/arm/common/atomic_arm.c |
Adds ARM-side 8/16-bit exclusive load/store intrinsics and new atomic ops. |
include/rttypes.h |
Adds rt_atomic8_t / rt_atomic16_t type definitions (including _Atomic variants). |
include/rtatomic.h |
Adds new 8/16-bit atomic APIs/macros and soft-atomic helper implementations. |
You can also share your feedback on Copilot code review. Take the survey.
为什么提交这份PR (why to submit this PR)
当前原子操作接口主要面向
rt_atomic_t,缺少对 8 位和 16 位原子类型的统一支持。这会带来以下问题:
因此,提交此 PR,为 RT-Thread 原子操作框架补充 8 位和 16 位原子类型及其相关接口,并同时支持硬件实现和软件回退路径。
你的解决方案是什么 (what is your solution)
本 PR 主要做了以下改动:
在
rttypes.h中新增原子类型定义:rt_atomic8_trt_atomic16_t在
rtatomic.h中新增 8 位和 16 位原子操作接口,包括:load/storeand/or对标准原子实现路径(
RT_USING_STDC_ATOMIC)补充对应宏映射,使 8 位和 16 位接口可以直接映射到标准原子操作;对硬件原子实现路径(
RT_USING_HW_ATOMIC)增加分层支持:ARCH_USING_HW_ATOMIC_8ARCH_USING_HW_ATOMIC_16在纯软件原子路径中新增以下实现:
rt_soft_atomic_load8rt_soft_atomic_store8rt_soft_atomic_load16rt_soft_atomic_store16rt_soft_atomic_and8rt_soft_atomic_or8rt_soft_atomic_and16rt_soft_atomic_or16这些实现通过关中断保护临界区,保证原子性;
在
libcpu/Kconfig中新增架构能力配置项:ARCH_USING_HW_ATOMIC_8ARCH_USING_HW_ATOMIC_16并为支持相关硬件原子能力的 ARM 架构默认选中这些选项;
在
libcpu/arm/common/atomic_arm.c中新增 ARM 平台 8 位和 16 位硬件原子支持,包括:LDREXB/STREXBLDREXH/STREXHload/store/and/or实现;保持现有 32 位原子操作接口和行为不变,仅扩展更小宽度类型的支持能力。
通过这些调整,RT-Thread 原子操作框架可以统一支持 8 位、16 位和原有宽度的原子访问与按位操作,同时兼容标准原子、硬件原子和软件回退三种实现路径。
请提供验证的bsp和config (provide the config and bsp)
BSP:
bsp/stm32/stm32l496-st-nucleo.config:
RT_USING_HW_ATOMICaction: