integrate C/LDFLAGS into LunaBuild flow
[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     assert(current_thread);
10     // prevent race condition.
11     cpu_disable_interrupt();
12
13     waitq_t* current_wq = &current_thread->waitqueue;
14     assert(llist_empty(&current_wq->waiters));
15
16     llist_append(&queue->waiters, &current_wq->waiters);
17
18     block_current_thread();
19     sched_pass();
20
21     // In case of SIGINT-forced awaken
22     llist_delete(&current_wq->waiters);
23     cpu_enable_interrupt();
24 }
25
26 void
27 pwake_one(waitq_t* queue)
28 {
29     if (llist_empty(&queue->waiters)) {
30         return;
31     }
32
33     waitq_t* wq = list_entry(queue->waiters.next, waitq_t, waiters);
34     struct thread* thread = container_of(wq, struct thread, waitqueue);
35
36     assert(thread->state == PS_BLOCKED);
37     thread->state = PS_READY;
38     llist_delete(&wq->waiters);
39 }
40
41 void
42 pwake_all(waitq_t* queue)
43 {
44     if (llist_empty(&queue->waiters)) {
45         return;
46     }
47
48     struct thread* thread;
49     waitq_t *pos, *n;
50     llist_for_each(pos, n, &queue->waiters, waiters)
51     {
52         thread = container_of(pos, struct thread, waitqueue);
53
54         assert(thread->state == PS_BLOCKED);
55         thread->state = PS_READY;
56         llist_delete(&pos->waiters);
57     }
58 }