Workqueue implementation in Linux Kernel

3.3k views Asked by At

Can any one help me to understand difference between below mentioned APIs in Linux kernel:

struct workqueue_struct *create_workqueue(const char *name); 
struct workqueue_struct *create_singlethread_workqueue(const char *name);

I had written sample modules, when I try to see them using ps -aef, both have created a workqueue, but I was not able to see any difference.

I have referred to http://www.makelinux.net/ldd3/chp-7-sect-6, and according to LDD3:

If you use create_workqueue, you get a workqueue that has a dedicated thread for each processor on the system. In many cases, all those threads are simply overkill; if a single worker thread will suffice, create the workqueue with create_singlethread_workqueue instead.

But I was not able to see multiple worker threads (each for a processor).

2

There are 2 answers

1
leesagacious On
if(singlethread){

    cwq = init_cpu_workqueue(wq, singlethread_cpu);
    err = create_workqueue_thread(cwq, singlethread_cpu);
    start_workqueue_thread(cwq, -1);

}else{

    list_add(&wq->list, &workqueues);
    for_each_possible_cpu(cpu) {    
                cwq = init_cpu_workqueue(wq, cpu);
                err = create_workqueue_thread(cwq, cpu);
                start_workqueue_thread(cwq, cpu);
    }
}
0
CL. On

Workqueues have changed since LDD3 was written.

These two functions are actually macros:

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

The alloc_workqueue documentation says:

Allocate a workqueue with the specified parameters. For detailed information on WQ_* flags, please refer to Documentation/workqueue.txt.

That file is too big to quote entirely, but it says:

alloc_workqueue() allocates a wq. The original create_*workqueue() functions are deprecated and scheduled for removal.
[...]
A wq no longer manages execution resources but serves as a domain for forward progress guarantee, flush and work item attributes.