延迟工作队列可以对原有的共享工作队列和自定义工作队列,加上定时器进一步封装,再进入中断程序的下半部分进行延迟进入。
直接延迟:「先占队列,再等延迟」→ 霸占公共资源;
delayed_work:「先等延迟,再占队列执行」→ 仅占用执行逻辑的必要时间,不浪费公共资源。
这是比在直接在中断下半段延迟的好处
驱动函数
cpp
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/workqueue.h>
int irq;
struct workqueue_struct *test_workqueue;
struct delayed_work test_workqueue_work;
// 工作项处理函数
void test_work(struct work_struct *work)
{
msleep(1000);
printk("This is test_work\n");
}
// 中断处理函数
irqreturn_t test_interrupt(int irq, void *args)
{
printk("This is test_interrupt\n");
// 提交延迟工作项到自定义工作队列
queue_delayed_work(test_workqueue, &test_workqueue_work, 3 * HZ);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int interrupt_irq_init(void)
{
int ret;
irq = gpio_to_irq(101); // 将GPIO映射为中断号
printk("irq is %d\n", irq);
// 请求中断
ret = request_irq(irq, test_interrupt, IRQF_TRIGGER_RISING, "test", NULL);
if (ret < 0)
{
printk("request_irq is error\n");
return -1;
}
// 创建工作队列
test_workqueue = create_workqueue("test_workqueue");
// 初始化延迟工作项
INIT_DELAYED_WORK(&test_workqueue_work, test_work);
return 0;
}
static void interrupt_irq_exit(void)
{
free_irq(irq, NULL); // 释放中断
cancel_delayed_work_sync(&test_workqueue_work); // 取消延迟工作项
flush_workqueue(test_workqueue); // 刷新工作队列
destroy_workqueue(test_workqueue); // 销毁工作队列
printk("bye bye\n");
}
module_init(interrupt_irq_init);
module_exit(interrupt_irq_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("quan");
Makfile
bash
obj-m += delay_workqueue.o
KDIR:=/home/linux/samba-mount/linux-kernel/linux-6.17.5
PWD?=$(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
echo $(PWD)
clean:
rm -rf *.ko *.o *.mod *.mod.o *.mod.c *.symvers *.order
install:
cp *.ko ../../linux-kernel/linux-6.17.5/kmodules
编译及开发板验证测试

由此可以看到延迟了4秒钟,包括进入下半段和下半段延迟。