Skip to content

Fix missing schedule when thread actively suspends itself #10395

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mouch6131
Copy link
Contributor

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

[

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

线程在主动挂起自己的时候,只是把自己加到suspend list,而没有发起调度,这就导致当前线程还会继续执行,直到tick或者其他线程发起调度

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

在rt_thread_suspend_to_list函数中判断是否属于主动挂起自己,在这种情况下主动发起调度

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

在rk3588板子上验证成功

]

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

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

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • [1 ] 本拉取/合并请求是一个成熟版本 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:

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

@CLAassistant
Copy link

CLAassistant commented Jun 14, 2025

CLA assistant check
All committers have signed the CLA.

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

📌 Code Review Assignment

🏷️ Tag: kernel

Path: src
Reviewers: @GorrayLi @lianux-mm @wdfk-prog

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

📊 Current Review Status (Last Updated: 2025-06-14 09:07 UTC)


📝 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.

@wdfk-prog
Copy link
Contributor

  • 请问是使用了什么函数,才会有线程在主动挂起自己的时候,只是把自己加到suspend list,而没有发起调度的情况呢?
  • 我发现ipc的所有函数,在执行rt_thread_suspend_to_list之后都有rt_schedule的执行
  • 例如

    rt-thread/src/ipc.c

    Lines 1406 to 1449 in 169d84d

    ret = rt_thread_suspend_to_list(thread, &(mutex->parent.suspend_thread),
    mutex->parent.parent.flag, suspend_flag);
    if (ret != RT_EOK)
    {
    rt_spin_unlock(&(mutex->spinlock));
    return ret;
    }
    /* set pending object in thread to this mutex */
    thread->pending_object = &(mutex->parent.parent);
    rt_sched_lock(&slvl);
    priority = rt_sched_thread_get_curr_prio(thread);
    /* update the priority level of mutex */
    if (priority < mutex->priority)
    {
    mutex->priority = priority;
    if (mutex->priority < rt_sched_thread_get_curr_prio(mutex->owner))
    {
    _thread_update_priority(mutex->owner, priority, RT_UNINTERRUPTIBLE); /* TODO */
    }
    }
    rt_sched_unlock(slvl);
    /* has waiting time, start thread timer */
    if (timeout > 0)
    {
    LOG_D("mutex_take: start the timer of thread:%s",
    thread->parent.name);
    /* reset the timeout of thread timer and start it */
    rt_timer_control(&(thread->thread_timer),
    RT_TIMER_CTRL_SET_TIME,
    &timeout);
    rt_timer_start(&(thread->thread_timer));
    }
    rt_spin_unlock(&(mutex->spinlock));
    /* do schedule */
    rt_schedule();

@BernardXiong
Copy link
Member

这部分在文档中明确有提及到,如果是suspend自己的操作需要自己后续主动进行一次调度。

@mouch6131
Copy link
Contributor Author

请问是使用了什么函数,才会有线程在主动挂起自己的时候,只是把自己加到suspend list,而没有发起调度的情况呢?

  • 就是线程自己主动调用rt_thread_suspend(rt_thread_self());,然后等其他线程去resume它。

这部分在文档中明确有提及到,如果是suspend自己的操作需要自己后续主动进行一次调度

  • 那不做成自动的原因是什么,是这个当前线程的判断不可靠,比如在中断上下文就需要额外考虑吗?或者是这个函数用的地方太多,那放到更上层的rt_thread_suspend如何。我翻了一下文档,确实是有明确说明,但是很多工程师开发的时候其实并不会认真从头到尾看一遍文档,特别是那些有其他平台经验的工程师更习惯直接上手,比如freertos我看它的处理是有判断当前线程,然后自动调用schedule的。

@BernardXiong
Copy link
Member

请问是使用了什么函数,才会有线程在主动挂起自己的时候,只是把自己加到suspend list,而没有发起调度的情况呢?

  • 就是线程自己主动调用rt_thread_suspend(rt_thread_self());,然后等其他线程去resume它。

这部分在文档中明确有提及到,如果是suspend自己的操作需要自己后续主动进行一次调度

  • 那不做成自动的原因是什么,是这个当前线程的判断不可靠,比如在中断上下文就需要额外考虑吗?或者是这个函数用的地方太多,那放到更上层的rt_thread_suspend如何。我翻了一下文档,确实是有明确说明,但是很多工程师开发的时候其实并不会认真从头到尾看一遍文档,特别是那些有其他平台经验的工程师更习惯直接上手,比如freertos我看它的处理是有判断当前线程,然后自动调用schedule的。

希望suspend接口简单一些,把是否进行调度放到函数外自行来处理,而不是一刀切的方式进行调度。如果确实需要考虑,那么应该是提供两份API,一个是只suspend,一个是suspend后进行一次调度。

@mysterywolf mysterywolf marked this pull request as draft June 16, 2025 15:14
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.

4 participants