feat: asynchronized SATA IO
[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     // FIXME centralize the state change.
14     __current->state = PS_BLOCKED;
15     sched_yieldk();
16 }
17
18 void
19 pwake_one(waitq_t* queue)
20 {
21     if (llist_empty(&queue->waiters)) {
22         return;
23     }
24
25     waitq_t* wq = list_entry(queue->waiters.next, waitq_t, waiters);
26     struct proc_info* proc = container_of(wq, struct proc_info, waitqueue);
27
28     assert(proc->state == PS_BLOCKED);
29     proc->state = PS_READY;
30     llist_delete(&wq->waiters);
31 }
32
33 void
34 pwake_all(waitq_t* queue)
35 {
36     if (llist_empty(&queue->waiters)) {
37         return;
38     }
39
40     struct proc_info* proc;
41     waitq_t *pos, *n;
42     llist_for_each(pos, n, &queue->waiters, waiters)
43     {
44         proc = container_of(pos, struct proc_info, waitqueue);
45
46         proc->state = PS_READY;
47         llist_delete(&pos->waiters);
48     }
49 }