rt-thread icon indicating copy to clipboard operation
rt-thread copied to clipboard

Update function rt_thread_suspend in src/thread.c.

Open ppppppsssss opened this issue 1 year ago • 5 comments

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:

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

ppppppsssss avatar Apr 24 '24 18:04 ppppppsssss

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Apr 24 '24 18:04 CLAassistant

看下上一个函数的说明:

/**
 * @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);
}

BernardXiong avatar Apr 25 '24 10:04 BernardXiong

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

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

ppppppsssss avatar Apr 26 '24 08:04 ppppppsssss

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

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

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

BernardXiong avatar Apr 26 '24 14:04 BernardXiong

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

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

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

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

ppppppsssss avatar May 10 '24 07:05 ppppppsssss