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

[kernel][src] reload timeout_tick with absolute base value #5402

Closed
wants to merge 4 commits into from

Conversation

thewon86
Copy link
Contributor

@thewon86 thewon86 commented Dec 20, 2021

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

[

  1. 分离出一个 _timer_start 局部接口,可以指定定时器启动时间。
  2. 梳理了 timer_check 过程,把周期定时器再次启动开始时间设定为 timer->timeout_tick
  3. 删除了 TIMER_IDLE TIMER_BUSY 等相关代码。

修改后的特性:

  1. 软定时器超时回调函数里支持执行长时间操作(大于 systick 小于定时器定时间隔)。
  2. 可以避免因长时间操作引起的定时器 timeout 漂移问题。
  3. 删掉 IDLE BUSY 等部分代码,是因为 timeout 的回调函数里可以对当前线程进行任何操作,不避讳 rt_timer_start rt_timer_stop rt_timer_control ,既不会引入意外问题,也不影响后续工作。
    ]

以下的内容不应该在提交PR时的message修改,修改下述message,PR会被直接关闭。请在提交PR后,浏览器查看PR并对以下检查项逐项check,没问题后逐条在页面上打钩。
The following content must not be changed in the submitted PR message. Otherwise, the PR will be closed immediately. After submitted PR, please use a web browser to visit PR, and check items one by one, and ticked them if no problem.

当前拉取/合并请求的状态 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

