驅動程式在初始化時,通過函數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閑或中斷後被調用。