如何在Linux中对同一个工作队列进行多次排队?
我看到当调用如何在Linux中对同一个工作队列进行多次排队?,linux,linux-device-driver,Linux,Linux Device Driver,我看到当调用schedule\u 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 a
schedule\u 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)
Workqueue
要求每个work
结构表示单个“任务”,需要运行一次
所以,最简单的方法就是多次运行一个任务——每次创建新的work
结构
或者,由于在运行时重复工作对于workqueue来说是不寻常的,因此您可以创建自己的内核线程以重复执行某些函数:
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);
}
DECLARE_WAITQUEUE(repeat_wq);//内核线程将在此工作队列上等待。
int n_works=0;//要处理的工作请求数。
//线程函数
作废重复工作(作废*未使用)
{
spin_lock_irq(repeat_wq.lock);//根据需要重用工作队列的spinlock
而(1){
//等待工作请求或线程停止
等待事件可中断锁定(&重复),
n| | | kthread|u应该|u stop());
如果(kthread_should_stop())中断;
旋转解锁irq(重复锁定);
//获取减量计数锁并重新检查条件
旋转锁定(重复锁定);
n_工作--;
}
//最后释放锁
旋转解锁irq(重复锁定);
}
//请求新工作。
作废添加工作(作废)
{
无符号长旗;
旋转锁定irqsave(重复锁定,标志);
n_works++;
唤醒锁定(&重复);
旋转解锁irqrestore(重复锁定,标志);
}
工作队列也是内核线程,有一个特定的线程函数
kthread\u worker\u fn()
,我想你可以安排尽可能多的在线CPU工作。否则你需要考虑其他的事情。