dedicated kthread interface and enablement of lrud auto-recycler
[lunaix-os.git] / lunaix-os / kernel / process / thread.c
index 493ed99cc61bd2e6102b571ec9a5b523b3a3661e..f4bbac04cb0a2024c6c869e972f26757028e1f77 100644 (file)
@@ -9,8 +9,8 @@
 
 #include <usr/lunaix/threads.h>
 
-#include <sys/abi.h>
-#include <sys/mm/mm_defs.h>
+#include <asm/abi.h>
+#include <asm/mm_defs.h>
 
 LOG_MODULE("THREAD")
 
@@ -83,6 +83,35 @@ found:;
     return align_stack(ptep_va(ptep, LFT_SIZE) - 1);
 }
 
+static int
+__thread_putsleep(int seconds)
+{
+    if (!seconds) {
+        return 0;
+    }
+
+    struct scheduler* sched;
+    time_t systime;
+    struct haybed* bed;
+
+    sched = scheduler();
+    systime = clock_systime() / 1000;
+    bed = &current_thread->sleep;
+
+    if (bed->wakeup_time) {
+        return (bed->wakeup_time - systime);
+    }
+
+    bed->wakeup_time = systime + seconds;
+
+    if (llist_empty(&bed->sleepers)) {
+        llist_append(&sched->sleepers, &bed->sleepers);
+    }
+
+    block_current_thread();
+    return seconds;
+}
+
 void
 thread_release_mem(struct thread* thread)
 {
@@ -237,6 +266,25 @@ thread_stats_update(bool inbound, bool voluntary)
     stats->last_entry = now;
 }
 
+void
+kthread_spawn(ptr_t entry)
+{
+    assert(kernel_process(__current));
+
+    struct thread* th = create_thread(__current, false);
+    
+    assert(th);
+    start_thread(th, entry);
+    detach_thread(th);
+}
+
+void
+kthread_sleep(int seconds)
+{
+    if (__thread_putsleep(seconds))
+        yield_current();
+}
+
 __DEFINE_LXSYSCALL3(int, th_create, tid_t*, tid, 
                         struct uthread_param*, thparam, void*, entry)
 {
@@ -332,3 +380,16 @@ __DEFINE_LXSYSCALL2(int, th_kill, tid_t, tid, int, signum)
     
     return 0;
 }
+
+__DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
+{
+    int sec;
+
+    sec = __thread_putsleep(seconds);
+    store_retval(seconds);
+    
+    if (sec)
+        schedule();
+
+    return 0;
+}
\ No newline at end of file