[mutex] 跨线程 rt_thread_delete 被删线程持有的mutex也一并释放#11620
Conversation
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: kernelReviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-07-15 12:43 CST)
📝 Review Instructions
|
|
|
@meng-plus 可以帮忙补充一份测试用例到utest里面? |
- 增加单元测试 Clarified mutex release conditions and added exception handling for closed owner threads.
|
查了下 FreeRTOS 和 Zephyr 源码,vTaskDelete() 和 k_thread_abort() 都不会清理线程持有的 mutex。FreeRTOS 的 prvDeleteTCB() 直接 vPortFree(pxTCB),mutex 中的 xMutexHolder 指针就变成野指针了。Zephyr 同理。 当前这个 PR 并非独创的释放持有的锁,结合源码可知,之前的设计意图也是释放mutex,只是设计后未经验证,不符合其设计预期 |
Clarified mutex release conditions and added exception handling for closed owner threads.
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
#11619
你的解决方案是什么 (what is your solution)
结合_thread_detach 清理流程可以看到先执行的 error = rt_thread_close(thread); 将 线程标记为RT_THREAD_CLOSE状态
再进入_thread_detach_from_mutex 清理所有mutex, 同时看到
mutex->hold = 1;可知此处意图也是释放mutex但是rt_mutex_release 处只进行了所有者的判定,未覆盖更多场景,如线程被外部杀死,线程锁死无法恢复等情况,
因此在释放处保留所有着判定外,额外增加 线程关闭也允许释放,以满足上述需求
对比工单11619 可看到解决效果

请提供验证的bsp和config (provide the config and bsp)
]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up