feat: (ahci) support multiple AHCI controller
[lunaix-os.git] / lunaix-os / kernel / ds / waitq.c
index 5241a4ea9dff80bd64fb1f462ed4821cab9918f2..7045e36731cfc3db8adf5f68d6f099fa2ef244b8 100644 (file)
@@ -1,19 +1,22 @@
 #include <lunaix/ds/waitq.h>
 #include <lunaix/process.h>
-#include <lunaix/sched.h>
 #include <lunaix/spike.h>
 
 void
 pwait(waitq_t* queue)
 {
+    // prevent race condition.
+    cpu_disable_interrupt();
+
     waitq_t* current_wq = &__current->waitqueue;
     assert(llist_empty(&current_wq->waiters));
 
     llist_append(&queue->waiters, &current_wq->waiters);
 
-    // FIXME centralize the state change.
-    __current->state = PS_BLOCKED;
+    block_current();
     sched_yieldk();
+
+    cpu_enable_interrupt();
 }
 
 void
@@ -44,6 +47,7 @@ pwake_all(waitq_t* queue)
     {
         proc = container_of(pos, struct proc_info, waitqueue);
 
+        assert(proc->state == PS_BLOCKED);
         proc->state = PS_READY;
         llist_delete(&pos->waiters);
     }