linux中断下文工作队列之自定义工作队列(中断五)

共享队列是由内核管理的全局工作队列,自定义工作队列是由内核或驱动程序创建的特定工作队列,用于处理特定的任务。

一、工作队列相关结构体

在 Linux 内核中,结构体 struct work_struct 描述的是要延迟执行的工作项,定义在include/linux/workqueue.h 当中,如下所示

c 复制代码
struct work_struct {
atomic_long_t data; // 工作项的数据字段
struct list_head entry; // 工作项在工作队列中的链表节点work_func_t func; // 工作项的处理函数
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map; // 锁依赖性映射
#endif
};

这些工作组织成工作队列,内核使用 struct workqueue_struct 结构体描述一个工作队列,定义在 include/linux/workqueue.h 当中,如下所示:

c 复制代码
struct workqueue_struct {
struct list_head pwqs; // 工作队列上的挂起工作项列表struct list_head delayed_works; // 延迟执行的工作项列表
struct delayed_work_timer dwork_timer; // 延迟工作项的定时器
struct workqueue_attrs *unbound_attrs; // 无绑定工作队列的属性struct pool_workqueue *dfl_pwq; // 默认的池化工作队列
... 
};

二、工作队列相关接口函数

2.1、创建一个工作队列

在 Linux 内核中,create_workqueue 函数用于创建一个工作队列,函数原型如下所示:

c 复制代码
struct workqueue_struct *create_workqueue(const char *name);

参数 name 是创建的工作队列的名字。使用这个函数可以给每个CPU 都创建一个CPU相关的工作队列。创建成功返回一个 struct workqueue_struct 类型指针,创建失败返回NULL。

如果只给一个 CPU 创建一个 CPU 相关的工作队列,使用以下函数。

c 复制代码
#define create_singlethread_workqueue(name) \ 
alloc_workqueue("%s", WQ_SINGLE_THREAD, 1, name)

参数 name 是创建的工作队列的名字。使用这个函数只会给一个CPU 创建一个CPU相关的工作队列。创建成功之后返回一个 struct workqueue_struct 类型指针,创建失败返回NULL。

2.2、调度工作队列

当工作队列创建好之后,需要将要延迟执行的工作项放在工作队列上,调度工作队列,使用 queue_work_on 函数,函数原型如下所示:

c 复制代码
bool queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work);

该函数有三个参数,第一个参数是一个整数 cpu,第二个参数是一个指向structworkqueue_struct 的指针 wq,第三个参数是一个指向 struct work_struct 的指针work。

该函数的返回类型是布尔值,表示是否成功调度工作队列。queue_work_on函数还有其他变种,比如 queue_work 函数,这里略过,其实思路是一致的,用于将定义好的工作项立即添加到工作队列中,并在工作队列可用时立即执行。

2.3、取消已经调度的工作

如果要取消一个已经调度的工作,使用函数 bool cancel_work_sync,函数原型如下所示:

c 复制代码
bool cancel_work_sync(struct work_struct *work);

函数的作用是取消一个已经调度的工作,如果被取消的工作已经正在执行,则会等待他执行完成再返回

2.4、刷新工作队列

在 Linux 内核中,使用 flush_workqueue 函数将刷新该工作队列中所有已提交但未执行的工作项。函数原型如下所示:

c 复制代码
void flush_workqueue(struct workqueue_struct *wq);

该函数参数是一个指向 struct workqueue_struct 类型的指针wq。函数的作用是刷新工作队列,告诉内核尽快处理工作队列上的工作。

2.5、删除自定义的工作队列

如果要删除自定义的工作队列,使用 destroy_workqueue 函数,函数原型如下所示:

c 复制代码
void destroy_workqueue(struct workqueue_struct *wq);

该函数参数是一个指向 struct workqueue_struct 类型的指针wq。

三、代码示例

3.1、驱动层程序

c 复制代码
#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 work_struct 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_work(test_workqueue, &test_workqueue_work); // 提交工作项到工作队列
  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_WORK(&test_workqueue_work, test_work);          // 初始化工作项

  return 0;
}

static void interrupt_irq_exit(void)
{
  free_irq(irq, NULL);                    // 释放中断
  cancel_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);

3.2、linux中断下文工作队列之自定义工作队列使用API要点

c 复制代码
struct workqueue_struct *test_workqueue;
c 复制代码
  test_workqueue = create_workqueue("test_workqueue"); // 创建工作队列
相关推荐
好想打kuo碎6 分钟前
轻量安全的密码管理工具Vaultwarden
linux·安全·ubuntu
Mayer_WOT7 分钟前
Jetson Orin AGX Getting Start with Pytorch
linux
什么半岛铁盒43 分钟前
Linux进程异常退出排查指南
linux·运维·服务器
Mylvzi1 小时前
Linux 性能利器:详解 `top` 命令的使用与输出信息解析
linux·服务器·网络
斗转星移31 小时前
解决ubuntu20.04无法唤醒的问题的一种方法
linux·ubuntu·电脑
lyh13442 小时前
【Ubuntu崩溃修复】
linux·运维·服务器
什么半岛铁盒3 小时前
【Linux系统】Linux环境变量:系统配置的隐形指挥官
linux
Lw老王要学习3 小时前
Linux容器篇、第一章_02Rocky9.5 系统下 Docker 的持久化操作与 Dockerfile 指令详解
linux·运维·docker·容器·云计算
橙子小哥的代码世界4 小时前
【大模型RAG】Docker 一键部署 Milvus 完整攻略
linux·docker·大模型·milvus·向量数据库·rag
倔强的石头1065 小时前
【Linux指南】用户与系统基础操作
linux·运维·服务器