fix: the correct way to detect ahci LBA48 support
[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     // prevent race condition.
9     cpu_disable_interrupt();
10
11     waitq_t* current_wq = &__current->waitqueue;
12     assert(llist_empty(&current_wq->waiters));
13
14     llist_append(&queue->waiters, &current_wq->waiters);
15
16     block_current();
17     sched_yieldk();
18
19     cpu_enable_interrupt();
20 }
21
22 void
23 pwake_one(waitq_t* queue)
24 {
25     if (llist_empty(&queue->waiters)) {
26         return;
27     }
28
29     waitq_t* wq = list_entry(queue->waiters.next, waitq_t, waiters);
30     struct proc_info* proc = container_of(wq, struct proc_info, waitqueue);
31
32     assert(proc->state == PS_BLOCKED);
33     proc->state = PS_READY;
34     llist_delete(&wq->waiters);
35 }
36
37 void
38 pwake_all(waitq_t* queue)
39 {
40     if (llist_empty(&queue->waiters)) {
41         return;
42     }
43
44     struct proc_info* proc;
45     waitq_t *pos, *n;
46     llist_for_each(pos, n, &queue->waiters, waiters)
47     {
48         proc = container_of(pos, struct proc_info, waitqueue);
49
50         assert(proc->state == PS_BLOCKED);
51         proc->state = PS_READY;
52         llist_delete(&pos->waiters);
53     }
54 }