找回密码
 注册
关于网站域名变更的通知
查看: 213|回复: 3
打印 上一主题 下一主题

请问调用rt_thread_create()创建线程,初始化定时器有何用处

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2022-7-13 13:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
调用rt_thread_create()创建线程的时候,调用了下面的代码,作用是什么?
* w% |$ J; h. K& u
" h* y; O+ n# u8 A/* initialize thread timer */
, ^; s' I7 n. N+ Wrt_timer_init(&(thread->thread_timer),thread->name,rt_thread_timeout,thread,0,RT_TIMER_FLAG_ONE_SHOT);2 ]8 P7 q& J% l3 w
为什么要创建这个定时器?5 f! E$ \; t1 W0 b, i! y
- W' T6 d. N& U
在系统时钟中断回调函数里面,已经调用了下面的代码
9 g; {  L# K/ a" ]8 P! F& m% ?( q( |# \: ~6 |' `+ L% i- s
void rt_tick_increase(void)
9 y& K  X7 `6 X$ _) P; R8 p4 U{
" k  z  i; V. [& a9 j7 E' estruct rt_thread thread;
' h$ e6 q/ w- c9 s8 S/ increase the global tick /
1 n% i- |/ U4 _- ]7 B8 u  G++ rt_tick;
0 t1 X( E- u! t5 a. x/ check time slice /3 r5 L) |3 u3 t7 G/ l; [9 F3 x# H
thread = rt_thread_self();! l/ {) C3 l5 E0 p* S. T  V
-- thread->remaining_tick;
6 X8 B& I( I/ z/ l7 `$ `if (thread->remaining_tick == 0)
- ]2 l/ P  O6 n$ e. b$ t& H6 p, L( c{+ D0 Q/ s' v( O2 d1 R/ T
/ change to initialized tick /5 r, [/ N0 n* k
thread->remaining_tick = thread->init_tick;
. k6 b* M% s/ T# t. f  `/ yield /) w! F/ Z/ a  [6 G
rt_thread_yield();
5 L% X- {  a2 H- u( N, v6 `}
6 t2 L. \5 R: x* f$ P8 B4 t/ check timer */8 B+ g7 t" P- d; [/ C
rt_timer_check();
7 b& c, h- S- \9 _/ m' M: l}
1 _! L( K# F- j% `' y上面的代码已经会去判断remaining_tick,这个为0就执行让出cpu操作了。
6 D3 i: N- P+ m6 s& h' k9 ~

该用户从未签到

2#
发表于 2022-7-13 13:51 | 只看该作者
用来控制挂起时间的。例如你主动调用delay或者阻塞于获取信号量等都会用到这个定时器。截取了一段信号量获取函数中的源码:% y: ~! a; Z) v* \9 t8 @
`
% F/ g5 r# A, M( q( ^; x/ start /
4 K* D# n) l4 z  c/ Q- A! lrt_ipc_list_suspend(&(sem->parent.suspend_thread)," o% v' W2 U1 n
thread,
9 K" |* F. g, o) U3 R4 r% usem->parent.parent.flag);: c% m% L! s0 |3 I

5 e! C! h! W% _1 z2 E- P/* has waiting time, start thread timer */
2 t" u! k3 Z6 Vif (time > 0)' l* E. k% f4 _$ {+ F& l
{+ r( B1 r1 C, i4 r2 J& Q/ P
RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",2 o/ c1 x  v# L
thread->name));
' K# X7 f1 j; o7 c. ]9 O/* reset the timeout of thread timer and start it */. @9 i$ |8 i8 w/ v: y, w1 G( B( J
rt_timer_control(&(thread->thread_timer),
1 K# c* L" j1 ~  K% X) G9 k" j+ FRT_TIMER_CTRL_SET_TIME,) f% \6 m0 f$ v' O5 J
&time);
$ N1 @; d/ ]' x2 A7 ?4 d; hrt_timer_start(&(thread->thread_timer));
* K/ K2 ]$ k( ~}/ ?% M* s0 X/ v# S- R8 N
/* enable interrupt */
1 z& u% {8 `6 art_hw_interrupt_enable(temp);1 A% m0 k) ^, \* Z
/* do schedule */
! A; {5 X' R+ {+ ]' l% Grt_schedule(); rt_ipc_list_suspend(&(sem->parent.suspend_thread),- d( @* j1 h7 p8 P# ?- d2 l
thread,! K) ?) C8 S6 f: ]$ Z  W' ~2 {
sem->parent.parent.flag);' [3 t. @* M2 i
/* has waiting time, start thread timer */
+ B) Q$ B7 M4 s2 F2 yif (time > 0)
) X7 {* t+ P; i4 i+ u+ h2 R, D{
! Y0 M: q+ Y5 S% S  y) f# IRT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",# M3 L7 T) c2 b% b' B
thread->name));
& N! T6 V7 B/ E0 r/* reset the timeout of thread timer and start it */
: y# @. N3 M& b( lrt_timer_control(&(thread->thread_timer),
' U. X6 B. G7 ?; VRT_TIMER_CTRL_SET_TIME,6 ?5 V: F( {  y( K$ i; T
&time);
$ n& l1 \) D1 T4 m/ k& ~/ H' rrt_timer_start(&(thread->thread_timer));8 w# s) {+ i4 e. G
}& p3 w: f  s$ N/ {( t  y( u  w
/* enable interrupt */
2 B' K7 h" e6 i, M1 Trt_hw_interrupt_enable(temp);! _* @$ u/ r4 K2 C3 f9 N' b
/* do schedule */+ {. O  T5 p7 A; z/ C7 Q; o, U- P
rt_schedule();`
  j( q1 j) B# c( `
  • TA的每日心情
    开心
    2023-6-2 15:15
  • 签到天数: 1 天

    [LV.1]初来乍到

    3#
    发表于 2022-7-13 14:50 | 只看该作者
    楼上说的很不错的0 B& q3 d% U2 n4 p6 u6 e+ K

    该用户从未签到

    4#
    发表于 2022-7-13 15:23 | 只看该作者
    可以试试一楼的方法
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-11-23 19:43 , Processed in 0.156250 second(s), 23 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表