Kernel waiting queue
Kernel waiting queue
Creator sz111@126.com
1.
Waiting queue plays an extremely important role in the kernel. As an asynchronous operation, its implementation is simple and powerful.
It uses a double-stranded table to link the waiting tast header to the waiting process list. You can see it clearly. So we know that to implement a waiting queue, there must be two parts first. Queue header and queue item. Let's look at their data structure.
Struct list_head {
Struct list_head * Next, * Prev;
};
Struct _ wait_queue_head {
Spinlock_t lock;
Struct list_head task_list;
};
Typedef struct _ wait_queue_head wait_queue_head_t;
Struct _ wait_queue {
Unsigned int flags;
# Define wq_flag_exclusive 0x01
Void * private; // version 2.6 adopts the void pointer, while the previous version is struct task_struct * task;
// In actual use, the private value is still assigned to the task.
Wait_queue_func_t func;
Struct list_head task_list;
};
Typedef struct _ wait_queue wait_queue_t;
Therefore, the queue headers and queue items are linked through list_head, which is a two-way linked list and is widely used in Linux kernel. In addition, list. H has many operations on it.
2. initialize the column header and queue items:
/*
* Macros for declaration and initialisaton of the datatypes
*/
# DEFINE _ waitqueue_initializer (name, tsk ){\
. Private = Tsk ,\
. Func = default_wake_function ,\
. Task_list = {null, null }}
// This is the initialization of a queue item. It is set to be empty before and after the tast_list chain table. It indicates that it has not been added to the chain table.
// Private data is the task structure of the task.
// This macro description and initialization are both done. If you do not want to do this, you can first declare and then pass
// Init_waitqueue_entry is complete.
# Define declare_waitqueue (name, tsk )\
Wait_queue_t name = _ waitqueue_initializer (name, tsk)
# DEFINE _ wait_queue_head_initializer (name ){\
. Lock = _ spin_lock_unlocked (name. Lock ),\
. Task_list = {& (name). task_list, & (name). task_list }}
// Declare a queue header so that its linked list points to itself. At this time, no linked list items have been added.
// This macro description and initialization are both done. If you do not want to do this, you can first declare and then pass
// Init_waitqueue_head.
# Define declare_wait_queue_head (name )\
Wait_queue_head_t name = _ wait_queue_head_initializer (name)
# DEFINE _ wait_bit_key_initializer (word, bit )\
{. Flags = word,. bit_nr = bit ,}
Extern void init_waitqueue_head (wait_queue_head_t * q );
Static inline void init_waitqueue_entry (wait_queue_t * q, struct task_struct * P)
{
Q-> flags = 0;
Q-> private = P;
Q-> func = default_wake_function;
}
// Add the specified waiting queue item new to the head of the linked list where the waiting queue head is located. This function assumes that the lock has been obtained.
Static inline void _ add_wait_queue (wait_queue_head_t * head, wait_queue_t * New)
{
List_add (& New-> task_list, & head-> task_list );
}
/*
* Used for wake-one threads:
*/
// Add the specified waiting queue item new to the end of the linked list where the waiting queue head is located. This function is assumed to have obtained the lock.
// In fact, because the queue is a circular queue, the head is the head, and the head can be regarded as the end, of course, there is nothing in the ring. // it is the end.
Static inline void _ add_wait_queue_tail (wait_queue_head_t * head,
Wait_queue_t * New)
{
List_add_tail (& New-> task_list, & head-> task_list );
}
Static inline void _ remove_wait_queue (wait_queue_head_t * head,
Wait_queue_t * old)
{
List_del (& old-> task_list );
}
3. sleep and wake-up operations.
Operations on the waiting queue include sleep and wakeup (related functions are stored in/kernel/sched. C and include/Linux/sched. H of the source code tree ). Thinking is more
Change the status of the current task and request rescheduling. Because the status of this process has changed and is no longer in the ready queue of the scheduling table, the execution opportunity cannot be obtained, enter"
Sleep state until it is "awakened", that is, its task status is changed back to the ready state.
Common sleep operations include interruptible_sleep_on and
Sleep_on. The two functions are similar, except that the former sets the Process status from the ready state (task_running) to task_interruptible and allows
Send signal to wake it up (a sleep state that can be interrupted), and the latter sets the Process status to task_uninterruptible, in which case, no
Singal. In general, we need to adopt a sleep that can be interrupted.
The interruptible_sleep_on macro code is as follows:
// First define the queue items, and then initialize the queue items. The task structure is set to the task structure of the current process.
# Define sleep_on_var \
Unsigned long flags ;\
Wait_queue_t wait ;\
Init_waitqueue_entry (& wait, current );
// Add the queue items to the queue header.
# Define sleep_on_head \
Spin_lock_irqsave (& Q-> lock, flags );\
_ Add_wait_queue (Q, & wait );\
Spin_unlock (& Q-> lock );
// After waking up, delete the queue items from the queue header.
# Define sleep_on_tail \
Spin_lock_irq (& Q-> lock );\
_ Remove_wait_queue (Q, & wait );\
Spin_unlock_irqrestore (& Q-> lock, flags );
Void fastcall _ sched interruptible_sleep_on (wait_queue_head_t * q)
{
Sleep_on_var
Current-> state = task_interruptible;
Sleep_on_head
Schedule ();
Sleep_on_tail
}
The corresponding wake-up operations include wake_up_interruptible and wake_up. The wake_up function can not only wake up in
Task_uninterruptible process, and can wake up the process in the task_interruptible state.
Wake_up_interruptible is only responsible for awakening the process in the task_interruptible state. The two macros are defined as follows:
# Define wake_up (x) _ wake_up (x), task_uninterruptible | task_interruptible, 1, null)
# Define wake_up_interruptible (x) _ wake_up (x), task_interruptible, 1, null)
The _ wake_up function is mainly used to obtain the Lock of queue operations. The specific task is to call _ wake_up_common.
/**
* _ Wake_up-wake up threads blocked on a waitqueue.
* @ Q: the queue header of the waitqueue wake-up queue
* @ Mode: Which threads wake up the type of waiting queue, such as: can be interrupted and can not be interrupted
* @ Nr_exclusive: How many wake-one or wake-up threads to wake up
* @ Key: is directly passed to the Wakeup function
*/
Void fastcall _ wake_up (wait_queue_head_t * q, unsigned int mode,
Int nr_exclusive, void * key)
{
Unsigned long flags;
Spin_lock_irqsave (& Q-> lock, flags );
_ Wake_up_common (Q, mode, nr_exclusive, 0, key );
Spin_unlock_irqrestore (& Q-> lock, flags );
}
/*
* The core Wakeup function. Non-exclusive wakeups (nr_exclusive = 0) Just
* Wake everything up. If it's an exclusive wakeup (nr_exclusive = Small + ve
* Number) Then we wake all the non-exclusive tasks and one exclusive task.
*
* There are circumstances in which we can try to wake a task which has already
* Started to run but is not in State task_running. try_to_wake_up () returns
* Zero in this (rare) case, And we handle it by continuing to scan the queue.
*/
Static void _ wake_up_common (wait_queue_head_t * q, unsigned int mode,
Int nr_exclusive, int sync, void * key)
{
Struct list_head * TMP, * next;
List_for_each_safe (TMP, next, & Q-> task_list ){
Wait_queue_t * curr = list_entry (TMP, wait_queue_t, task_list );
Unsigned flags = curr-> flags;
// The func here is actually
// Int default_wake_function (wait_queue_t * curr, unsigned mode, int sync,
// Void * key)
// {Wake up a process and put it in the running queue if it is not in the running queue. The "current" process is always in the running queue
// Return try_to_wake_up (curr-> private, mode, sync );
//}
If (curr-> func (curr, mode, sync, key )&&
(Flags & wq_flag_exclusive )&&! -- Nr_exclusive)
Break;
}
}
4. Applications waiting for the queue
The application waiting for the queue involves two processes, A and B. A is the consumer of resources, and B is the producer of resources. A must ensure that the resource has been produced during consumption, so define a resource waiting queue. This queue is used by both process a and process B. We can define it as a global variable.
Declare_wait_queue_head (rsc_queue);/* global variable */
In process A, the execution logic is as follows:
While (resource is unavaiable ){
Interruptible_sleep_on (& WQ );
}
Consume_resource ();
In process B, the execution logic is as follows:
Produce_resource ();
Wake_up_interruptible (& WQ );
This article is from the chinaunix blog, If you view the original, click: http://blog.chinaunix.net/u2/84137/showart_1667870.html