?? 2.html
字號:
jmp 9f<p>ALIGN<br>reschedule:<br>pushl $ret_from_sys_call<br> jmp SYMBOL_NAME(schedule) # test<p>另外,一些與時鐘中斷及bottom half機制有關的數據結構介紹如下:<br>#define HZ 100<br>unsigned long volatile jiffies=0;<br>系統每隔10ms自動把它加1,它是核心系統計時的單位。<br>enum {<br> TIMER_BH = 0,<br> CONSOLE_BH,<br> TQUEUE_BH,<br> DIGI_BH,<br> SERIAL_BH,<br> RISCOM8_BH,<br>SPECIALIX_BH,<br> BAYCOM_BH,<br> NET_BH,<br> IMMEDIATE_BH,<br> KEYBOARD_BH,<br> CYCLADES_BH,<br> CM206_BH<br>};<br>現在只定義了13個bottom half隊列,將來可擴充到32個隊列。<br>unsigned long intr_count = 0;<br>相當于信號量的作用。只有其等于0,才可以do_bottom_half。<br>int bh_mask_count[32];<br>用來計算bottom half隊列被屏蔽的次數。只有某隊列的bh_mask_count數為0,才能enable該隊列。<br>unsigned long bh_active = 0;<br>bh_active是32位長整數,每一位表示一個bottom half隊列,該位置1,表示該隊列處于激活狀態,隨時準備在CPU認為合適的時候執行該隊列的服務,置0則相反。<br>unsigned long bh_mask = 0;<br>bh_mask也是32位長整數,每一位對應一個bottom half隊列,該位置1,表示該隊列可用,并把處理函數的入口地址賦給bh_base,置0則相反。<br>void (*bh_base[32])(void);<br>bottom half服務函數入口地址數組。定時器處理函數擁有最高的優先級,它的地址存放在bh_base[0],總是最先執行它所指向的函數。<p>我們注意到,在IRQ#_interrupt和fast_IRQ#_interrupt中斷函數處理返回前,都通過語句jmp ret_from_sys_call,跳到系統調用的返回處(見irq.h),如果bottom half隊列不為空,則在那里做類似:<br> if (bh_active & bh_mask) {<br> intr_count = 1;<br> do_bottom_half();<br> intr_count = 0;<br> }(該判斷的匯編代碼見Entry.S)<br>的判斷,調用do_bottom_half()函數。<br>在CPU調度時,通過schedule函數執行上述的判斷,再調用do_bottom_half()函數。<br>總而言之,在下列三種時機:<br>CPU調度時<br>系統調用返回前<br>中斷處理返回前<br>都會作判斷調用do_bottom_half函數。Do_bottom_half函數依次掃描32個隊列,找出需要服務的隊列,執行服務后把對應該隊列的bh_active的相應位置0。由于bh_active標志中TIMER_BH對應的bit為1,因而系統根據服務函數入口地址數組bh_base找到函數timer_bh()的入口地址,并馬上執行該函數,在函數timer_bh中,調用函數run_timer_list()和函數run_old_timers()函數,定時執行服務。<p>TVECS結構及其實現<br>有關TVECS結構的一些數據結構定義如下:<p>#define TVN_BITS 6<br>#define TVR_BITS 8<br>#define TVN_SIZE (1 << TVN_BITS)<br>#define TVR_SIZE (1 << TVR_BITS)<br>#define TVN_MASK (TVN_SIZE - 1)<br>#define TVR_MASK (TVR_SIZE - 1)<p>#define SLOW_BUT_DEBUGGING_TIMERS 0<p>struct timer_vec {<br> int index;<br> struct timer_list *vec[TVN_SIZE];<br>};<br>struct timer_vec_root {<br> int index;<br> struct timer_list *vec[TVR_SIZE];<br>};<p>static struct timer_vec tv5 = { 0 };<br>static struct timer_vec tv4 = { 0 };<br>static struct timer_vec tv3 = { 0 };<br>static struct timer_vec tv2 = { 0 };<br>static struct timer_vec_root tv1 = { 0 };<p>static struct timer_vec * const tvecs[] = {<br> (struct timer_vec *)&tv1, &tv2, &tv3, &tv4, &tv5<br>};<br>#define NOOF_TVECS (sizeof(tvecs) / sizeof(tvecs[0]))<br>static unsigned long timer_jiffies = 0;<p>TVECS結構是一個元素個數為5的數組,分別指向tv1,tv2,tv3,tv4,tv5的地址。其中,tv1是結構timer_vec_root的變量,它有一個index域和有256個元素的指針數組,該數組的每個元素都是一條類型為timer_list的鏈表。其余四個元素都是結構timer_vec的變量,它們各有一個index域和64個元素的指針數組,這些數組的每個元素也都是一條鏈表。<p>函數internal_add_timer(struct timer_list *timer)<p>函數代碼如下:<br>static inline void internal_add_timer(struct timer_list *timer)<br>{<br> /*<br> * must be cli-ed when calling this<br> */<br> unsigned long expires = timer->expires;<br> unsigned long idx = expires - timer_jiffies;<p> if (idx < TVR_SIZE) {<br> int i = expires & TVR_MASK;<br> insert_timer(timer, tv1.vec, i);<br> } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {<br> int i = (expires >> TVR_BITS) & TVN_MASK;<br> insert_timer(timer, tv2.vec, i);<br> } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {<br> int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;<br> insert_timer(timer, tv3.vec, i);<br> } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {<br> int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;<br> insert_timer(timer, tv4.vec, i);<br> } else if (expires < timer_jiffies) {<br> /* can happen if you add a timer with expires == jiffies,<br> * or you set a timer to go off in the past<br> */<br> insert_timer(timer, tv1.vec, tv1.index);<br> } else if (idx < 0xffffffffUL) {<br> int i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;<br> insert_timer(timer, tv5.vec, i);<br> } else {<br> /* Can only get here on architectures with 64-bit jiffies */<br> timer->next = timer->prev = timer;<br> }<br>}<p> expires<p><br>在調用該函數之前,必須關中。對該函數的說明如下:<br>取出要加進TVECS的timer的激發時間(expires),算出expires與timer_jiffies的差值idx,用來決定該插到哪個隊列中去。<br>若idx小于2^8,則取expires的第0位到第7位的值I,把timer加到tv1.vec中第I個鏈表的第一個表項之前。<br>若idx小于2^14,則取expires的第8位到第13位的值I,把timer加到tv2.vec中第I個鏈表的第一個表項之前。<br>若idx小于2^20,則取expires的第14位到第19位的值I,把timer加到tv3.vec中第I個鏈表的第一個表項之前。<br>若idx小于2^26,則取expires的第20位到第25位的值I,把timer加到tv4.vec中第I個鏈表的第一個表項之前。<br>若expires小于timer_jiffies,即idx小于0,則表明該timer到期,應該把timer放入tv1.vec中tv1.index指定的鏈表的第一個表項之前。<br>若idx小于2^32,則取expires的第26位到第32位的值I,把timer加到tv5.vec中第I個鏈表的第一個表項之前。<br>若idx大等于2^32,該情況只有在64位的機器上才有可能發生,在這種情況下,不把timer加入TVECS結構。<p>函數cascade_timers(struct timer_vec *tv)<p>該函數只是把tv->index指定的那條鏈表上的所有timer調用internal_add_timer()函數進行重新調整,這些timer將放入TVECS結構中比原來位置往前移一級,比如說,tv4上的timer將放到tv3上去,tv2上的timer將放到tv1上。這種前移是由run_timer_list函數里調用cascade_timers函數的時機來保證的。然后把該條鏈表置空,tv->index加1,若tv->index等于64,則重新置為0。<p>函數run_timer_list()<p>函數代碼如下:<br>static inline void run_timer_list(void)<br>{<br>cli();<br>while ((long)(jiffies - timer_jiffies) >= 0) {<br> struct timer_list *timer;<br> if (!tv1.index) {<br> int n = 1;<br> do {<br> cascade_timers(tvecs[n]);<br> } while (tvecs[n]->index == 1 && ++n < NOOF_TVECS);<br> }<br> while ((timer = tv1.vec[tv1.index])) {<br> void (*fn)(unsigned long) = timer->function;<br> unsigned long data = timer->data;<br> detach_timer(timer);<br> timer->next = timer->prev = NULL;<br> sti();<br> fn(data);<br>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -