Linux Kernel 中 Workqueue 使用系统默认队列和创建队列的方法
关于workqueue,网上资料爆翻天。当然即便是这样,对此我们还是有很多话要说。想必大家对workqueue相关的函数(schedule_work 、queue_work、INIT_WORK、create_singlethread_workqueue 等)都不陌生。但说起差异,可能还有许多话需要坐下来慢慢讲。对于workqueue,与之最为相关的两个东西便是wor
static struct work_struct work;
{
Do something here!!
}
{
return queue_work( system_wq, work);
}
alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args)
#define create_workqueue(name) \
alloc_workqueue((name), WQ_MEM_RECLAIM, 1)
#define create_freezable_workqueue(name) \
alloc_workqueue((name), WQ_FREEZABLE | WQ_UNBOUND | WQ_MEM_RECLAIM, 1)
#define create_singlethread_workqueue(name) \
alloc_workqueue((name), WQ_UNBOUND | WQ_MEM_RECLAIM, 1)
__INIT_DELAYED_WORK(_work, _func, 0)
这个问题比较有意思,写了这个例子来验证答案(例子跑在android 4.4 code base中)
sample test code:
static struct workqueue_struct *test_wq;
static void try_to_test(struct work_struct *work)
{
printk(“[bevis] :wq into \n”);
msleep(5*1000); //5s
printk(“[bevis] :wq out \n”);
}
static DECLARE_WORK(mytest_work, try_to_test);
gsensor probe function end add :
test_wq = alloc_ordered_workqueue(“test_wq”, 0); //初始化一个单独的工作队列
int a = 0;
for(a=0 ; a<3 ; a++){
printk(“[bevis] : read func (%d) before \n”,a);
queue_work(test_wq, &mytest_work); //让这个队列开始被调度
printk(“[bevis] : read func (%d) msleep 2s \n”,a);
msleep(2*1000);
printk(“[bevis] : read func (%d) after \n”,a);
}
log如下:
10-16 14:10:41.940 I/KERNEL ( 109): [ 6.954658] [bevis] : read func (0) before
10-16 14:10:41.940 I/KERNEL ( 109): [ 6.954727] [bevis] : read func (0) msleep 2s
10-16 14:10:41.940 I/KERNEL ( 109): [ 6.954742] [bevis] :wq into
10-16 14:10:43.950 I/KERNEL ( 109): [ 8.960997] [bevis] : read func (0) after
10-16 14:10:43.950 I/KERNEL ( 109): [ 8.961085] [bevis] : read func (1) before
10-16 14:10:43.950 I/KERNEL ( 109): [ 8.961155] [bevis] : read func (1) msleep 2s
10-16 14:10:45.960 I/KERNEL ( 109): [ 10.971954] [bevis] : read func (1) after
10-16 14:10:45.960 I/KERNEL ( 109): [ 10.972076] [bevis] : read func (2) before
10-16 14:10:45.960 I/KERNEL ( 109): [ 10.972132] [bevis] : read func (2) msleep 2s
10-16 14:10:46.950 I/KERNEL ( 6): [ 11.961884] [bevis] :wq out
10-16 14:10:46.950 I/KERNEL ( 6): [ 11.961953] [bevis] :wq into
10-16 14:10:47.970 I/KERNEL ( 109): [ 12.982276] [bevis] : read func (2) after
10-16 14:10:51.960 I/KERNEL ( 6): [ 16.973719] [bevis] :wq out
更多推荐
所有评论(0)