linux等待队列(Linux wait queue)
linux等待队列(Linux wait queue)
The waiting queue of Linux kernel is based on double loop linked list, and is closely integrated with the process scheduling mechanism. It can be used to realize the core asynchronous event notification mechanism. In this list, there are two data structures: the waiting queue header (wait_queue_head_t) and the waiting queue item wait_queue_t. The waiting queue headers and waiting queue entries contain a domain of type list_head as the connector". Because we only need to queue to add and delete operations, and will not modify the object, so we only need to provide a protection for the entire infrastructure and the lock on the object, the lock in the wait queue head, wq_lock_t.
Waiting queues in the kernel have many uses, especially in dealing with interrupts, processes, synchronization, timing, and so on. You can use the wait queue in the wake of the blocking process. It is a queue based data structure and closely integrated with the process scheduling mechanism. Can be used to implement asynchronous event notification mechanisms in the kernel. Synchronize access to system resources.
Commonly used structures:
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;
Wait_queue_func_t func;
Struct list_head task_list;
};
Typedef struct __wait_queue wait_queue_t;
Field detail:
(1) spinlock_t lock;
In the operation of task_list, the mutex is used to achieve exclusive access to the waiting queue.
(2) struct, list_head, task_list;
Bidirectional circular list used to store waiting queues.
Common operations:
(1) define and initialize
Void init_waitqueue_head (wait_queue_head_t *q)
{
Spin_lock_init (&q->lock); / / initialize atomic operation
INIT_LIST_HEAD (&q->task_list); / / initialize list_head
}
Another method of using macros to initialize waiting queues:
D#define DECLARE_WAIT_QUEUE_HEAD (