X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/69777bdcab284335651a8002e2896f3862fa423d..refs/heads/cor/lrud:/lunaix-os/kernel/process/sched.c diff --git a/lunaix-os/kernel/process/sched.c b/lunaix-os/kernel/process/sched.c index bdd9736..ccf0e45 100644 --- a/lunaix-os/kernel/process/sched.c +++ b/lunaix-os/kernel/process/sched.c @@ -1,8 +1,7 @@ -#include -#include +#include +#include -#include -#include +#include #include #include @@ -18,20 +17,22 @@ #include #include #include -#include +#include #include #include struct thread empty_thread_obj; -volatile struct proc_info* __current; +volatile struct proc_info* __current = NULL; volatile struct thread* current_thread = &empty_thread_obj; struct scheduler sched_ctx; struct cake_pile *proc_pile ,*thread_pile; +#define root_process (sched_ctx.procs[1]) + LOG_MODULE("SCHED") void @@ -59,6 +60,7 @@ run(struct thread* thread) set_current_executing(thread); switch_context(); + fail("unexpected return from switching"); } @@ -69,10 +71,9 @@ run(struct thread* thread) clean-up on these thread, in the preemptible kernel thread. */ -void _preemptible -cleanup_detached_threads() { - ensure_preempt_caller(); - +void +cleanup_detached_threads() +{ // XXX may be a lock on sched_context will ben the most appropriate? cpu_disable_interrupt(); @@ -99,13 +100,22 @@ cleanup_detached_threads() { cpu_enable_interrupt(); } -int +bool can_schedule(struct thread* thread) { if (!thread) { return 0; } + if (proc_terminated(thread)) { + return false; + } + + if (preempt_check_stalled(thread)) { + thread_flags_set(thread, TH_STALLED); + return true; + } + if (unlikely(kernel_process(thread->process))) { // a kernel process is always runnable return thread->state == PS_READY; @@ -116,6 +126,7 @@ can_schedule(struct thread* thread) if ((thread->state & PS_PAUSED)) { return !!(sh->sig_pending & ~1); } + if ((thread->state & PS_BLOCKED)) { return sigset_test(sh->sig_pending, _SIGINT); } @@ -125,8 +136,9 @@ can_schedule(struct thread* thread) // all other threads are also SIGSTOP (as per POSIX-2008.1) // In which case, the entire process is stopped. thread->state = PS_STOPPED; - return 0; + return false; } + if (sigset_test(sh->sig_pending, _SIGCONT)) { thread->state = PS_READY; } @@ -173,7 +185,7 @@ schedule() assert(sched_ctx.ptable_len && sched_ctx.ttable_len); // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序…… - cpu_disable_interrupt(); + no_preemption(); if (!(current_thread->state & ~PS_RUNNING)) { current_thread->state = PS_READY; @@ -207,46 +219,11 @@ schedule() sched_ctx.procs_index = to_check->process->pid; done: - intc_notify_eos(0); run(to_check); fail("unexpected return from scheduler"); } -void -sched_pass() -{ - cpu_enable_interrupt(); - cpu_trap_sched(); -} - -__DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) -{ - if (!seconds) { - return 0; - } - - time_t systime = clock_systime() / 1000; - struct haybed* 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_ctx.sleepers, &bed->sleepers); - } - - store_retval(seconds); - - block_current_thread(); - schedule(); - - return 0; -} - __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds) { struct haybed* bed = ¤t_thread->sleep; @@ -255,7 +232,6 @@ __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds) bed->alarm_time = seconds ? now + seconds : 0; - struct proc_info* root_proc = sched_ctx.procs[0]; if (llist_empty(&bed->sleepers)) { llist_append(&sched_ctx.sleepers, &bed->sleepers); } @@ -303,6 +279,7 @@ _wait(pid_t wpid, int* status, int options) } wpid = wpid ? wpid : -__current->pgid; + repeat: llist_for_each(proc, n, &__current->children, siblings) { @@ -321,12 +298,12 @@ repeat: return 0; } // 放弃当前的运行机会 - sched_pass(); + yield_current(); goto repeat; done: if (status) { - *status = proc->exit_code | status_flags; + *status = PEXITNUM(status_flags, proc->exit_code); } return destroy_process(proc->pid); } @@ -339,7 +316,7 @@ get_free_pid() { ; if (unlikely(i == MAX_PROCESS)) { - panick("Panic in Ponyville shimmer!"); + fail("Panic in Ponyville shimmer!"); } return i; @@ -389,8 +366,10 @@ alloc_process() proc->created = clock_systime(); proc->pgid = proc->pid; - proc->sigreg = vzalloc(sizeof(struct sigregister)); - proc->fdtable = vzalloc(sizeof(struct v_fdtable)); + proc->root = vfs_sysroot; + + proc->sigreg = vzalloc(sizeof(struct sigregistry)); + proc->fdtable = fdtable_create(); proc->mm = procvm_create(proc); @@ -434,7 +413,7 @@ commit_process(struct proc_info* process) // every process is the child of first process (pid=1) if (!process->parent) { if (likely(!kernel_process(process))) { - process->parent = sched_ctx.procs[1]; + process->parent = root_process; } else { process->parent = process; } @@ -473,6 +452,20 @@ destory_thread(struct thread* thread) cake_release(thread_pile, thread); } +static void +orphan_children(struct proc_info* proc) +{ + struct proc_info *root; + struct proc_info *pos, *n; + + root = root_process; + + llist_for_each(pos, n, &proc->children, siblings) { + pos->parent = root; + llist_append(&root->children, &pos->siblings); + } +} + void delete_process(struct proc_info* proc) { @@ -509,7 +502,7 @@ delete_process(struct proc_info* proc) vfree(proc->fdtable); - signal_free_registers(proc->sigreg); + signal_free_registry(proc->sigreg); procvm_mount(mm); @@ -519,6 +512,8 @@ delete_process(struct proc_info* proc) destory_thread(pos); } + orphan_children(proc); + procvm_unmount_release(mm); cake_release(proc_pile, proc); @@ -526,7 +521,7 @@ delete_process(struct proc_info* proc) pid_t destroy_process(pid_t pid) -{ +{ int index = pid; if (index <= 0 || index > sched_ctx.ptable_len) { syscall_result(EINVAL); @@ -541,6 +536,8 @@ destroy_process(pid_t pid) static void terminate_proc_only(struct proc_info* proc, int exit_code) { + assert(proc->pid != 0); + proc->state = PS_TERMNAT; proc->exit_code = exit_code; @@ -568,13 +565,13 @@ terminate_proccess(struct proc_info* proc, int exit_code) { assert(!kernel_process(proc)); if (proc->pid == 1) { - panick("Attempt to kill init"); + fail("Attempt to kill init"); } terminate_proc_only(proc, exit_code); struct thread *pos, *n; - llist_for_each(pos, n, &__current->threads, proc_sibs) { + llist_for_each(pos, n, &proc->threads, proc_sibs) { pos->state = PS_TERMNAT; } } @@ -608,4 +605,5 @@ orphaned_proc(pid_t pid) // 如果其父进程的状态是terminated 或 destroy中的一种 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程 return proc_terminated(parent) || parent->created > proc->created; -} \ No newline at end of file +} +