Tasking
Work Queue
Allow kernel code to request that a function be called at some future time.
Basic usage is shown below:
Establish work queue:
struct workqueue_struct is needed which is defined in <linux/workqueue.h>.
There are two methods which can create the work queue:
struct workqueue_struct *create_workqueue(const char *name);
This function would return a workqueue that has a dedicated thread for each processor on the system.struct workqueue_struct *create_singlethread_workqueue(const char *name);
This function would return a workqueue that has a single worker thread.
After linux 2.6.63, there's a new mechanism called "Concurrency-managed workqueues", and there're two new function for creating the work queue:
alloc_ordered_workqueue
/** * alloc_ordered_workqueue - allocate an ordered workqueue * @name: name of the workqueue * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful) * * Allocate an ordered workqueue. An ordered workqueue executes at * most one work item at any given time in the queued order. They are * implemented as unbound workqueues with @max_active of one. * * RETURNS: * Pointer to the allocated workqueue on success, %NULL on failure. */
Submit a task to a workqueue
At compile time
DECLARE_WORK(name, void (*function)(void *), void *data);
At runtime
- Prepre the work_struct
INIT_WORK(struct work_struct *work, void (*function)(void *), void *data); PREPARE_WORK(struct work_struct *work, void (*function)(void *), void *data);
- Submitting work to a workqueue
int queue_work(struct workqueue_struct *queue, struct work_struct *work); int queue_delayed_work(struct workqueue_struct *queue, struct work_struct *work, unsigned long delay);
- Prepre the work_struct
reference: http://www.makelinux.net/ldd3/chp-7-sect-6