新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > linux内核工作队列讲解和源码详细注释

linux内核工作队列讲解和源码详细注释

作者: 时间:2016-10-08 来源:网络 收藏

4.3.2.2 schedule_delayed_work_on

指定CPU的延迟调度工作结构, 和schedule_delayed_work相比增加了一个CPU参数, 其他都相同/** * 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. 结论

工作队列和定时器函数处理有点类似, 都是执行一定的回调函数, 但和定时器处理函数不同的是定时器回调函数只执行一次, 而且执行定时器回调函数的时候是在时钟中断中, 限制比较多, 因此回调程序不能太复杂; 而工作队列是通过内核线程实现, 一直有效, 可重复执行, 由于执行时降低了线程的优先级, 执行时可能休眠, 因此工作队列处理的应该是那些不是很紧急的任务, 如垃圾回收处理等, 通常在系统空闲时执行,在xfrm库中就广泛使用了workqueue,使用时,只需要定义work结构,然后调用schedule_(delayed_)work即可。


上一页 1 2 3 4 下一页

关键词:

评论


相关推荐

技术专区

关闭