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