Second Extended Filesystem (ext2) and other improvements (#33)
[lunaix-os.git] / lunaix-os / kernel / ds / waitq.c
index caed3bc6f498a11a705c990d060aab2637d5081f..c1028828543d4d39dd081ab5f00f9b0c93a5d33f 100644 (file)
@@ -1,23 +1,54 @@
 #include <lunaix/ds/waitq.h>
 #include <lunaix/process.h>
 #include <lunaix/ds/waitq.h>
 #include <lunaix/process.h>
+#include <lunaix/sched.h>
 #include <lunaix/spike.h>
 #include <lunaix/spike.h>
+#include <lunaix/kpreempt.h>
 
 
-void
-pwait(waitq_t* queue)
+static inline void must_inline
+__try_wait(bool check_stall) 
+{
+    unsigned int nstall;
+    waitq_t* current_wq = &current_thread->waitqueue;
+    if (waitq_empty(current_wq)) {
+        return;
+    }
+    
+    block_current_thread();
+
+    if (!check_stall) {
+        // if we are not checking stall, we give up voluntarily
+        yield_current();
+    } else {
+        // otherwise, treat it as being preempted by kernel
+        preempt_current();
+    }
+
+    // In case of SIGINT-forced awaken
+    llist_delete(&current_wq->waiters);
+}
+
+static inline void must_inline
+__pwait(waitq_t* queue, bool check_stall)
 {
 {
-    assert(__current);
     // prevent race condition.
     // prevent race condition.
-    cpu_disable_interrupt();
+    no_preemption();
 
 
-    waitq_t* current_wq = &__current->waitqueue;
-    assert(llist_empty(&current_wq->waiters));
+    prepare_to_wait(queue);
+    __try_wait(check_stall);
 
 
-    llist_append(&queue->waiters, &current_wq->waiters);
+    set_preemption();
+}
 
 
-    block_current();
-    sched_yieldk();
+void
+pwait(waitq_t* queue)
+{
+    __pwait(queue, false);
+}
 
 
-    cpu_enable_interrupt();
+void
+pwait_check_stall(waitq_t* queue)
+{
+    __pwait(queue, true);
 }
 
 void
 }
 
 void
@@ -28,10 +59,10 @@ pwake_one(waitq_t* queue)
     }
 
     waitq_t* wq = list_entry(queue->waiters.next, waitq_t, waiters);
     }
 
     waitq_t* wq = list_entry(queue->waiters.next, waitq_t, waiters);
-    struct proc_info* proc = container_of(wq, struct proc_info, waitqueue);
+    struct thread* thread = container_of(wq, struct thread, waitqueue);
 
 
-    assert(proc->state == PS_BLOCKED);
-    proc->state = PS_READY;
+    assert(thread->state == PS_BLOCKED);
+    thread->state = PS_READY;
     llist_delete(&wq->waiters);
 }
 
     llist_delete(&wq->waiters);
 }
 
@@ -42,14 +73,40 @@ pwake_all(waitq_t* queue)
         return;
     }
 
         return;
     }
 
-    struct proc_info* proc;
+    struct thread* thread;
     waitq_t *pos, *n;
     llist_for_each(pos, n, &queue->waiters, waiters)
     {
     waitq_t *pos, *n;
     llist_for_each(pos, n, &queue->waiters, waiters)
     {
-        proc = container_of(pos, struct proc_info, waitqueue);
+        thread = container_of(pos, struct thread, waitqueue);
 
 
-        assert(proc->state == PS_BLOCKED);
-        proc->state = PS_READY;
+        if (thread->state == PS_BLOCKED) {
+            thread->state = PS_READY;
+        }
+        
+        // already awaken or killed by other event, just remove it
         llist_delete(&pos->waiters);
     }
         llist_delete(&pos->waiters);
     }
-}
\ No newline at end of file
+}
+
+void
+prepare_to_wait(waitq_t* queue)
+{
+    assert(current_thread);
+    
+    waitq_t* current_wq = &current_thread->waitqueue;
+    assert(llist_empty(&current_wq->waiters));
+
+    llist_append(&queue->waiters, &current_wq->waiters);
+}
+
+void
+try_wait()
+{
+    __try_wait(false);
+}
+
+void
+try_wait_check_stall()
+{
+    __try_wait(true);
+}