Skip to content
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

Update function rt_thread_suspend in src/thread.c. #8856

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

Conversation

pillarshi
Copy link

@pillarshi pillarshi commented Apr 24, 2024

Make it easier and safer for users to suspend the current thread.

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

[

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

用户在不是很了解的情况下挂起自身线程,可能不会调用rt_schedule
更新 src/thread.c 中的rt_thread_suspend函数,使挂起当前线程更方便、更安全。

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

在一般的使用中挂起自己常用rt_thread_suspend函数,该函数内部对于RT_NULL并未使用而时作为错误进行断言。
因此借鉴FreeRTOS,在传入到内部实现之前,先判断参数是否为RT_NULL,并替换为当前线程,并在之后调用rt_schedule
同时添加rt_thread_suspend函数自身注释,以便使用。

请提供验证的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

@CLAassistant
Copy link

CLAassistant commented Apr 24, 2024

CLA assistant check
All committers have signed the CLA.

@mysterywolf mysterywolf reopened this Apr 24, 2024
@BernardXiong
Copy link
Member

看下上一个函数的说明:

/**
 * @brief   This function will suspend the specified thread and change it to suspend state.
 *
 * @note    This function ONLY can suspend current thread itself.
 *              rt_thread_suspend(rt_thread_self());
 *
 *          Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a
 *          thread is executing when you suspend it. If you suspend a thread while sharing a resouce with
 *          other threads and occupying this resouce, starvation can occur very easily.
 *
 * @param   thread the thread to be suspended.
 * @param   suspend_flag status flag of the thread to be suspended.
 *
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
 */
rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
{
    return rt_thread_suspend_to_list(thread, RT_NULL, 0, suspend_flag);
}

@pillarshi
Copy link
Author

感谢熊老大的指点,我刚开始学习RTT和阅读源码,会有许多不足望多多指点。

确实原有的方案已经解决了挂起自身的问题,但使用rt_thread_suspend(rt_thread_self());后还要调用rt_schedule();才能完全挂起,这似乎并不方便合理,是不是应该有更好的解决方案? 比如:在封装一个函数,让用户可以更加方便安全的挂起自身线程,或者类似我的更改。请说说您的看法思路?

@BernardXiong
Copy link
Member

感谢熊老大的指点,我刚开始学习RTT和阅读源码,会有许多不足望多多指点。

确实原有的方案已经解决了挂起自身的问题,但使用rt_thread_suspend(rt_thread_self());后还要调用rt_schedule();才能完全挂起,这似乎并不方便合理,是不是应该有更好的解决方案? 比如:在封装一个函数,让用户可以更加方便安全的挂起自身线程,或者类似我的更改。请说说您的看法思路?

但是之前的方式都是自行在外面调用,所以如果突然改成内部调用,那么就会出现相关所有代码都需要仔细查看一遍,同时做好相关的测试工作。

@pillarshi
Copy link
Author

但是之前的方式都是自行在外面调用,所以如果突然改成内部调用,那么就会出现相关所有代码都需要仔细查看一遍,同时做好相关的测试工作。

这确实比较麻烦,我现在有两个想法:

  1. 可以参考我的这一次PR,对之前的相关代码仍然支持,但在使用 rt_thread_suspend 时,支持传入参数为 RT_NULL 表示挂起当前线程,并开启调度。
  2. 就比较简单粗暴,额外添加一个函数来实现挂起并调度。

以上方法各有优缺点,不知道您怎么看?是否有更好的方法?

Make it easier and safer for users to suspend the current thread.
@BernardXiong BernardXiong added the discussion This PR/issue needs to be discussed later label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion This PR/issue needs to be discussed later
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants