Skip to content

feat(core): Add 8-bit and 16-bit atomic operation support#11244

Open
wdfk-prog wants to merge 1 commit intoRT-Thread:masterfrom
wdfk-prog:atomic
Open

feat(core): Add 8-bit and 16-bit atomic operation support#11244
wdfk-prog wants to merge 1 commit intoRT-Thread:masterfrom
wdfk-prog:atomic

Conversation

@wdfk-prog
Copy link
Contributor

为什么提交这份PR (why to submit this PR)

当前原子操作接口主要面向 rt_atomic_t,缺少对 8 位和 16 位原子类型的统一支持。
这会带来以下问题:

  1. 某些场景只需要对 8 位或 16 位状态变量进行原子读写和按位操作,但当前缺少统一接口;
  2. 不同架构或组件在处理小宽度原子变量时,难以复用统一 API;
  3. 即使部分架构具备 8 位/16 位硬件原子能力,当前框架层也没有对应抽象;
  4. 在没有硬件支持的情况下,也缺少可回退的软件实现。

因此,提交此 PR,为 RT-Thread 原子操作框架补充 8 位和 16 位原子类型及其相关接口,并同时支持硬件实现和软件回退路径。

你的解决方案是什么 (what is your solution)

本 PR 主要做了以下改动:

  1. rttypes.h 中新增原子类型定义:

    • rt_atomic8_t
    • rt_atomic16_t
  2. rtatomic.h 中新增 8 位和 16 位原子操作接口,包括:

    • load/store
    • and/or
  3. 对标准原子实现路径(RT_USING_STDC_ATOMIC)补充对应宏映射,使 8 位和 16 位接口可以直接映射到标准原子操作;

  4. 对硬件原子实现路径(RT_USING_HW_ATOMIC)增加分层支持:

    • 新增 ARCH_USING_HW_ATOMIC_8
    • 新增 ARCH_USING_HW_ATOMIC_16
    • 当架构支持 8 位/16 位硬件原子操作时,使用硬件实现;
    • 当架构不支持时,自动回退到软件原子实现;
  5. 在纯软件原子路径中新增以下实现:

    • rt_soft_atomic_load8
    • rt_soft_atomic_store8
    • rt_soft_atomic_load16
    • rt_soft_atomic_store16
    • rt_soft_atomic_and8
    • rt_soft_atomic_or8
    • rt_soft_atomic_and16
    • rt_soft_atomic_or16

    这些实现通过关中断保护临界区,保证原子性;

  6. libcpu/Kconfig 中新增架构能力配置项:

    • ARCH_USING_HW_ATOMIC_8
    • ARCH_USING_HW_ATOMIC_16

    并为支持相关硬件原子能力的 ARM 架构默认选中这些选项;

  7. libcpu/arm/common/atomic_arm.c 中新增 ARM 平台 8 位和 16 位硬件原子支持,包括:

    • LDREXB/STREXB
    • LDREXH/STREXH
    • 对应的 load/store/and/or 实现;
  8. 保持现有 32 位原子操作接口和行为不变,仅扩展更小宽度类型的支持能力。

通过这些调整,RT-Thread 原子操作框架可以统一支持 8 位、16 位和原有宽度的原子访问与按位操作,同时兼容标准原子、硬件原子和软件回退三种实现路径。

请提供验证的bsp和config (provide the config and bsp)

  • BSP:

    • 请填写已验证的 BSP 路径,例如 bsp/stm32/stm32l496-st-nucleo
  • .config:

    • 与 CPU 架构和原子操作相关的配置项
    • 例如:
      • RT_USING_HW_ATOMIC
      • 对应 ARM 架构配置项
      • 如需验证软件回退路径,也可说明关闭硬件原子支持后的测试配置
  • action:

    • 请填写你自己仓库 PR 分支触发的 CI / Action 链接

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:atomic
  • 设置PR number为 \ Set the PR number to:11244
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 atomic 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the atomic branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions github-actions bot added the libcpu label Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant