目錄
定時器的實現
定時器的細節
對於一個單片機,定時器通常是最基礎的功能。不過,這裏的定時器是基於硬件的定時器。依靠時鐘晶振,硬件電路來實現的。RT-Thread也有自己的定時器,這個定時器是由以下幾個部分構成:
/** * Thread structure */ struct rt_thread { ....... struct rt_timer thread_timer; /**< built-in thread timer */ ....... } /** *timer structure */ struct rt_timer { struct rt_object parent; /**< inherit from rt_object */ rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL]; void (*timeout_func)(void *parameter); /**< timeout function */ void *parameter; /**< timeout function's parameter */ rt_tick_t init_tick; /**< timer timeout tick */ rt_tick_t timeout_tick; /**< timeout tick */ }; typedef struct rt_timer *rt_timer_t;
內嵌到線程控制塊之中,內嵌到線程之中是爲了實現RT-Thread中線程的一個狀態的實現掛起態(阻塞態),比如說我們要掛起一個線程。我們直接修改線程的狀態位,成功將線程掛起,然後修改remaining_tick,設置需要延時的時間。嗯嗯嗯~~貌似這樣也能行,不過這是之前的版本了。
我梳理了一下定時器的使用:
- 定時器單獨使用
- 定時器嵌入線程中,用於掛起的
static void _rt_timer_init(rt_timer_t timer,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
int i;
/* set flag */
timer->parent.flag = flag;
/* set deactivated */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->timeout_func = timeout;
timer->parameter = parameter;
timer->timeout_tick = 0;
timer->init_tick = time;
/* initialize timer list */
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
{
rt_list_init(&(timer->row[i]));
}
}
就功能而言,rt_time功能複雜度更類似於rt_thread。超時函數類似於線程的入口函數.
定時器定時餘時檢索
/**
* This function will check timer list, if a timeout event happens, the
* corresponding timeout function will be invoked.
*
* @note this function shall be invoked in operating system timer interrupt.
*/
void rt_timer_check(void)
{
struct rt_timer *t;
rt_tick_t current_tick;
register rt_base_t level;
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
current_tick = rt_tick_get();
/* disable interrupt */
level = rt_hw_interrupt_disable();
while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1])) //定時器列表不爲空,就掃描定時器列表
{
t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
/*
* 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_TICK_MAX一定是有原因的,
//設想,如果timeout_tick是大於RT_TICK_MAX / 2
{
RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
/* remove timer from timer list firstly */
_rt_timer_remove(t);
/* 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);
}
else
{
/* stop timer */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
}
}
else
break;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
}
RT-Thread中,設定了一個鏈表,static rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL],這個鏈表存在的意義就是:縮減了查詢那麼多定時器所花費的時間。有了這一個鏈表,那麼只需要查詢一次便可以得知有沒有已經到達了時間的定時器。
原因是:這個鏈表是有順序的,越靠近初始位的定時時間越短。
/**
* 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)
{
unsigned int row_lvl;
rt_list_t *timer_list;
register rt_base_t level;
rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
unsigned int tst_nr;
static unsigned int random_nr;
/* timer check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
/* stop timer firstly */
level = rt_hw_interrupt_disable();
/* remove timer from list */
_rt_timer_remove(timer);
/* change status of timer */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
rt_hw_interrupt_enable(level);
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
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;
}
//獲取系統定時器列表第一條鏈表根節點地址
row_head[0] = &timer_list[0];
//這裏的RT_TIMER_SKIP_LIST_LEVEL的大小定義並不在rtconfig.h文件中,寫就是不是常用來修改的,且常爲1,只執行一遍for循環這裏
for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
{
//第一次進入rt_timer_start函數時,row_head[row_lvl] == timer_list[row_lvl].prev;也就是第一次不執行這個函數
//加入是第二次進入,那麼,由於不滿足函數體的條件會依次從第一項向下搜索
for (; row_head[row_lvl] != timer_list[row_lvl].prev;
row_head[row_lvl] = row_head[row_lvl]->next)
{
struct rt_timer *t;
rt_list_t *p = row_head[row_lvl]->next;
/* fix up the entry pointer */
t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
/* If we have two timers that timeout at the same time, it's
* preferred that the timer inserted early get called early.
* So insert the new timer to the end the the some-timeout timer
* list.
*/
if ((t->timeout_tick - timer->timeout_tick) == 0)
{
continue;
}
else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
{
break;
}
}
////等等我再想想這點吧
if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
row_head[row_lvl + 1] = row_head[row_lvl] + 1;
}
/* Interestingly, this super simple timer insert counter works very very
* well on distributing the list height uniformly. By means of "very very
* well", I mean it beats the randomness of timer->timeout_tick very easily
* (actually, the timeout_tick is not random and easy to be attacked). */
//這裏是爲了計算添加了幾個定時器
random_nr++;
tst_nr = random_nr;
//剛纔找到了插入的位置,現在插入到鏈表中
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
//RT_TIMER_SKIP_LIST_LEVEL是等於1的,不會執行。
//等等我再想想這點吧
for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
{
if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
else
break;
/* Shift over the bits we have tested. Works well with 1 bit and 2
* bits. */
tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
}
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
/* enable interrupt */
rt_hw_interrupt_enable(level);
#ifdef RT_USING_TIMER_SOFT
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
/* check whether timer thread is ready */
if ((timer_thread.stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)
{
/* resume timer thread to check soft timer */
rt_thread_resume(&timer_thread);
rt_schedule();
}
}
#endif
return RT_EOK;
}
RTM_EXPORT(rt_timer_start);
至於爲什麼定時器表的第一個的定時時間是最短,是因爲這個rt_timer_start函數來設置的。定時器的啓動都會經過這個函數,就像是線程啓動都會經過startup函數一樣;定時器啓動的函數就設置了這個定時器處於鏈表的哪一個位置。
硬定時器和軟定時器
硬定時器和軟定時器的說法,你可能在視頻或者什麼裏見過。至少我當時是聽過的,我還記得他當時說,硬定時器的調度是在滴答定時器上的,軟定時器是軟定時上的,他們響應不是在同一個位置。
通過參看源碼,也是這樣的,軟定時器是寫了一個定時線程,這個定時線程的優先級爲10,像是一個線程一樣。而硬件定時器,每一個滴答週期都會去檢索定時器表,以確定那個定時器已經到達時間。
/**
* @ingroup SystemInit
*
* This function will initialize system timer thread
*/
void rt_system_timer_thread_init(void)
{
#ifdef RT_USING_TIMER_SOFT
int i;
for (i = 0;
i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
i++)
{
rt_list_init(rt_soft_timer_list + i);
}
/* start software timer thread */
rt_thread_init(&timer_thread,
"timer",
rt_thread_timer_entry,
RT_NULL,
&timer_thread_stack[0],
sizeof(timer_thread_stack),
RT_TIMER_THREAD_PRIO,
10);
/* startup */
rt_thread_startup(&timer_thread);
#endif
}
時間片的實現
時間片的細節
時間片,是爲了爲同一個優先級下有多個線程時使用的。線程在這個線程就緒表中,且有剩餘的時間片時間,纔可以繼續執行,類似於輪詢。工作狀態類似於輪詢,因爲看起來是每個都執行了。不過,就實況來看,輪詢是執行完週期,這裏是消耗自己的時間片時間。
/**
* Thread structure
*/
struct rt_thread
{
···
rt_ubase_t init_tick; /**< thread's initialized tick */
rt_ubase_t remaining_tick; /**< remaining tick */
···
}
static rt_err_t _rt_thread_init(struct rt_thread *thread,
const char *name,
void (*entry)(void *parameter),
void *parameter,
void *stack_start,
rt_uint32_t stack_size,
rt_uint8_t priority,
rt_uint32_t tick)
{
···
/* tick init */
thread->init_tick = tick;
thread->remaining_tick = tick;
```
}
/**
* This function will notify kernel there is one tick passed. Normally,
* this function is invoked by clock ISR.
*/
void rt_tick_increase(void)
{
struct rt_thread *thread;
/* increase the global tick */
++ rt_tick;
/* check time slice */
thread = rt_thread_self();
-- thread->remaining_tick;
if (thread->remaining_tick == 0)
{
/* change to initialized tick */
thread->remaining_tick = thread->init_tick;
/* yield */
rt_thread_yield(); //如果運行時間達到,出讓CPU,查看這個函數,你可以發現是從就緒列表中刪除
//從優先級列表刪除後,重新插入優先級列表
}
/* check timer */
rt_timer_check();
}
長延時下的RT-Thread任務切換
長延時,比如說要延時10秒,用rt_thread_delay()函數明顯是不行的。所以會出現這樣的函數
tick = rt_tick_get();
while (rt_tick_get() - tick < (RT_TICK_PER_SECOND / 2)) ;
當在這個函數中時候,由於沒有主動出讓CPU,是不是以爲程序會卡死在這端代碼裏。但是實際上,程序是不會卡死在這端代碼中的。由於滴答定時器的原因,系統會檢索當前線程的remain_tick大小,如果相同優先級下有另外一個線程處於就緒態那麼就出讓CPU,如果沒有,那就繼續佔有CPU;或者,檢索定時器,查看定時器是不是有定時到達的線程,若果有,那就執行定時器的回調函數。
線程就緒列表,優先級表,定時器鏈表,對象容器,時間片這五者的關係怎麼用一張圖表示?他們中間是如何切換的?
下面是我的理解:
- 就緒列表是每個優先級一個
- 在優先級列表裏檢索最大的已就緒優先級
- 優先級列表不會主動檢索自己,檢索的功能是rt_scheduler來操作的
- 定時器有定時器鏈表,每個滴答週期都會檢索定時器鏈表