如何在Linux中多次排队同一个workqueue工作?

时间:2017-05-22 20:12:28

标签: linux linux-device-driver

我看到,当调用schedule_work函数时,如果工作任务已经排队,它将不会将工作任务放入队列中。但是,我想将同一个任务排队等待多次运行,即使它已经在队列中。怎么办呢?

来自workqueue.h:

/**
 * schedule_work - put work task in global workqueue
 * @work: job to be done
 *
 * Returns %false if @work was already on the kernel-global workqueue and
 * %true otherwise.
 *
 * This puts a job in the kernel-global workqueue if it was not already
 * queued and leaves it in the same position on the kernel-global
 * workqueue otherwise.
 */
static inline bool schedule_work(struct work_struct *work)

1 个答案:

答案 0 :(得分:2)

children期望每个Workqueue结构代表单个“任务”,需要运行一次。

因此,最简单的方法是多次运行任务 - 每次都创建新的work结构。

或者,因为在运行时重复工作是工作队列的异常,你可以创建自己的内核线程来重复执行某些功能:

work

工作队列也是内核线程,具有特定的线程函数DECLARE_WAITQUEUE(repeat_wq); // Kernel thread will wait on this workqueue. int n_works = 0; // Number of work requests to process. // Thread function void repeat_work(void* unused) { spin_lock_irq(repeat_wq.lock); // Reuse workqueue's spinlock for our needs while(1) { // Wait until work request or thread should be stopped wait_event_interruptible_locked(&repeat_wq, n_works || kthread_should_stop()); if(kthread_should_stop()) break; spin_unlock_irq(repeat_wq.lock); <do the work> // Acquire the lock for decrement count and recheck condition spin_lock_irq(repeat_wq.lock); n_works--; } // Finally release the lock spin_unlock_irq(repeat_wq.lock); } // Request new work. void add_work(void) { unsigned long flags; spin_lock_irqsave(repeat_wq.lock, flags); n_works++; wake_up_locked(&repeat_wq); spin_unlock_irqrestore(repeat_wq.lock, flags); }