linux內(nèi)核工作隊列講解和源碼詳細注釋
4.3.2.2 schedule_delayed_work_on
指定CPU的延遲調(diào)度工作結構, 和schedule_delayed_work相比增加了一個CPU參數(shù), 其他都相同/** * schedule_delayed_work_on - queue work in global workqueue on CPU after delay * @cpu: cpu to use * @work: job to be done * @delay: number of jiffies to wait * * After waiting for a given time this puts a job in the kernel-global * workqueue on the specified CPU. */ int schedule_delayed_work_on(int cpu,struct work_struct *work, unsigned long delay)
{ return queue_delayed_work_on(cpu, keventd_wq, work, delay);}
/** * queue_delayed_work_on - queue work on specific CPU after delay * @cpu: CPU number to execute work on * @wq: workqueue to use * @work: work to queue * @delay: number of jiffies to wait before queueing * * Returns 0 if @work was already on a queue, non-zero otherwise. */ int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,struct work_struct *work, unsigned long delay)
{ int ret = 0;struct timer_list *timer = work->timer;if (!test_and_set_bit(0, work->pending)) { BUG_ON(timer_pending(timer));BUG_ON(!list_empty(work->entry));/* This stores wq for the moment, for the timer_fn */ work->wq_data = wq;timer->expires = jiffies + delay;timer->data = (unsigned long)work;timer->function = delayed_work_timer_fn;add_timer_on(timer, cpu);ret = 1;} return ret;} EXPORT_SYMBOL_GPL(queue_delayed_work_on);
5. 結論
工作隊列和定時器函數(shù)處理有點類似, 都是執(zhí)行一定的回調(diào)函數(shù), 但和定時器處理函數(shù)不同的是定時器回調(diào)函數(shù)只執(zhí)行一次, 而且執(zhí)行定時器回調(diào)函數(shù)的時候是在時鐘中斷中, 限制比較多, 因此回調(diào)程序不能太復雜; 而工作隊列是通過內(nèi)核線程實現(xiàn), 一直有效, 可重復執(zhí)行, 由于執(zhí)行時降低了線程的優(yōu)先級, 執(zhí)行時可能休眠, 因此工作隊列處理的應該是那些不是很緊急的任務, 如垃圾回收處理等, 通常在系統(tǒng)空閑時執(zhí)行,在xfrm庫中就廣泛使用了workqueue,使用時,只需要定義work結構,然后調(diào)用schedule_(delayed_)work即可。
評論