X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/b60166b327a9108b07e3069fa6568a451529ffd9..bdc143a7aa3f51a46eceec62b0b364599533fa21:/lunaix-os/kernel/process/sched.c diff --git a/lunaix-os/kernel/process/sched.c b/lunaix-os/kernel/process/sched.c index c92cde9..82a2aa9 100644 --- a/lunaix-os/kernel/process/sched.c +++ b/lunaix-os/kernel/process/sched.c @@ -1,7 +1,6 @@ #include #include -#include #include #include @@ -18,18 +17,24 @@ #include #include #include -#include +#include #include +#include + #include +struct thread empty_thread_obj; + volatile struct proc_info* __current; -volatile struct thread* current_thread; +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 @@ -53,6 +58,7 @@ run(struct thread* thread) thread->process->state = PS_RUNNING; thread->process->th_active = thread; + procvm_mount_self(vmspace(thread->process)); set_current_executing(thread); switch_context(); @@ -80,9 +86,11 @@ cleanup_detached_threads() { continue; } - vmm_mount_pd(VMS_MOUNT_1, vmroot(pos->process)); - destory_thread(VMS_MOUNT_1, pos); - vmm_unmount_pd(VMS_MOUNT_1); + struct proc_mm* mm = vmspace(pos->process); + + procvm_mount(mm); + destory_thread(pos); + procvm_unmount(mm); i++; } @@ -173,8 +181,10 @@ schedule() if (!(current_thread->state & ~PS_RUNNING)) { current_thread->state = PS_READY; __current->state = PS_READY; + } + procvm_unmount_self(vmspace(__current)); check_sleepers(); // round-robin scheduler @@ -200,7 +210,7 @@ schedule() sched_ctx.procs_index = to_check->process->pid; done: - intc_notify_eos(0); + isrm_notify_eos(0); run(to_check); fail("unexpected return from scheduler"); @@ -248,7 +258,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); } @@ -382,7 +391,7 @@ alloc_process() proc->created = clock_systime(); proc->pgid = proc->pid; - proc->sigreg = vzalloc(sizeof(struct sigregister)); + proc->sigreg = vzalloc(sizeof(struct sigregistry)); proc->fdtable = vzalloc(sizeof(struct v_fdtable)); proc->mm = procvm_create(proc); @@ -427,7 +436,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; } @@ -447,7 +456,7 @@ commit_process(struct proc_info* process) } void -destory_thread(ptr_t vm_mnt, struct thread* thread) +destory_thread(struct thread* thread) { cake_ensure_valid(thread); @@ -458,7 +467,7 @@ destory_thread(ptr_t vm_mnt, struct thread* thread) llist_delete(&thread->sleep.sleepers); waitq_cancel_wait(&thread->waitqueue); - thread_release_mem(thread, vm_mnt); + thread_release_mem(thread); proc->thread_count--; sched_ctx.ttable_len--; @@ -466,10 +475,25 @@ destory_thread(ptr_t vm_mnt, 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) { pid_t pid = proc->pid; + struct proc_mm* mm = vmspace(proc); assert(pid); // long live the pid0 !! @@ -501,19 +525,19 @@ delete_process(struct proc_info* proc) vfree(proc->fdtable); - signal_free_registers(proc->sigreg); + signal_free_registry(proc->sigreg); - vmm_mount_pd(VMS_MOUNT_1, vmroot(proc)); + procvm_mount(mm); struct thread *pos, *n; llist_for_each(pos, n, &proc->threads, proc_sibs) { // terminate and destory all thread unconditionally - destory_thread(VMS_MOUNT_1, pos); + destory_thread(pos); } - procvm_cleanup(VMS_MOUNT_1, proc); + orphan_children(proc); - vmm_unmount_pd(VMS_MOUNT_1); + procvm_unmount_release(mm); cake_release(proc_pile, proc); }