X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/34f6af4f61e0eec9c96113e07f140b609b4113c8..refs/heads/cor/lrud:/lunaix-os/kernel/process/thread.c diff --git a/lunaix-os/kernel/process/thread.c b/lunaix-os/kernel/process/thread.c index 78cce8b..f4bbac0 100644 --- a/lunaix-os/kernel/process/thread.c +++ b/lunaix-os/kernel/process/thread.c @@ -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 = ¤t_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