fix: sleeper issue #13
[lunaix-os.git] / lunaix-os / kernel / process / sched.c
index 91932ca3dd5d7d4aa3ac3c7c9e170b89cc8d2cb2..5e882b7df8f4c32fa3ff7757f84be78d215efae4 100644 (file)
@@ -47,6 +47,8 @@ sched_init()
     sched_init_dummy();
 }
 
     sched_init_dummy();
 }
 
+#define DUMMY_STACK_SIZE 2048
+
 void
 sched_init_dummy()
 {
 void
 sched_init_dummy()
 {
@@ -54,28 +56,30 @@ sched_init_dummy()
     // It is a living nightmare!
 
     extern void my_dummy();
     // It is a living nightmare!
 
     extern void my_dummy();
-    static char dummy_stack[1024] __attribute__((aligned(16)));
+    static char dummy_stack[DUMMY_STACK_SIZE] __attribute__((aligned(16)));
 
     // memset to 0
     dummy_proc = (struct proc_info){};
 
     // memset to 0
     dummy_proc = (struct proc_info){};
-    dummy_proc.intr_ctx =
-      (isr_param){ .registers = { .ds = KDATA_SEG,
-                                  .es = KDATA_SEG,
-                                  .fs = KDATA_SEG,
-                                  .gs = KDATA_SEG,
-                                  .esp = (void*)dummy_stack + 1004 },
-                   .cs = KCODE_SEG,
-                   .eip = (void*)my_dummy,
-                   .ss = KDATA_SEG,
-                   .eflags = cpu_reflags() | 0x0200 };
-
-    *(u32_t*)(&dummy_stack[1020]) = dummy_proc.intr_ctx.eflags;
-    *(u32_t*)(&dummy_stack[1016]) = KCODE_SEG;
-    *(u32_t*)(&dummy_stack[1012]) = dummy_proc.intr_ctx.eip;
+    dummy_proc.intr_ctx = (isr_param){
+        .registers = { .ds = KDATA_SEG,
+                       .es = KDATA_SEG,
+                       .fs = KDATA_SEG,
+                       .gs = KDATA_SEG,
+                       .esp = (void*)dummy_stack + DUMMY_STACK_SIZE - 20 },
+        .cs = KCODE_SEG,
+        .eip = (void*)my_dummy,
+        .ss = KDATA_SEG,
+        .eflags = cpu_reflags() | 0x0200
+    };
+
+    *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 4]) = dummy_proc.intr_ctx.eflags;
+    *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 8]) = KCODE_SEG;
+    *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 12]) = dummy_proc.intr_ctx.eip;
 
     dummy_proc.page_table = cpu_rcr3();
     dummy_proc.state = PS_READY;
     dummy_proc.parent = &dummy_proc;
 
     dummy_proc.page_table = cpu_rcr3();
     dummy_proc.state = PS_READY;
     dummy_proc.parent = &dummy_proc;
+    dummy_proc.pid = KERNEL_PID;
 
     __current = &dummy_proc;
 }
 
     __current = &dummy_proc;
 }
@@ -209,7 +213,10 @@ __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
 
     struct proc_info* root_proc = sched_ctx._procs[0];
     __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
 
     struct proc_info* root_proc = sched_ctx._procs[0];
     __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
-    llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
+
+    if (llist_empty(&__current->sleep.sleepers)) {
+        llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
+    }
 
     __current->intr_ctx.registers.eax = seconds;
 
 
     __current->intr_ctx.registers.eax = seconds;