kernel work queue
使用方法
/************************************************************************************
** 头文件 **
*************************************************************************************/
#include <linux/workqueue.h>
/************************************************************************************
** 静态声明 **
*************************************************************************************/
static void _sm_msg_process_work(struct work_struct* work);
/************************************************************************************
** 静态变量 **
*************************************************************************************/
//创建工作队列
static struct workqueue_struct *SmWorkQueue;
//描述一个 work 的变量为MsgProcessWork,对应函数为 _sm_msg_process_work
static DECLARE_WORK(MsgProcessWork, _sm_msg_process_work);
/************************************************************************************
** 函数定义 **
*************************************************************************************/
static void _sm_msg_process_work(struct work_struct* work)
{
cl_debug_track("Start \r\n");
mdelay(5000);
cl_debug_track("Over \r\n");
}
void init(void)
{
//创建工作队列SmWorkQueue,名字为sm_msg_process
//实际上这是一个线程,线程名为sm_msg_process可通过ps查看该线程
// work是该线程的一个子程序
SmWorkQueue = create_singlethread_workqueue("sm_msg_process");
}
void test_run_work()
{
//将MsgProcessWork工作压入到 SmWorkQueue 队列队尾中
queue_work(SmWorkQueue, &MsgProcessWork);
}
注意
同一时间多次queue_work 来调度工作队列,并不会如愿以偿的执行多次。工作队列遵循以下原则
- 必须上一个work执行完成后,才会执行下一个同样的work
- 实测同时至多存在2个work