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"); // 创建工作队列
相关推荐
orion5721 小时前
Missing Semester Class1:course overview and introduction of shell
linux
用户120487221611 天前
Linux驱动编译与加载
linux·嵌入式
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698031 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia3 天前
linux curl命令详解_curl详解
linux
扛枪的书生3 天前
Linux 网络管理器用法速查
linux
顺风尿一寸3 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode3 天前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫3 天前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux