驅動程式使用tasklet機制

來源:互聯網
上載者:User
驅動程式在初始化時,通過函數task_init建立一個tasklet,然後調用函數tasklet_schedule將這個tasklet
放在
tasklet_vec鏈表的頭部,並喚醒後台線程ksoftirqd。當後台線程ksoftirqd運行調用__do_softirq時,會執行在中斷
向量表softirq_vec裡中斷號TASKLET_SOFTIRQ對應的tasklet_action函數,然後tasklet_action遍曆
tasklet_vec鏈表,調用每個tasklet的函數完成非強制中斷操作。 下面對函數tasklet_init和tasklet_schedule分析:   函數tasklet_init初始化一個tasklet,其參數t是tasklet_struct結構描述的tasklet,參數(*func)是非強制中斷響應函數。void tasklet_init(struct tasklet_struct *t,
    void (*func)(unsigned long), unsigned long data)
{
 t->next = NULL;
 t->state = 0;
 atomic_set(&t->count, 0);
 t->func = func;
 t->data = data;
}驅動程式調用函數tasklet_schedule來運行tasklet。static inline void tasklet_schedule(struct tasklet_struct *t)
{
 if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
  __tasklet_schedule(t);
}函數__tasklet_schedule得到當前CPU的tasklet_vec鏈表,並執行TASKLET_SOFTIRQ非強制中斷。void fastcall __tasklet_schedule(struct tasklet_struct *t)
{
 unsigned long flags; local_irq_save(flags);
 t->next = __get_cpu_var(tasklet_vec).list;
 __get_cpu_var(tasklet_vec).list = t;
 raise_softirq_irqoff(TASKLET_SOFTIRQ);
 local_irq_restore(flags);
}函數raise_softirq_irqoff設定非強制中斷nr為掛起狀態,並在沒有中斷時喚醒線程ksoftirqd。函數raise_softirq_irqoff必須在關中斷情況下運行。inline fastcall void raise_softirq_irqoff(unsigned int nr)
{
 __raise_softirq_irqoff(nr); /*
  * If we're in an interrupt or softirq, we're done
  * (this also catches softirq-disabled code). We will
  * actually run the softirq once we return from
  * the irq or softirq.
  *
  * Otherwise we wake up ksoftirqd to make sure we
  * schedule the softirq soon.
  */
 if (!in_interrupt())
  wakeup_softirqd();
} 下面是tasklet_struct和softirq_action的定義。struct tasklet_struct
{
 struct tasklet_struct *next;
 unsigned long state;
 atomic_t count;
 void (*func)(unsigned long);
 unsigned long data;
}; struct softirq_action
{
 void (*action)(struct softirq_action *);
 void *data;
}; 

摘錄於《Linux核心分析及編程>

http://hi.baidu.com/ryderlee/blog/item/ceeec316e8d1f318962b431a.html

 1.Tasklet 可被hi-schedule和一般schedule,hi-schedule一定比一般shedule早運行;
   2.同一個Tasklet可同時被hi-schedule和一般schedule;
   3.同一個Tasklet若被同時hi-schedule多次,等同於只hi-shedule一次,因為,在tasklet未 運行時,hi-shedule同一tasklet無意義,會衝掉前一個tasklet;
   4.對於一般shedule, 同上。
   5.不同的tasklet不按先後shedule順序運行,而是並行運行。
 6.Tasklet的已耗用時間:
         a.若在中斷中schedule tasklet, 中斷結束後立即運行;
         b.若CPU忙,在不在此次中斷後立即運行;
         c.不在中斷中shedule tasklet;
         d.有軟或硬中斷在運行;
         e.從系統調用中返回;(僅當process閑時)
         f.從異常中返回;
         g.偵錯工具調度。(ksoftirqd運行時,此時CPU閑)
 7.Taskelet的hi-schedule 使用softirq 0, 一般schedule用softirq 30;
 8.Tasklet的已耗用時間最完在下一次time tick 時。(因為最外層中斷一定會運行使能的softirq, 面不在中斷中便能或shedule的softirq在下一定中斷後一定會被調用。)

  綜上: Tasklet 能保證的已耗用時間是(1000/HZ)ms,一般是10ms。Tasklet在CPU閑或中斷後被調用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.