fix: use wait queue for blocking process
[lunaix-os.git] / lunaix-os / kernel / ds / waitq.c
1 #include <lunaix/ds/waitq.h>
2 #include <lunaix/process.h>
3 #include <lunaix/spike.h>
4
5 void
6 pwait(waitq_t* queue)
7 {
8     waitq_t* current_wq = &__current->waitqueue;
9     assert(llist_empty(&current_wq->waiters));
10
11     llist_append(&queue->waiters, &current_wq->waiters);
12
13     block_current();
14     sched_yieldk();
15 }
16
17 void
18 pwake_one(waitq_t* queue)
19 {
20     if (llist_empty(&queue->waiters)) {
21         return;
22     }
23
24     waitq_t* wq = list_entry(queue->waiters.next, waitq_t, waiters);
25     struct proc_info* proc = container_of(wq, struct proc_info, waitqueue);
26
27     assert(proc->state == PS_BLOCKED);
28     proc->state = PS_READY;
29     llist_delete(&wq->waiters);
30 }
31
32 void
33 pwake_all(waitq_t* queue)
34 {
35     if (llist_empty(&queue->waiters)) {
36         return;
37     }
38
39     struct proc_info* proc;
40     waitq_t *pos, *n;
41     llist_for_each(pos, n, &queue->waiters, waiters)
42     {
43         proc = container_of(pos, struct proc_info, waitqueue);
44
45         proc->state = PS_READY;
46         llist_delete(&pos->waiters);
47     }
48 }