Linux内核工做队列

Linux内核中实现工做推后执行的方法有:软中断、tasklet和工做队列(work queue)。linux

本文介绍工做队列的简单用法。函数

1、工做任务定义

Linux内核中的工做队列包括:共享工做队列和自定义工做队列。区别以下:spa

1)共享工做队列:将新建立的工做任务添加到Linux内核建立的全局工做队列system_wq中;线程

2)自定义工做队列:将新建立的工做任务添加到本身建立的工做队列中;指针

一、共享工做队列rest

1)、静态定义code

宏:DECLARE_WORK(n, f),文件:include/linux/workqueue.h,定义以下:接口

#define DECLARE_WORK(n, f)      \
 struct work_struct n = __WORK_INITIALIZER(n, f)

参数:队列

n:表示工做任务的名称;ci

f:表示工做任务的实现函数;

相似接口:DECLARE_DELAYED_WORK(n, f),建立延时工做任务。

2)、动态定义

文件:include/linux/workqueue.h,定义以下:

#define INIT_WORK(_work, _func)      \
 __INIT_WORK((_work), (_func), 0)

参数:

_work:表示工做任务的名称;

_func:表示工做任务的实现函数;

二、自定义工做队列

文件:include/linux/workqueue.h,定义以下:

#define create_workqueue(name)      \
 alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, (name))
 
#define create_singlethread_workqueue(name)    \
 alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, name)

参数:

name:工做队列名称。传入值为字符串,和共享工做队列里的参数不一样。

返回值:工做队列指针

2、经常使用接口说明

工做任务定义 工做任务添加 工做任务清除 工做任务取消
DECLARE_WORK() schedule_work() flush_work() cancel_work_sync()
DECLARE_DELAYED_WORK() schedule_delayed_work() flush_delayed_work() cancel_delayed_work() cancel_delayed_work_sync()
INIT_WORK() schedule_work() flush_work() cancel_work_sync()
INIT_DELAYED_WORK() schedule_delayed_work() flush_delayed_work() cancel_delayed_work() cancel_delayed_work_sync()
create_workqueue() queue_work() queue_delayed_work() queue_work_on() flush_workqueue() destroy_workqueue()
create_singlethread_workqueue() queue_work() flush_workqueue() destroy_workqueue()

注:

一、flush_work():堵塞工做任务,直到工做任务完成

二、flush_delayed_work():等待延时工做任务完成

三、cancel_work_sync():取消工做任务并等待它完成

四、cancel_delayed_work():取消延时工做任务

五、cancel_delayed_work_sync():取消延时工做任务并等待它完成

六、create_workqueue():对于多CPU系统,内核会在每一个CPU上建立一个工做队列,使线程处理并行化

七、create_singlethread_workqueue():内核只在一个CPU上建立一个工做队列

八、queue_work_on():在指定CPU上添加工做任务,queue_work()调用queue_work_on()在全部CPU上添加工做任务

3、接口使用举例

一、共享工做队列

文件:drivers/gpu/drm/drm_fb_helper.c,举例以下:

## 三、工做任务的具体实现
static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
{
 ...
}

## 一、定义工做任务,名称:drm_fb_helper_restore_work,实现函数:drm_fb_helper_restore_work_fn
static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);

static void drm_fb_helper_sysrq(int dummy1)
{
  ## 二、将drm_fb_helper_restore_work加入到全局工做队列
        schedule_work(&drm_fb_helper_restore_work);
}

其它接口使用方法相似。

二、自定义工做队列

文件:drivers/input/touchscreen/gt9xx/gt9xx.c

## 1.定义工做任务和工做队列
static struct delayed_work gtp_esd_check_work;
static struct workqueue_struct * gtp_esd_check_workqueue = NULL;

static int goodix_ts_init(void)
{
 ...
 ## 2.初始化工做任务gtp_esd_check_work; 建立工做队列gtp_esd_check_workqueue
    INIT_DELAYED_WORK(&gtp_esd_check_work, gtp_esd_check_func);
    gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
 ...
}

## 3.工做任务gtp_esd_check_work的实现函数
static void gtp_esd_check_func(struct work_struct *work)
{
 ...
}

void gtp_esd_switch(struct i2c_client *client, s32 on)
{
 ...
 ## 4.将工做任务gtp_esd_check_work添加到工做队列gtp_esd_check_workqueue,延时调度
    queue_delayed_work(gtp_esd_check_workqueue, &gtp_esd_check_work, ts->clk_tick_cnt);
    ...
    ## 5.等待延时任务完成
    cancel_delayed_work_sync(&gtp_esd_check_work);
    ...
}

static int goodix_ts_remove(struct i2c_client *client)
{
 ...
 ## 6.销毁工做队列gtp_esd_check_workqueue
    destroy_workqueue(gtp_esd_check_workqueue);
 ...
}

注:工做队列容许任务从新调度和睡眠。

相关文章
相关标签/搜索