feat: serial device interfacing
[lunaix-os.git] / lunaix-os / kernel / ds / waitq.c
index 0d643d02e7de3649d4da25d50c7b9d41258cc4c1..caed3bc6f498a11a705c990d060aab2637d5081f 100644 (file)
@@ -5,14 +5,19 @@
 void
 pwait(waitq_t* queue)
 {
+    assert(__current);
+    // 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
@@ -43,6 +48,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);
     }