EDA365电子论坛网

标题: 请问调用rt_thread_create()创建线程,初始化定时器有何用处 [打印本页]

作者: choose521    时间: 2022-7-13 13:13
标题: 请问调用rt_thread_create()创建线程,初始化定时器有何用处
调用rt_thread_create()创建线程的时候,调用了下面的代码,作用是什么?
1 l' @% r& U( G/ |- W
. W* b1 E6 ~9 o0 F( J3 s, }1 X+ K6 Y9 T/* initialize thread timer */6 n! T# V) G3 b# s
rt_timer_init(&(thread->thread_timer),thread->name,rt_thread_timeout,thread,0,RT_TIMER_FLAG_ONE_SHOT);% ^7 c8 N- z" D( e. Q
为什么要创建这个定时器?  P' v, P0 [, d5 Z- u; v0 \
! J$ U! l) I: X9 C  s
在系统时钟中断回调函数里面,已经调用了下面的代码
: b& n3 k. j) N  V7 Y
8 U3 V$ l$ a2 \2 pvoid rt_tick_increase(void)
' }0 d# Z" @+ @3 N+ F2 p' E{- n& x+ i7 N8 B" e( [- ~9 _8 S' u
struct rt_thread thread;
& _7 b4 ?" f0 N% E8 `, o( s1 A( ?/ increase the global tick /1 k7 g, A; \5 S. i6 P& t' a/ `& S3 W
++ rt_tick;
) u/ W' G$ ]  }+ N8 e+ J& c/ check time slice /+ }4 u& O& @  h9 n8 p/ Y* q# ?
thread = rt_thread_self();0 \4 t6 C% M$ t
-- thread->remaining_tick;$ a! F! Z1 [  v$ @/ a4 u" X& t
if (thread->remaining_tick == 0)( f0 o- B5 [# h! f9 I2 c
{
5 _; V8 Q% g6 \1 B/ change to initialized tick /$ Z: L' _8 R3 F) z! p
thread->remaining_tick = thread->init_tick;
, w. e, R  j) ?/ yield /
$ I0 J! [" V: yrt_thread_yield();4 _: u0 b5 D$ T
}
8 U* e: s0 J7 G4 D/ _! q/ check timer */
. j+ C- Z( ^0 A! L% |' ]rt_timer_check();5 _- R  z; I7 [) g; w$ r2 n" _8 ~
}! N* u2 y% f0 U: B, V4 [
上面的代码已经会去判断remaining_tick,这个为0就执行让出cpu操作了。5 l- v7 A7 ]0 H/ O

作者: opipo    时间: 2022-7-13 13:51
用来控制挂起时间的。例如你主动调用delay或者阻塞于获取信号量等都会用到这个定时器。截取了一段信号量获取函数中的源码:
' I* X! ]2 C, D* j, [`; j$ _  K, Y" i7 F
/ start /
) _8 r( q7 D& F, vrt_ipc_list_suspend(&(sem->parent.suspend_thread),3 s# ^  c/ f7 l- t6 w
thread,. B$ t6 M+ v% e. C9 e( e) f* W
sem->parent.parent.flag);
" T: Y6 W1 f/ d  o; z7 {& O! Q/ G3 b6 O& g$ R" ~
/* has waiting time, start thread timer */
& }4 M" X, b  s7 t9 W, A* S' ]if (time > 0)
& z- W+ A6 N8 K+ Z{# y4 u* o' L7 S6 H4 H! X
RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",) ~/ y$ r. V( P5 P/ Y% }6 P8 p
thread->name));& W. e. v$ M$ a% X1 V
/* reset the timeout of thread timer and start it */+ }, H9 j3 t' h
rt_timer_control(&(thread->thread_timer),
# p2 _6 _- h/ _, C1 LRT_TIMER_CTRL_SET_TIME,0 X: J" i( }( f0 W
&time);9 {3 `4 a; A7 Q5 e& @8 D$ L
rt_timer_start(&(thread->thread_timer));
5 \; _$ F7 C' t& Y2 w* f}' o8 z: c1 ?' q$ c# I* s. M
/* enable interrupt */' G: v. T! D: P
rt_hw_interrupt_enable(temp);
8 Y/ k: k% w- p, y( e8 v# ^9 S/* do schedule */7 R8 p% G4 J" B( q, T# h% s
rt_schedule(); rt_ipc_list_suspend(&(sem->parent.suspend_thread),
* m- S# f" O2 f" K: lthread,! z) a4 L$ l7 h2 j* {9 h
sem->parent.parent.flag);
0 l# U# U4 h: `) E4 j' g/* has waiting time, start thread timer */+ z  }1 \4 o! B2 M0 @  k
if (time > 0)
# r8 _6 }, I' s/ {! X{/ w3 S- w. I9 e2 J$ j& g) j
RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",4 e" r4 d! Y! v. T* T
thread->name));; s( l7 Y: Q, }* [2 [  O- n
/* reset the timeout of thread timer and start it */
* f- R% V# j: ~rt_timer_control(&(thread->thread_timer),
% X2 r+ w/ j1 |8 q% U, `RT_TIMER_CTRL_SET_TIME,
" N( v/ ~0 _3 d' G+ p) B2 [&time);
/ a8 g% f$ p1 _9 g) x3 Y: @rt_timer_start(&(thread->thread_timer));8 n+ \! T) ~4 _. M1 }
}
3 T% k3 R! |8 V1 T* M/* enable interrupt */
+ C, v2 F1 k' i/ N7 U0 Z1 urt_hw_interrupt_enable(temp);2 a* z$ x' Y* o5 i
/* do schedule */
" d# M; y/ m5 }/ J1 x9 K2 K, \rt_schedule();`0 `( R) ?9 G* X' U- N7 q* S: c

作者: Blah    时间: 2022-7-13 14:50
楼上说的很不错的* @) Y( g9 Q0 G3 S! h

作者: land    时间: 2022-7-13 15:23
可以试试一楼的方法




欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/) Powered by Discuz! X3.2