<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

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

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

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

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

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

          4.3 調(diào)度工作

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

          4.3.1 立即調(diào)度// 在其他函數(shù)中使用以下函數(shù)來(lái)調(diào)度工作結(jié)構(gòu), 是把工作結(jié)構(gòu)掛接到工作隊(duì)列中進(jìn)行調(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)添加到事件工作隊(duì)列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)還沒(méi)在隊(duì)列, 設(shè)置pending標(biāo)志表示把工作結(jié)構(gòu)掛接到隊(duì)列中if (unlikely(is_single_threaded(wq)))

          cpu = singlethread_cpu;BUG_ON(!list_empty(work->entry));// 進(jìn)行具體的排隊(duì)__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工作隊(duì)列work->wq_data = cwq;// 掛接到工作鏈表list_add_tail(work->entry, cwq->worklist);// 遞增插入的序列號(hào)cwq->insert_sequence++;// 喚醒等待隊(duì)列準(zhǔn)備處理工作結(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)度工作, 延遲一定時(shí)間后再將工作結(jié)構(gòu)掛接到工作隊(duì)列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;// 定時(shí)器, 此時(shí)的定時(shí)器應(yīng)該是不起效的, 延遲將通過(guò)該定時(shí)器來(lái)實(shí)現(xiàn)struct timer_list *timer = work->timer;if (!test_and_set_bit(0, work->pending)) { // 工作結(jié)構(gòu)還沒(méi)在隊(duì)列, 設(shè)置pending標(biāo)志表示把工作結(jié)構(gòu)掛接到隊(duì)列中// 如果現(xiàn)在定時(shí)器已經(jīng)起效, 出錯(cuò)BUG_ON(timer_pending(timer));// 工作結(jié)構(gòu)已經(jīng)掛接到鏈表, 出錯(cuò)BUG_ON(!list_empty(work->entry));/* This stores wq for the moment, for the timer_fn */ // 保存工作隊(duì)列的指針work->wq_data = wq;// 定時(shí)器初始化timer->expires = jiffies + delay;timer->data = (unsigned long)work;// 定時(shí)函數(shù)timer->function = delayed_work_timer_fn;// 定時(shí)器生效, 定時(shí)到期后再添加到工作隊(duì)列add_timer(timer);ret = 1;} return ret;} EXPORT_SYMBOL_GPL(queue_delayed_work);

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



          關(guān)鍵詞:

          評(píng)論


          相關(guān)推薦

          技術(shù)專(zhuān)區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();