1 #include <common/wait_queue.h>
2 #include <process/process.h>
3 #include <sched/sched.h>
4 
5 /**
6  * @brief 初始化等待队列
7  *
8  * @param wait_queue 等待队列
9  * @param pcb pcb
10  */
wait_queue_head_init(wait_queue_head_t * wait_queue)11 void wait_queue_head_init(wait_queue_head_t *wait_queue)
12 {
13     list_init(&wait_queue->wait_list);
14     spin_init(&wait_queue->lock);
15 }
16 
17 /**
18  * @brief 在等待队列上进行等待, 但是你需要确保wait已经被init, 同时wakeup只能使用wake_up_on_stack函数。
19  *
20  * @param wait_queue_head 队列头指针
21  */
wait_queue_sleep_with_node(wait_queue_head_t * q,wait_queue_node_t * wait)22 void wait_queue_sleep_with_node(wait_queue_head_t *q, wait_queue_node_t *wait)
23 {
24     BUG_ON(wait->pcb == NULL);
25 
26     wait->pcb->state = PROC_UNINTERRUPTIBLE;
27     list_append(&q->wait_list, &wait->wait_list);
28 
29     sched();
30 }
31 
32 /**
33  * @brief 在等待队列上进行等待,同时释放自旋锁, 但是你需要确保wait已经被init, 同时wakeup只能使用wake_up_on_stack函数。
34  *
35  * @param wait_queue_head 队列头指针
36  */
wait_queue_sleep_with_node_unlock(wait_queue_head_t * q,wait_queue_node_t * wait,void * lock)37 void wait_queue_sleep_with_node_unlock(wait_queue_head_t *q, wait_queue_node_t *wait, void *lock)
38 {
39     BUG_ON(wait->pcb == NULL);
40 
41     wait->pcb->state = PROC_UNINTERRUPTIBLE;
42     list_append(&q->wait_list, &wait->wait_list);
43     spin_unlock((spinlock_t *)lock);
44 
45     sched();
46 }
47 
48 /**
49  * @brief 在等待队列上进行等待(允许中断), 但是你需要确保wait已经被init, 同时wakeup只能使用wake_up_on_stack函数。
50  *
51  * @param wait_queue_head 队列头指针
52  */
wait_queue_sleep_with_node_interriptible(wait_queue_head_t * q,wait_queue_node_t * wait)53 void wait_queue_sleep_with_node_interriptible(wait_queue_head_t *q, wait_queue_node_t *wait)
54 {
55     BUG_ON(wait->pcb == NULL);
56 
57     wait->pcb->state = PROC_INTERRUPTIBLE;
58     list_append(&q->wait_list, &wait->wait_list);
59 
60     sched();
61 }
62 
63 /**
64  * @brief 唤醒在等待队列的头部的进程, 但是不会free掉这个节点的空间(默认这个节点在栈上创建)
65  *
66  * @param wait_queue_head
67  * @param state
68  */
wait_queue_wakeup_on_stack(wait_queue_head_t * q,int64_t state)69 void wait_queue_wakeup_on_stack(wait_queue_head_t *q, int64_t state)
70 {
71     if (list_empty(&q->wait_list))
72         return;
73 
74     wait_queue_node_t *wait = container_of(list_next(&q->wait_list), wait_queue_node_t, wait_list);
75 
76     // 符合唤醒条件
77     if (wait->pcb->state & state)
78     {
79         list_del_init(&wait->wait_list);
80         process_wakeup_immediately(wait->pcb);
81     }
82 }