You can use the declare_wait_queue_head (name) macro to define a new waiting queue header. It statically declares a waiting queue header variable named name and initializes the lock and task_list fields of the variable. The init_waitqueue_head () function can be used to initialize the dynamically allocated waiting queue header variable.
The init_waitqueue_entry (p, q) function initializes the variable q of the wait_queue_t structure as follows:
Q-> flags = 0;
Q-> task = P;
Q-> func = default_wake_function;
Non-mutex process P will be awakened by default_wake_function.
Once an element is defined, it must be inserted into the waiting queue. The add_wait_queque () function inserts a non-mutex process into the first position of the waiting queue linked list, add_wait_queue_exclusive () the function inserts a mutex process into the last position of the waiting queue linked list. The remove_wait_queue () function deletes an element from the waiting queue linked list. The waitqueue_active () function checks whether a given waiting queue is empty.
A process waiting for a specific condition can call any of the following functions:
1. sleep_on () is used to operate the current process.
Void sleep_on (wait_queue_head_t * WQ)
{
Wait_queue_twait;
Init_waitqueue_entry (& wait, current );
Current-> state = task_uninterruptible;
Add_wait_queue (WQ, & wait );
Schedule ();
Remove_wait_queue (WQ, & wait );
}
2. The interruptible_sleep_on () and sleep_on () functions are the same, but slightly different. The former sets the current process status to task_interruptible, and the latter to task_uninterruptible.
3. sleep_on_timeout () and interruptible_sleep_on_timeout () Allow the caller to define a time interval. After the time interval expires, the process will be awakened by the kernel.
4, prepare_to_wait (), prepare_to_wait_exclusive (), finish_wait ()
5. The wait_event and wait_event_interruptible macros enable their calling processes to wait for the queue to sleep until the conditions are modified.
The kernel uses any macro below to wake up the processes in the waiting queue and set their status to task_running.
Wake_up, wake_up_nr, wake_up_all, wake_up_interruptible, and wake_up_locked.
When a process is awakened, all mutex processes are at the end of the two-way linked list, and all non-mutex processes are at the beginning of the linked list, therefore, the function always wakes up non-mutex processes and then wakes up the mutex process.