Skip to content

smp: ensure safe thread termination in rt_thread_close #10549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

eatvector
Copy link
Contributor

@eatvector eatvector commented Jul 26, 2025

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

确保smp环境下 rt_thread_close 能真正关闭并停止目标线程的运行,可参考问题: #10554
[

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

在SMP环境下,当一个运行在一个核心上的线程通过rt_thread_close和rt_thread_detach间接调用rt_thread_close试图关闭另一个核心上运行的线程时可能会存在潜在问题。具体而言,rt_thread_close的操作逻辑仅是将目标线程的控制块从调度队列中移除,并将其状态标记为关闭,但这一操作并不能立即终止目标线程的实际执行。

由于目标线程可能仍在另一个核心上运行,它无法实时感知到自身的关闭状态,只有下次触发调度时才会真正脱离CPU。这种延迟会导致关键问题:在rt_thread_close调用完成后,目标线程可能仍在执行代码,从而引发并发访问冲突或资源竞争等问题。

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

本pr的修改只会影响smp环境下的两条调用链:

rt_thread_delete -> _thread_detach -> rt_thread_close
rt_thread_detach -> _thread_detach -> rt_thread_close

不会影响线程通过 _thread_exit -> _thread_detach -> rt_thread_close 关闭自己的调用链

在rt_thread_close的实现中,当目标线程被标记为关闭状态后,如果其运行的核心与当前核心不同,系统会向其所在核心发送核间中断,以尽快触发该核心的重新调度,从而确保目标线程能够及时脱离 CPU 并真正停止执行。

而在 rt_thread_delete和 rt_thread_detach中,由于当前核心的调度已被 rt_enter_critical 禁用,因此采用轮询机制检测目标线程是否已完全脱离 CPU。为了应对目标核心始终无法调度的极端情况,设置了超时机制。该超时时间应该与硬件平台性能和系统负载相关,目前暂采RT_SMP_THREAD_DETACH_TIMEOUT宏进行配置。

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

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 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/workflows/bsp_buildings.yml 详细请参考链接BSP自查

@github-actions github-actions bot added the Kernel PR has src relate code label Jul 26, 2025
Copy link

github-actions bot commented Jul 26, 2025

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: GorrayLi ReviewSun hamburger-os lianux-mm wdfk-prog xu18838022837

Changed Files (Click to expand)
  • src/Kconfig
  • src/thread.c

📊 Current Review Status (Last Updated: 2025-08-05 10:20 CST)

  • GorrayLi Pending Review
  • ReviewSun Pending Review
  • hamburger-os Pending Review
  • lianux-mm Pending Review
  • wdfk-prog Pending Review
  • xu18838022837 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.

@eatvector eatvector marked this pull request as draft July 26, 2025 14:17
@eatvector eatvector force-pushed the fix/rt-thread-close-smp branch 22 times, most recently from ddebfe5 to da7cfa3 Compare July 27, 2025 15:24
@eatvector eatvector marked this pull request as ready for review July 28, 2025 03:56
@BernardXiong
Copy link
Member

detach/delete操作应该很少使用的,而且也禁止做跨核操作啊。

@eatvector eatvector force-pushed the fix/rt-thread-close-smp branch from da7cfa3 to 5e7598b Compare July 30, 2025 03:52
@eatvector
Copy link
Contributor Author

eatvector commented Jul 30, 2025

detach/delete操作应该很少使用的,而且也禁止做跨核操作啊。

鸥鸥,主要detach里没有相关的检测逻辑或者注释说明应该禁止跨核detach😂

@eatvector eatvector closed this Jul 30, 2025
@eatvector eatvector reopened this Aug 5, 2025
@eatvector eatvector closed this Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Kernel PR has src relate code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants