Today I want to talk about work queues.
Work queues are used in situations when the caller cannot do the intended action itself, for instance because it is an interrupt service routine and the work is too long for an interrupt, or is otherwise inappropriate to run in an interrupt (because it requires a process context).
Your code that should run in a later time is called a "work".
"Work" is some action that should complete in a reasonable time, because multiple work items share the the same worker thread.
When you want to use the work queue mechanism, you have 3 options to create a kernel thread:
- singlethread_workqueue() - creates a work queue on a single CPU.
- create_workqueue() - To create one worker thread per CPU in the system.
- Use the default work queue. These are per-CPU worker threads, which were created at the time of boot up, that you can timeshare.
Example code:
#include <linux/workqueue.h>
struct workqueue_struct *wq;
static int __init mymodule_init(void) { /* ... */ wq = create_singlethread_workqueue("my_wq"); return 0; } int add_work_to_my_wq(void (*func)(void *data), void *data) { struct work_struct *some_work; some_work = kmalloc(sizeof(struct work_struct), GFP_KERNEL); /* Init the work structure */ INIT_WORK(some_work, func, data); /* Enqueue Work */ queue_work(wq, some_work); return 0; }
User may submit work to a work queue with a delay request, using:
int queue_delayed_work(struct workqueue_struct *queue,
struct work_struct *work, unsigned long delay);
NOTE: Work queues API is only available to modules which are declared under GPL license, using:
MODULE_LICENSE("GPL");
No comments:
Post a Comment