45.work_struct和delayed_work的工作队列使用

介绍

在中断处理中,经常用到工作队列,这样便能缩短中断处理时的时间

中断中通过调用schedule_work(work)来通知内核线程,然后中断结束后,再去继续执行work对应的func函数

示例

当中断来了,立马调用schedule_work(work),然后退出.

中断结束后,内核便会调用_work对应的func函数,最后才来读取按键值,上报按键值,这样就大大缩短了中断处理时间

 

work_struct之相关函数

1.

INIT_WORK(work, func);

其中参数1是个work_struct结构体,参数2是个函数名,通过INIT_WORK将work_struct与一个函数建立起来.

其中work_struct结构体定义如下所示:

struct work_struct {
         atomic_long_t data;                       
         struct list_head entry;                     
         work_func_t func;                            //函数指针,指向func函数
#ifdef CONFIG_LOCKDEP
         struct lockdep_map lockdep_map;
#endif
};

2.

schedule_work(work);

通知内核线程,在后续的时间里,系统将会自动调用work结构体对应的func函数

 3.

bool cancel_work_sync(struct work_struct *work);

取消work结构体对应的func函数,一般在exit中使用

接下来,在这个链接,将会在中断里用到它们: https://www.cnblogs.com/lifexy/p/9629699.html

delayed_work之相关函数

在上个链接,我们只是讲了work_struct如何使用
而在内核中,除了work_struct外,还有一个结构体delayed_work,该工作队列里拥有一个timer定时器结构体,从而实现延时工作,其中delayed_work结构体如下所示:

该结构体相关函数如下所示:

INIT_DELAYED_WORK(dwork, work);    
//参数1是个delayed_work结构体,参数2是个函数名
schedule_delayed_work (struct delayed_work *dwork, unsigned long delay); //将当前dwork变量进入到等待队列中,在后续的delay时间后,将会调用dwork变量对应的work函数

bool cancel_delayed_work(struct delayed_work *dwork); //如果当前dwork在等待队列中,则将取消掉
原文地址:https://www.cnblogs.com/lifexy/p/9629380.html