新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應用 > linux內(nèi)核工作隊列講解和源碼詳細注釋

linux內(nèi)核工作隊列講解和源碼詳細注釋

作者: 時間:2016-10-08 來源:網(wǎng)絡(luò) 收藏

flush_workqueue的核心處理函數(shù)為flush_cpu_workqueue:static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)

{ if (cwq->thread == current) { // 如果是工作隊列進程正在被調(diào)度/* * Probably keventd trying to flush its own queue. So simply run * it by hand rather than deadlocking. */ // 執(zhí)行完該工作隊列run_workqueue(cwq);} else { // 定義等待DEFINE_WAIT(wait);long sequence_needed;// 加鎖spin_lock_irq(cwq->lock);// 最新工作結(jié)構(gòu)序號sequence_needed = cwq->insert_sequence;// 該條件是判斷隊列中是否還有沒有執(zhí)行的工作結(jié)構(gòu)while (sequence_needed - cwq->remove_sequence > 0) { // 有為執(zhí)行的工作結(jié)構(gòu)// 通過work_done等待隊列等待prepare_to_wait(cwq->work_done, wait,TASK_UNINTERRUPTIBLE);// 解鎖spin_unlock_irq(cwq->lock);// 睡眠, 由wake_up(cwq->work_done)來喚醒schedule();// 重新加鎖spin_lock_irq(cwq->lock);} // 等待清除finish_wait(cwq->work_done, wait);spin_unlock_irq(cwq->lock);}

4.3 調(diào)度工作

在大多數(shù)情況下, 并不需要自己建立工作隊列,而是只定義工作, 將工作結(jié)構(gòu)掛接到內(nèi)核預定義的事件工作隊列中調(diào)度, 在kernel/workqueue.c中定義了一個靜態(tài)全局量的工作隊列keventd_wq:static struct workqueue_struct *keventd_wq;

4.3.1 立即調(diào)度// 在其他函數(shù)中使用以下函數(shù)來調(diào)度工作結(jié)構(gòu), 是把工作結(jié)構(gòu)掛接到工作隊列中進行調(diào)度/** * schedule_work - put work task in global workqueue * @work: job to be done * * This puts a job in the kernel-global workqueue. */ // 調(diào)度工作結(jié)構(gòu), 將工作結(jié)構(gòu)添加到事件工作隊列keventd_wq int fastcall schedule_work(struct work_struct *work)

{ return queue_work(keventd_wq, work);} EXPORT_SYMBOL(schedule_work);

/** * queue_work - queue work on a workqueue * @wq: workqueue to use * @work: work to queue * * Returns 0 if @work was already on a queue, non-zero otherwise. * * We queue the work to the CPU it was submitted, but there is no * guarantee that it will be processed by that CPU. */ int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)

{ int ret = 0, cpu = get_cpu();if (!test_and_set_bit(0, work->pending)) { // 工作結(jié)構(gòu)還沒在隊列, 設(shè)置pending標志表示把工作結(jié)構(gòu)掛接到隊列中if (unlikely(is_single_threaded(wq)))

cpu = singlethread_cpu;BUG_ON(!list_empty(work->entry));// 進行具體的排隊__queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);ret = 1;} put_cpu();return ret;} EXPORT_SYMBOL_GPL(queue_work);/* Preempt must be disabled. */ // 不能被搶占static void __queue_work(struct cpu_workqueue_struct *cwq,struct work_struct *work)

{ unsigned long flags;// 加鎖spin_lock_irqsave(cwq->lock, flags);// 指向CPU工作隊列work->wq_data = cwq;// 掛接到工作鏈表list_add_tail(work->entry, cwq->worklist);// 遞增插入的序列號cwq->insert_sequence++;// 喚醒等待隊列準備處理工作結(jié)構(gòu)wake_up(cwq->more_work);spin_unlock_irqrestore(cwq->lock, flags);}

4.3.2 延遲調(diào)度

4.3.2.1 schedule_delayed_work /** * schedule_delayed_work - put work task in global workqueue after delay * @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. */ // 延遲調(diào)度工作, 延遲一定時間后再將工作結(jié)構(gòu)掛接到工作隊列int fastcall schedule_delayed_work(struct work_struct *work, unsigned long delay)

{ return queue_delayed_work(keventd_wq, work, delay);} EXPORT_SYMBOL(schedule_delayed_work);

/** * queue_delayed_work - queue work on a workqueue after delay * @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 fastcall queue_delayed_work(struct workqueue_struct *wq,struct work_struct *work, unsigned long delay)

{ int ret = 0;// 定時器, 此時的定時器應該是不起效的, 延遲將通過該定時器來實現(xiàn)struct timer_list *timer = work->timer;if (!test_and_set_bit(0, work->pending)) { // 工作結(jié)構(gòu)還沒在隊列, 設(shè)置pending標志表示把工作結(jié)構(gòu)掛接到隊列中// 如果現(xiàn)在定時器已經(jīng)起效, 出錯BUG_ON(timer_pending(timer));// 工作結(jié)構(gòu)已經(jīng)掛接到鏈表, 出錯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;// 定時函數(shù)timer->function = delayed_work_timer_fn;// 定時器生效, 定時到期后再添加到工作隊列add_timer(timer);ret = 1;} return ret;} EXPORT_SYMBOL_GPL(queue_delayed_work);

// 定時中斷函數(shù)static void delayed_work_timer_fn(unsigned long __data)

{ struct work_struct *work = (struct work_struct *)__data;struct workqueue_struct *wq = work->wq_data;// 獲取CPU int cpu = smp_processor_id();if (unlikely(is_single_threaded(wq)))

cpu = singlethread_cpu;// 將工作結(jié)構(gòu)添加到工作隊列,注意這是在時間中斷調(diào)用__queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);}



關(guān)鍵詞:

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