linux - 如何在 Linux 中多次排队相同的工作队列工作?

标签 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)

最佳答案

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);      
}

工作队列也是内核线程,具有特定的线程函数kthread_worker_fn()

关于linux - 如何在 Linux 中多次排队相同的工作队列工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44121372/

相关文章:

linux - 无法将以空格分隔的文件添加到git

linux-kernel - 抢占时 ISR 能否迁移到其他 CPU?

linux - 如何在centos 5.5上安装AMP

linux - 使用 Shell 脚本进行数据、时间验证

linux - 使用 linux I2C 驱动程序

linux-kernel - dma_map_single() : minimum requirements to struct device

linux - Linux 内核支持的最大 IRQ 数量是多少?

linux-kernel - i2c 探针没有被调用....不知道在哪里调用 i2c_register_board_info

java - 在 java 代码中间将参数传递给 bash 文件

linux - 如何在 Bash 中重定向输出