Let's get hands-on with a work queue! In the following sections, we will write a simple demo kernel module (ch5/workq_simple) that demonstrates using the kernel-default workqueue to execute a work task. It's actually built upon our earlier LKM, which we used to demonstrate kernel timers (ch5/timer_simple). Let's check it out code-wise (as usual, we won't show the full code here, only the most relevant portions). We'll begin by looking at its private context data structure and init method:
static struct st_ctx {
struct work_struct work;
struct timer_list tmr;
int data;
} ctx;
[ ... ]
static int __init workq_simple_init(void)
{
ctx.data = INITIAL_VALUE;
/* Initialize our work queue */
INIT_WORK(&ctx.work, work_func);
/* Initialize our kernel timer */
ctx.tmr.expires = jiffies + msecs_to_jiffies(exp_ms);
ctx.tmr.flags = 0;
timer_setup(&ctx.tmr, ding, 0);
...