rt-thread的定时器管理源码分析(二)
function will check timer list, if a timeout event happens, the
* corresponding timeout function will be invoked.
*/
void rt_soft_timer_check(void)
{
rt_tick_t current_tick;
rt_list_t *n;
struct rt_timer *t;
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
current_tick = rt_tick_get();//得到当前时间点
for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list);)//得到下一定时器节点
{
t = rt_list_entry(n, struct rt_timer, list);//t指向rt_timer定时器
/*
* It supposes that the new tick shall less than the half duration of
* tick max.
*/
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)//如果当前的时间点超过定时器的超时时间点
{
RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));//使用钩子函数
/* move node to the next */
n = n->next;//指向下一定时器
/* remove timer from timer list firstly */
rt_list_remove(&(t->list));//移除当前定时器
/* call timeout function */
t->timeout_func(t->parameter);//产生定时器超时事件,调用对应处理函数
/* re-get tick */
current_tick = rt_tick_get();//再次获取当前时间点
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&//如果当前定时器是周期性定时器,则将其再次按序放入软件定时器链表
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
{
/* start it */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;//置标志为非激活状态
rt_timer_start(t);//再次将定时器t放入软件定时器链表末尾
}
else
{
/* stop timer */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;//置标志为非激活状态
}
}
else break; /* not check anymore */
}
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
}
其上rt_timer_start函数如下定义:
[cpp]
/**
* This function will start the timer
*
* @param timer the timer to be started
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_timer_start(rt_timer_t timer)
{
struct rt_timer *t;
register rt_base_t level;
rt_list_t *n, *timer_list;
/* timer check */
RT_ASSERT(timer != RT_NULL);
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)//如果传入的定时器已经激活,则直接返回错误
return -RT_ERROR;
RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));//使用钩子函数
/*
* get timeout tick,
* the max timeout tick shall not great than RT_TICK_MAX/2
*/
RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
timer->timeout_tick = rt_tick_get() + timer->init_tick;//得到定时器超时的时间点
/* disable interrupt */
level = rt_hw_interrupt_disable();//关中断
#ifdef RT_USING_TIMER_SOFT//如果采用的是软件定时器管理模式,则将定时器加入到rt_soft_timer_list中
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
/* insert timer to soft timer list */
timer_list = &rt_soft_timer_list;
}
else
#endif
{
/* insert timer to system timer list */
timer_list = &rt_timer_list;
}
for (n = timer_list->next; n != timer_list; n = n->next)//将定时器按序加入到定时器链表中
{
t = rt_list_entry(n, struct rt_timer, list);
/*
* It supp