Repository navigation
Conversation
rt_sched_insert_thread() inserted every thread without RT_THREAD_STAT_YIELD at the head of the ready list. A thread resumed from an IPC pending list never carries that flag, because the scheduler clears it when switching away from the thread (see scheduler_up.c). Waking several same-priority threads in pending order therefore reversed their execution order, and freshly started threads had the same problem. Only a thread that was preempted while it was still running keeps the head insertion now, which is the case the original code comment described. Every other case (time slice consumed, resumed from an IPC pending list, freshly started) is appended to the tail, so same-priority threads run in the order in which they became ready. The single core path and both multi-core insertion paths are updated consistently. Add utest core.scheduler_ready_order covering the semaphore wakeup path and the startup path. On SMP the worker threads are bound to the CPU running the test case so that the insertion order stays observable. Refs RT-Thread#8459
|
👋 感谢您对 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-10-21 00:06 CST)
📝 Review Instructions
|
|
@Rbb666 这两个 workflow 仍处于 Waiting for approval(首次从 fork 贡献),需要维护者批准才会运行:
本地已在 QEMU vexpress-a9 跑过完整调度组:修复前 6 passed / 1 failed,修复后 7 passed / 0 failed(对照数据在正文表格里)。批准后 CI 应该能印证同一结果。 另外,若对"同优先级线程是否必须保证执行顺序 FIFO"这个语义方向有不同意见,请直接说明,我可以只保留 utest 部分、内核代码不动。 |
|
补充一项此前未覆盖的验证:时间片轮转是否被这次改动影响。 我原先只验证了"同优先级线程的唤醒顺序",跑了既有 7 个调度用例,但没有专门测 tick 驱动的轮转——而这正是 方法QEMU vexpress-a9,3 个同优先级线程(priority 20),时间片均为 5 tick,线程体内是纯忙循环、不调用 结果
结论三个线程各拿到约三分之一 CPU,修复前后差异在噪声范围内(1.05x vs 1.09x),轮转调度未被破坏。 机制上也对得上:时间片用尽时线程带 上面的验证脚本是临时写的,没有提交。如果需要把它固化成 utest(同优先级线程轮转公平性),我可以补。 |
|
补一条旁证:#6645(2022-11 提交,标签
也就是"只有运行中被抢占且时间片未用尽才插队首,其余(时间片用尽 / 挂起恢复 / 新启动)一律队尾",与本 PR 完全一致。两边独立得出相同结论。 顺带说明本 PR 没有采纳它的另一部分改动:那个 patch 还把 详见 #6645 下的分析评论。 |
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
关联 issue #8459(我已认领)。
rt_sched_insert_thread()对任何没有RT_THREAD_STAT_YIELD标志的线程都插到 ready list 队首。而从 IPC 挂起队列恢复的线程不带这个标志——scheduler_up.c在每次切走线程时就把 YIELD 位清掉了——于是同一批被唤醒的同优先级线程,执行顺序被反转:新启动(
rt_thread_startup)的线程同样被插到队首,所以"先启动的先运行"也不成立。QEMU vexpress-a9 实测(RT-Thread 5.4.0,同一 .config、同一 QEMU 参数):
你的解决方案是什么 (what is your solution)
在覆盖
stat之前先把原值存下来,只有"仍在运行、且时间片未用尽被抢占"的线程保留队首插入——这正是原注释所描述的场景;其余情况(时间片用尽、从 IPC 挂起队列恢复、新启动)一律追加到队尾,使同优先级线程按就绪先后执行。单核
scheduler_up.c与多核scheduler_mp.c的两处插入分支做了一致修改。新增 utest
core.scheduler_ready_order,覆盖信号量唤醒路径与启动路径。SMP 下把工作线程绑定到测试线程所在核,否则它们会被空闲核立刻取走,插入顺序无法观测。请提供验证的bsp和config (provide the config and bsp)
CONFIG_RT_USING_UTEST=y,CONFIG_RT_USING_UTESTCASES=y,CONFIG_RT_UTEST_SCHEDULER=y,CONFIG_RT_USING_CI_ACTION=y,CONFIG_UTEST_THR_STACK_SIZE=8192调度组用例全量对照:
core.scheduler_ready_ordercore.scheduler_mutexcore.scheduler_semcore.scheduler_threadcore.scheduler_timed_mtxcore.scheduler_timed_semcore.scheduler_timeout_race关于
scheduler_timed_mtx/scheduler_timed_sem打印的Total failed times: N(in 1000):这是用例内部的计时竞争计数,两次运行分别是 271 / 213 与 1 / 0,用例本身两次都判定 PASSED,属于运行间波动,不是本次改动引入的回归。需要讨论的一点
这个改动会改变 IPC 唤醒之后的调度顺序,属于语义变更。issue #8459 里我提了两种取舍(把
RT_THREAD_STAT_YIELD语义收窄为"仅时间片未用尽被抢占",还是维持现状并只把同优先级 FIFO 的保证范围写进文档),目前还没有维护者结论。所以在正文里把这一点明确写出来,希望能先拿到结论,而不是让这个改动悄悄合进去。如果认为只应保证"获取信号量的顺序"而不保证"执行顺序",可以只合入 utest 部分,并把断言改为记录观测值(或在用例里直接断言当前行为),内核代码不动。
]
当前拉取/合并请求的状态 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