Skip to content

[components][ipc][rb] add utests and fix stability corner cases#11261

Open
Ryan-CW-Code wants to merge 2 commits intoRT-Thread:masterfrom
Ryan-CW-Code:ringbuf
Open

[components][ipc][rb] add utests and fix stability corner cases#11261
Ryan-CW-Code wants to merge 2 commits intoRT-Thread:masterfrom
Ryan-CW-Code:ringbuf

Conversation

@Ryan-CW-Code
Copy link
Contributor

拉取/合并请求描述:(PR description)

本PR核心内容由 Codex 完成,我仅引导和审查。

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

在别的平台使用rt的ringbuf发现rt_ringbuffer_put_force实现有问题具体表现为
当发生覆盖写入时,语义要求“丢弃最旧数据并写入新数据,最终应当是满状态”。旧实现存在一种 wrapped 场景,会导致“满被误判为空”。
可复现的具体场景(旧实现会错):

  • buffer_size = 8
  • wrapped 状态:
    read_index = 5
    write_index = 2
    read_mirror = 0
    write_mirror = 0
  • 数据长度 = 8 - (5 - 2) = 5,空间 = 3
  • 执行 put_force(length = 5)

旧逻辑执行:

  1. drop_length = 5 - 3 = 2
  2. buffer_size - read_index = 3,满足 3 > drop_length:
    read_index = 5 + 2 = 7
    read_mirror 不变
  3. 写入时 buffer_size - write_index = 6,走不跨界路径:
    write_index = 2 + 5 = 7
    write_mirror 不变
  4. 结果:
    read_index == write_index == 7
    read_mirror == write_mirror == 0
    上述状态会被识别为“空”,但实际已经覆盖写入并保持满,这破坏了“索引相等时镜像位必须区分空/满”的不变量。
    ———

2. 修复思路(理论依据)

新实现保证以下两个不变量始终成立:

  1. 准确推进读指针
    根据 drop_length 分三种情况推进 read_index:
    • drop_length < buffer_size - read_index:仅前移读指针
    • drop_length == buffer_size - read_index:读指针到尾部
    • drop_length > buffer_size - read_index:跨界并翻转 read_mirror
  2. 覆盖写入后保证“满状态不被误判为空”
    如果发生覆盖写入并且最终满足:
    write_index == read_index
    write_mirror == read_mirror
    则强制翻转 write_mirror,使其满足:
    write_index == read_index
    write_mirror != read_mirror
    这样覆盖写入后必被判定为“满”。
    ———

2. 修复思路(理论依据)

新实现保证以下两个不变量始终成立:

  1. 准确推进读指针
    根据 drop_length 分三种情况推进 read_index:
    • drop_length < buffer_size - read_index:仅前移读指针
    • drop_length == buffer_size - read_index:读指针到尾部
    • drop_length > buffer_size - read_index:跨界并翻转 read_mirror
  2. 覆盖写入后保证“满状态不被误判为空”
    如果发生覆盖写入并且最终满足:
    write_index == read_index
    write_mirror == read_mirror
    则强制翻转 write_mirror,使其满足:
    write_index == read_index
    write_mirror != read_mirror
    这样覆盖写入后必被判定为“满”。
    ———

优化ringbuf内部断言、添加ringbuf utest

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

使用RT-Thread的qemu-vexpress-a9验证

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@Ryan-CW-Code Ryan-CW-Code requested a review from Rbb666 as a code owner March 17, 2026 04:48
@github-actions
Copy link

👋 感谢您对 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:ringbuf
  • 设置PR number为 \ Set the PR number to:11261
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 ringbuf 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the ringbuf 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
Copy link

github-actions bot commented Mar 17, 2026

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • components/drivers/ipc/ringbuffer.c
  • components/drivers/ipc/utest/Kconfig
  • components/drivers/ipc/utest/SConscript
  • components/drivers/ipc/utest/ringbuffer_tc.c

📊 Current Review Status (Last Updated: 2026-03-18 09:27 CST)

  • Maihuanyi Pending Review

📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ Ryan-CW-Code
❌ github-actions[bot]
You have signed the CLA already but the status is still pending? Let us recheck it.

@Rbb666 Rbb666 closed this Mar 18, 2026
@Rbb666 Rbb666 reopened this Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants