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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
调用rt_thread_create()创建线程的时候,调用了下面的代码,作用是什么?) C) }# i) z. P6 i; f7 ]
) I3 M( ]6 V$ ^* P( V4 P
/* initialize thread timer */# h3 G6 _0 g6 W5 t* H
rt_timer_init(&(thread->thread_timer),thread->name,rt_thread_timeout,thread,0,RT_TIMER_FLAG_ONE_SHOT);; K! p2 V- U9 k0 J# _/ M4 L
为什么要创建这个定时器?+ @2 q5 q5 W8 _, K; R; g
( R* L  o. c5 N1 w
在系统时钟中断回调函数里面,已经调用了下面的代码
( g( ^' w& G! o+ W/ }9 a
) H8 s2 J! E$ m8 M/ |: Q& Y, f0 `void rt_tick_increase(void)
& T' T/ Q4 S% e# ]{
0 w$ Y% y4 t- ^, K% S3 Sstruct rt_thread thread;
; X$ |! _$ w9 y# d4 f: Y/ increase the global tick /
. K0 l- w$ D: K, L++ rt_tick;' a% R, D7 C7 j, @! H
/ check time slice /! b# Y8 t7 s) N
thread = rt_thread_self();
: F6 a. g7 d2 o+ s-- thread->remaining_tick;
6 r. t4 Q; B0 @! r3 q) Zif (thread->remaining_tick == 0)2 u3 U; L  Z* C0 e
{1 W3 r3 y* n9 l) n3 e
/ change to initialized tick /
& h" |( R7 w( `) e1 hthread->remaining_tick = thread->init_tick;4 v+ W2 U9 |6 _2 Z% j- A0 v8 A
/ yield /$ P/ E& O5 V/ g2 _: J& |
rt_thread_yield();
" `7 h- V  e+ _  J0 n) n; V}
' O# P" B4 V0 u6 I& h* ~/ check timer */
9 S2 B5 y$ I& ?; ort_timer_check();
1 J. p3 Z) R8 Q$ \}0 Z7 q" z, h" g9 X+ ?9 w
上面的代码已经会去判断remaining_tick,这个为0就执行让出cpu操作了。3 b/ H# I) t, b9 d  D+ m

该用户从未签到

2#
发表于 2022-7-13 13:51 | 只看该作者
用来控制挂起时间的。例如你主动调用delay或者阻塞于获取信号量等都会用到这个定时器。截取了一段信号量获取函数中的源码:0 C  }7 c! [6 ^) ^1 i3 h
`( f) j: B4 X( k' S7 L
/ start /
- a7 _6 \0 e9 f5 x) nrt_ipc_list_suspend(&(sem->parent.suspend_thread),6 y- A- Z3 w) a6 v+ H9 c0 p1 V3 X
thread,
6 ?: C9 h; n/ _9 u: zsem->parent.parent.flag);
! g. H0 o9 U5 ^2 n* t
& @* V2 t0 O" s7 @: P/* has waiting time, start thread timer */! ^5 J/ b, j3 E8 b0 E' J" q
if (time > 0)! z/ Q0 S+ |, g( I9 U6 k  u
{8 a8 R# _  ]* g4 f! ~, ^3 S& S
RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",6 t! C% w( K: c
thread->name));
8 C6 x  {: {( M# |& U/* reset the timeout of thread timer and start it */
/ ?/ V! T4 m/ Nrt_timer_control(&(thread->thread_timer),# x# n- A9 G3 d+ k2 V! T8 ?$ W
RT_TIMER_CTRL_SET_TIME,
' f' U3 V7 n! U& B&time);* O3 s. @: y, C2 D
rt_timer_start(&(thread->thread_timer));# d. ~# h" G+ j6 y5 e/ Q8 @/ y
}9 k" P% v/ f8 B$ d
/* enable interrupt */
# j' ?" w, [/ w9 R& u, \rt_hw_interrupt_enable(temp);& U+ s0 z& H# b# s4 ~! G
/* do schedule */" x$ D: a! r! \& d0 k
rt_schedule(); rt_ipc_list_suspend(&(sem->parent.suspend_thread),
# b5 Y% I; g4 Z4 d- S* M6 k/ ^thread,
5 ], z9 w  X5 c, Vsem->parent.parent.flag);  H1 j8 c1 A9 m9 K$ G: V/ q
/* has waiting time, start thread timer */
9 e# Y) G) N! h' mif (time > 0)
" b6 _4 E6 I( J. m9 n{+ e" G" ]* \6 i  O9 t* z- r' T- d
RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",# V' k$ c( u9 c2 r8 ^- O
thread->name));
6 [" a: @. ?! M1 E3 h/* reset the timeout of thread timer and start it */
1 }6 G* X. L" b/ D! Trt_timer_control(&(thread->thread_timer),
) B4 y5 g* j, n* ~  W: o6 DRT_TIMER_CTRL_SET_TIME,
0 E6 [; V; l6 {8 b&time);
, e6 r* |( |5 N6 @) Brt_timer_start(&(thread->thread_timer));
4 R; {! F2 j- W( q}! |3 a- L; @8 e: l/ f
/* enable interrupt */. T. {, c& }  p3 ]3 v, J
rt_hw_interrupt_enable(temp);
6 [4 b! b5 N$ s, u  C/* do schedule */
" x. x4 T; v% r& D. F( yrt_schedule();`) a, H8 l$ `! [5 U
  • TA的每日心情
    开心
    2023-6-2 15:15
  • 签到天数: 1 天

    [LV.1]初来乍到

    3#
    发表于 2022-7-13 14:50 | 只看该作者
    楼上说的很不错的( [  X4 m# C7 q( w; w9 q

    该用户从未签到

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

    本版积分规则

    关闭

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

    EDA365公众号

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

    GMT+8, 2025-10-11 20:45 , Processed in 0.140625 second(s), 23 queries , Gzip On.

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

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

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