{
/* resume timer thread to check soft timer */
rt_thread_resume(&_timer_thread);
need_schedule = RT_TRUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need_schedule 标记是防止 A 定时器回调中 sleep 等操作导致线程挂起。B 定时器启动时,意外将 timer 线程唤醒。导致 A 定时器意外唤醒

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need_schedule 这个变量的作用不是你说那样,跟你说的那个没联系,rt_timer_start 最后有两种情况,一种是需要进行任务调度切换到 timer 线程,一种是不需要。
需要任务调度的流程是: 开中断;调用rt_schedulereturn 返回。
不需要任务调度的流程是: 开中断;return 返回。
之前就是把上述两种流程合并到一起,用 need_schedule 变量控制中间 rt_schedule 是否被执行的开关。这样的用法在多处有 return 返回的情况下使用比较合适,但是这里只在函数最后紧挨着的地方有两个返回点,分开写代码语义比较清晰。
你可能想说的是 _soft_timer_status 这个变量,用来限制“某些”情况下任务调度,关于这个 pr 的详细说明,请移步论坛,那里有更全面的理论分析。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

能否贴一下论坛链接?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_soft_timer_status 变量去掉后,timer 线程被异常唤醒的问题,这个问题是咋解决的?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,这问题是怎么解决的?

@enkiller
Copy link
Contributor

enkiller commented Jan 2, 2022

timer 定时器的周期,指的是回调执行完成,下一次执行需要等待的时间。回调函数执行的时间,并不算在周期内

@thewon86
Copy link
Contributor Author

thewon86 commented Jan 4, 2022

timer 定时器的周期,指的是回调执行完成,下一次执行需要等待的时间。回调函数执行的时间,并不算在周期内

第一次听到这种说法。

@Guozhanxin
Copy link
Member

timer 定时器的周期,指的是回调执行完成,下一次执行需要等待的时间。回调函数执行的时间,并不算在周期内

我理解的定时器的周期,跟回调函数执行时间没关系,表示的是在一个固定周期内调用回调函数

比如:如果在10s的时候启动了一个周期是3s的周期性定时器A,那就是要在时刻,13、16、19、22、25、28... 的时刻去调用回调函数。

@enkiller
Copy link
Contributor

enkiller commented Jan 9, 2022

timer 定时器的周期,指的是回调执行完成,下一次执行需要等待的时间。回调函数执行的时间,并不算在周期内

我理解的定时器的周期,跟回调函数执行时间没关系,表示的是在一个固定周期内调用回调函数

比如:如果在10s的时候启动了一个周期是3s的周期性定时器A,那就是要在时刻,13、16、19、22、25、28... 的时刻去调用回调函数。

不能单纯从周期这个词上理解。定时器的周期,可以理解为自动调用 start 函数。

@Guozhanxin
Copy link
Member

timer 定时器的周期,指的是回调执行完成,下一次执行需要等待的时间。回调函数执行的时间,并不算在周期内

我理解的定时器的周期,跟回调函数执行时间没关系,表示的是在一个固定周期内调用回调函数
比如:如果在10s的时候启动了一个周期是3s的周期性定时器A,那就是要在时刻,13、16、19、22、25、28... 的时刻去调用回调函数。

不能单纯从周期这个词上理解。定时器的周期,可以理解为自动调用 start 函数。

但是,我感觉大家应该都认为,周期就是周期的意思。。。你这种说法,我也是第一次听说

@mysterywolf
Copy link
Member

mysterywolf commented Jan 12, 2022

不必在周期这个概念上过于纠结,实际上一个完备的定时器是有多种不同的周期模式的, @thewon86 出出提到的是一种, @enkiller 炸弹哥提到的也是一种,怎么理解都对。
RT-Thread的定时器没有提供多种周期模式,因此我们只需要规定好我们rtt的timer的周期指的是什么意思就好,不需要纠结这一点。
比如uCOS的定时器周期就可以设置2个还是3种我忘了,相对的绝对的都可以设定。
MATLAB的定时器也可以同时支持炸弹哥和出出说的这种情况。

之前这个定时器到底什么时候开始计时其实在文档中没有明确体现出来,大家也是稀里糊涂的用,这次正好明确一下。其实我们的定时器也不需要像matlab或者ucos一样支持多种,只需要支持某一种就好。

https://blog.csdn.net/weixin_43455581/article/details/109221500

MATLAB中定时器的四种定时模式:
sigleShot:只执行一次,故Period属性不起作用,其他模式都可以执行多次

fixedDelay:上一次TimerFcn执行完毕时刻到下一次TimerFcn被加入队列时刻之间的间隔

fixedRate:上一次开始执行到下一次被加入队列之间的间隔

fixedSpacing:前后两次被加入到执行语句队列时刻之间的间隔

@mysterywolf mysterywolf added the urgent 🏃 urgent label Jan 12, 2022
@thewon86
Copy link
Contributor Author

thewon86 commented Jan 18, 2022

@mysterywolf
sigleShot rtt 是支持的,我修改后的是 fixedSpacing 模式。如果想实现 fixedDelay ,当初我想着是在 timeout 回调函数执行结束返回前应用主动执行一次 rt_timer_start。

fixedRate 模式有点儿意思,它考虑到了到达定时时间和执行回调函数时间之间可能存在时间差。timoeout 回调函数执行开始执行一次 rt_timer_start 也是同样的效果。

这样不限制在 timeout 回调里调用 rt_timer_start ,而且可以由应用决定是否重新计时。

@mysterywolf
Copy link
Member

mysterywolf commented Jan 20, 2022

我修改后的是 fixedSpacing 模式

我觉得OK! 其他的fixedRate fixedDelay可以不用实现,或者以后再实现都可以。我们只需要准确的明确,周期性定时器到底是从什么时候开始算起就好。

fixedSpacing 这种方式要比fixedDelay要好控制周期,因为软件定时器回调函数具体要执行多少时间估计起来很麻烦。

@mysterywolf mysterywolf requested a review from enkiller January 20, 2022 03:58
@mysterywolf mysterywolf added the +1 Agree +1 label Jan 20, 2022
@BernardXiong
Copy link
Member

@mysterywolf 合并你的PR后,这个变成冲突了

src/timer.c Outdated
Comment on lines 402 to 405
/*
* get timeout tick,
* the max timeout tick shall not great than RT_TICK_MAX/2
*/
Copy link
Member

@mysterywolf mysterywolf Jan 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个注释应该删掉 这个是给之前那个assert的注释

src/timer.c Outdated
rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
unsigned int tst_nr;
static unsigned int random_nr;

/* parameter check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);

need_schedule = RT_FALSE;
RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mysterywolf mysterywolf added the in progress PR/issue in progress. label Jan 21, 2022
@mysterywolf mysterywolf added urgent 🏃 urgent and removed in progress PR/issue in progress. urgent 🏃 urgent labels Jan 21, 2022
@BernardXiong
Copy link
Member

除了 @mysterywolf ,其他人是否搭成一致? @Guozhanxin @enkiller ?Please provide your comments, thank you.

@Guozhanxin
Copy link
Member

Guozhanxin commented Jan 24, 2022

除了 @mysterywolf ,其他人是否搭成一致? @Guozhanxin @enkiller ?Please provide your comments, thank you.

我对代码更改没意见,我是期望有更多的测试用例和测试结果给出来。下面是之前的测试用例运行的结果,看着没问题
image

@Guozhanxin Guozhanxin added the +2 Agree +2 label Jan 24, 2022
@enkiller
Copy link
Contributor

这次定时器修改后,需要在文档中定义好定时器的行为。

@mysterywolf
Copy link
Member

这次定时器修改后,需要在文档中定义好定时器的行为。

是的 这个在文档中心可以清楚的明确出来

@mysterywolf mysterywolf added discussion This PR/issue needs to be discussed later and removed urgent 🏃 urgent +1 Agree +1 +2 Agree +2 v4.1.0 labels Jan 30, 2022
@supperthomas supperthomas changed the title reload timeout_tick with absolute base value [kernel][src] reload timeout_tick with absolute base value May 4, 2023
@supperthomas supperthomas added the Kernel PR has src relate code label May 4, 2023
@mysterywolf mysterywolf closed this Dec 5, 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 expired Kernel PR has src relate code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants