Working queue in Linux

Source: Internet
Author: User

From: http://tanatseng.blog.163.com/blog/static/174991629201132734828701/

A work queue is generally used for delayed work. For example, you can do a lot of work in the interrupt, but it is time-consuming. In this case, you can put the time-consuming work in the work queue. To put it bluntly, It is a custom function for system latency scheduling.

 
Working queue is a new mechanism for implementing latency. It is provided from the Linux kernel 2.5. Unlike the one-step delay method of micro-threads, the working queue adopts a common delay mechanism, and the processing program function of the working queue can sleep (which cannot be implemented in the micro-thread mode ). The work queue can have a higher latency than the micro-thread, and provide more functions for the task delay API. In the past, the latency function was implemented through keventd to queue tasks, but now it is managed by kernel worker threads events/X.
 
The work queue provides a common way to delay tasks to bottom halves. At the core is the work queue (struct workqueue_struct), where tasks are arranged. The task is described by the struct work_struct to identify which tasks are delayed and which delay functions are used (see figure 3 ). Events/X Kernel threads (one per CPU) extract tasks from the working queue and activate a bottom-half handler (specified by the handler function in the struct work_struct ).
 
 
Process behind the work queue
 
 
As work_struct indicates the handler function to be used, the work queue can be used to queue tasks for different handlers. Now, let's take a look at the API functions that can be used in the work queue.
 
Work queue API
 
The working queue API is slightly more complex than the microthread because it supports many options. We will first discuss the work queue, and then look at the tasks and variants.
 
Figure 3 shows that the core structure of the working queue is the queue itself. This struct is used to arrange the task out of top half and enter bottom half, thus delaying its execution. The work queue generates create_workqueue through macro calls and returns a workqueue_struct reference value. When a work queue is used up, you can call the destroy_workqueue function to remove it (if needed ):
 
Struct workqueue_struct * create_workqueue (name );
Struct workqueue_struct * create_singlethread_workqueue (const char * Name );
Void destroy_workqueue (struct workqueue_struct *);
 
A work queue must be explicitly created before use. If create_workqueue is used, a work queue is obtained, which has a dedicated thread on each processor of the system. In many cases, multithreading affects the system. If a single thread is sufficient, create_singlethread_workqueue is used to create a work queue.
Tasks that communicate with a work queue can be defined by the work_struct struct. Generally, this struct is the first element of the struct used for task definition (in the following example ). The work queue API provides three functions to initialize a task (through a pre-allocated cache). For details, see Listing 6. Macro init_work provides the required initialization data and the configuration of handler functions (transmitted by the user ). If a developer needs to delay before the task is put into the work queue, use the macros init_delayed_work and init_delayed_work_deferrable.
 
Listing 6. Task initialization macro

Init_work (work, func );
Init_delayed_work (work, func );
Init_delayed_work_deferrable (work, func );
 
Init _ * performs more comprehensive initialization of the structure, which is used when the structure is created for the first time.
After the initialization of the task struct is complete, you need to arrange the task into the work queue. You can perform this operation in multiple ways (see listing 7 ). First, use queue_work to simply arrange the task into the work queue (which binds the task to the current CPU ). Alternatively, you can use queue_work_on to specify the CPU on which the processing program runs. The two additional functions provide the same functionality for delayed tasks (their struct is loaded into the struct work_struct, and a timer is used for task latency ).
 
Listing 7. Work queue Functions

Int queue_work (struct workqueue_struct * WQ, struct work_struct * work );
Int queue_work_on (int cpu, struct workqueue_struct * WQ, struct work_struct * work );
Int queue_delayed_work (struct workqueue_struct * WQ,
Struct delayed_work * dwork, unsigned long delay );
Int queue_delayed_work_on (int cpu, struct workqueue_struct * WQ,
Struct delayed_work * dwork, unsigned long delay );
 
Each worker is added to the specified workqueue. If queue_delay_work is used, the actual work must go through the specified jiffies at least to be executed. If 1 is returned for these functions, the work is successfully added to the queue. If 0, it means that the work has been waiting in the queue and cannot be added again.
You can use the global kernel global work queue and four functions to locate the work queue. These functions (see listing 8) simulate listing 7, but do not need to define the work queue struct.
 
Listing 8. kernel global work queue Functions

Int schedule_work (struct work_struct * work );
Int schedule_work_on (int cpu, struct work_struct * work );
Int scheduled_delayed_work (struct delayed_work * dwork, unsigned long delay );
Int scheduled_delayed_work_on (
Int CPU, struct delayed_work * dwork, unsigned long delay );
 
Some help functions are used to clear or cancel tasks in the work queue. To clear a specific task project and block the task until the task is completed, call flush_work. All tasks in the specified work queue can be completed by calling flush_workqueue. In both cases, the caller is blocked until the operation is completed. To clear the global kernel work queue, you can call flush_scheduled_work.
 
Int flush_work (struct work_struct * work );
Int flush_workqueue (struct workqueue_struct * WQ );
Void flush_scheduled_work (void );
 
After flush_workqueue is returned, the functions submitted before this call are not run anywhere in the system.
Tasks not executed in the processing program can be canceled. Calling cancel_work_sync will terminate the tasks in the queue or block the tasks until the callback ends (if the handler is already processing the task ). If the task is delayed, you can call cancel_delayed_work_sync.
 
Int cancel_work_sync (struct work_struct * work );
Int cancel_delayed_work_sync (struct delayed_work * dwork );
 
Finally, you can call work_pending or delayed_work_pending to determine whether the task project is in progress.
 
Work_pending (work );
Delayed_work_pending (work );
 
This is the core of the work queue API. You can find the implementation method of the work queue API in./kernel/workqueue. C. The API is defined in./include/Linux/workqueue. h. The following is a simple example of a working queue API.
 
Simple example of working queue
 
The following example describes several core workflow queue API functions. As with the microthread example, you can deploy this example in the kernel module context for convenience.
 
First, let's take a look at the task struct and handler functions used to implement bottom half (see listing 9 ). First, you will notice the definition of the work queue struct reference (my_wq) and my_work_t. The header of the my_work_t type definition includes the struct work_struct and an integer representing the task project. The handler (callback function) changes the reference of the work_struct pointer to the my_work_t type. After a task item (an integer from the struct) is sent, the task pointer is released.
 
Listing 9. Task struct and bottom-half Handler

# Include <Linux/kernel. h>
# Include <Linux/module. h>
# Include <Linux/workqueue. h>
 
Module_license ("GPL ");
 
Static struct workqueue_struct * my_wq;
 
Typedef struct {
Struct work_struct my_work;
Int X;
} My_work_t;
 
My_work_t * Work, * work2;
 
Static void my_wq_function (struct work_struct * Work)
{
My_work_t * my_work = (my_work_t *) work;
Printk ("my_work.x % d \ n", my_work-> X );
Kfree (void *) work );
Return;
}
 
Listing 10 is the init_module function, which starts from generating a work queue using the create_workqueue API function. After a work queue is successfully generated, two task projects are created (allocated through kmalloc ). Use init_work to initialize each task project, define the task, and then schedule the task to the work queue by calling queue_work. The top-half process (simulated here) is completed. As shown in listing 10, tasks are sometimes processed later by the processing program.
 
 
Listing 10. Creating a work queue and a task

Int init_module (void)
{
Int ret;
 
My_wq = create_workqueue ("my_queue ");
If (my_wq ){
 
/* Queue some work (item 1 )*/
Work = (my_work_t *) kmalloc (sizeof (my_work_t), gfp_kernel );
If (work ){
Init_work (struct work_struct *) work, my_wq_function );
Work-> x = 1;
Ret = queue_work (my_wq, (struct work_struct *) work );
}
 
/* Queue some additional work (item 2 )*/
Work2 = (my_work_t *) kmalloc (sizeof (my_work_t), gfp_kernel );
If (work2 ){
Init_work (struct work_struct *) work2, my_wq_function );
Work2-> X = 2;
Ret = queue_work (my_wq, (struct work_struct *) work2 );
}
}
 
Return 0;
}
 
The final elements are displayed in listing 11. During the module cleanup process, some special work queues will be cleared (they will remain congested until the processing program completes processing the task), and then the work queue will be destroyed.
 
Listing 11. Work queue cleaning and destruction

Void cleanup_module (void)
{
Flush_workqueue (my_wq );
Destroy_workqueue (my_wq );
Return;
}
 
Refer:
Kernel working queue usage
 
A work queue is generally used for delayed work. For example, you can do a lot of work in the interrupt, but it is time-consuming. In this case, you can put the time-consuming work in the work queue. To put it bluntly, It is a custom function for system latency scheduling.
 
1. Define struct work_struct irq_queue;
 
2. initialize init_work (& irq_queue, do_irq_queuework );
 
3. Call method: schedule_work (& rq_queue );
 
Note: after the call is completed, the system releases this function. If you want to execute the function again, call schedule_work () again.
 
In addition, a file system must be mounted to the kernel to use the Working queue. My understanding is: The work queue is also scheduled. If the kernel crashes, it will not be scheduled, and of course it will not be used.
 
Work queue Interface
The work queue interface is introduced in the development process of 2.5 and is used to replace the task queue interface (used to schedule kernel tasks ). Each work queue has a dedicated thread,
All tasks from the running queue run in the context of the process (so that they can sleep ). Drivers can create and use their own work queues, or use a work queue of the kernel.
Create a work queue as follows:
Struct workqueue_struct * create_workqueue (const char * Name); here name is the name of the work queue.
 
A work queue task can be created at compilation or runtime. The task needs to be encapsulated into a struct called work_struct. It is used to initialize a work queue task during compilation:
Declare_work (name, void (* function) (void *), void * data); here name is the name of work_struct, and function is the function called when the task is scheduled, data is a pointer to that function.
 
It is used to initialize a work queue at runtime:
Init_work (struct work_struct * Work, void (* function) (void *), void * data );
 
Use the following function call to add a job (A Job Queue job/task in the work_struct structure) to the work queue:
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 );
Specify delay in queue_delay_work () to ensure that tasks in the work queue can be actually executed at least after a specified minimum delay.
 
Tasks in the work queue are executed by related working threads, which may be at an unexpected time (depending on the load, interruption, etc.) or after a delay. To cancel a task that has been waiting for an infinite amount of time in the work queue and has not been running, use the following method:
Int cancel_delayed_work (struct work_struct * work );
 
If the task is being executed when a call to cancel the operation is returned, the task will continue to be executed, but will not be added to the queue. Clear all tasks in the work queue:
Void flush_workqueue (struct workqueue_struct * Queue );

Destruction queue usage:
Void destroy_workqueue (struct workqueue_struct * Queue );
Not all drivers must have their own work queues. The driver can use the default working queue provided by the kernel. Because this work queue is shared by many drivers,
The task may take a long time to start execution. To solve this problem, the latency in the working function should be minimized or not at all.
 
Note that the default queue is available for all drivers, but only drivers licensed by GP can use custom work queues:
Int schedule_work (struct work_struct * work); -- add a task to the work queue
Int schedule_delayed_work (struct work_struct * Work, unsigned long delay); -- add a task to the work queue and delay execution
 
When the module is being loaded, call a flash_scheduled_work () function, which will wait for all tasks in the queue to be executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.