新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > linux內核工作隊列講解和源碼詳細注釋

        linux內核工作隊列講解和源碼詳細注釋

        作者: 時間:2016-10-08 來源:網絡 收藏

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

        { if (cwq->thread == current) { // 如果是工作隊列進程正在被調度/* * Probably keventd trying to flush its own queue. So simply run * it by hand rather than deadlocking. */ // 執行完該工作隊列run_workqueue(cwq);} else { // 定義等待DEFINE_WAIT(wait);long sequence_needed;// 加鎖spin_lock_irq(cwq->lock);// 最新工作結構序號sequence_needed = cwq->insert_sequence;// 該條件是判斷隊列中是否還有沒有執行的工作結構while (sequence_needed - cwq->remove_sequence > 0) { // 有為執行的工作結構// 通過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 調度工作

        在大多數情況下, 并不需要自己建立工作隊列,而是只定義工作, 將工作結構掛接到內核預定義的事件工作隊列中調度, 在kernel/workqueue.c中定義了一個靜態全局量的工作隊列keventd_wq:static struct workqueue_struct *keventd_wq;

        4.3.1 立即調度// 在其他函數中使用以下函數來調度工作結構, 是把工作結構掛接到工作隊列中進行調度/** * schedule_work - put work task in global workqueue * @work: job to be done * * This puts a job in the kernel-global workqueue. */ // 調度工作結構, 將工作結構添加到事件工作隊列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)) { // 工作結構還沒在隊列, 設置pending標志表示把工作結構掛接到隊列中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++;// 喚醒等待隊列準備處理工作結構wake_up(cwq->more_work);spin_unlock_irqrestore(cwq->lock, flags);}

        4.3.2 延遲調度

        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. */ // 延遲調度工作, 延遲一定時間后再將工作結構掛接到工作隊列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;// 定時器, 此時的定時器應該是不起效的, 延遲將通過該定時器來實現struct timer_list *timer = work->timer;if (!test_and_set_bit(0, work->pending)) { // 工作結構還沒在隊列, 設置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(timer);ret = 1;} return ret;} EXPORT_SYMBOL_GPL(queue_delayed_work);

        // 定時中斷函數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;// 將工作結構添加到工作隊列,注意這是在時間中斷調用__queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);}



        關鍵詞:

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 亳州市| 华池县| 舟曲县| 长宁区| 延寿县| 固原市| 清苑县| 尼玛县| 枣强县| 陆良县| 永川市| 天全县| 稻城县| 吉林市| 荣成市| 南华县| 安塞县| 尼木县| 泰和县| 乐安县| 邢台县| 宜良县| 定陶县| 民和| 廉江市| 抚远县| 区。| 台州市| 内丘县| 长沙市| 策勒县| 荣成市| 西贡区| 江阴市| 洞口县| 潞西市| 永平县| 龙胜| 玛纳斯县| 大连市| 津南区|