From 319f197fcd73d401ac184244dbc51504443e1437 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Sun, 3 May 2020 23:13:17 +0200 Subject: [PATCH] Fix scheduler with sleeping task --- core/kthread.c | 15 ++++++++++----- core/main.c | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/kthread.c b/core/kthread.c index 7ec8ef4..4038835 100644 --- a/core/kthread.c +++ b/core/kthread.c @@ -77,10 +77,13 @@ free_mem: void kthreadDelete(struct kthread *thread) { + uint32_t flags; + disable_IRQs(flags); list_delete(currentThread, thread); free((void *)thread->stackAddr); free((void *)thread); + restore_IRQs(flags); } struct kthread *kthreadSelectNext() @@ -93,8 +96,7 @@ struct kthread *kthreadSelectNext() return nextThread; } } - assert("Cannot find next thread\n"); - return nextThread; + return currentThread; } struct cpu_state *kthreadSwitch(struct cpu_state *prevCpu) @@ -140,7 +142,9 @@ int kthreadYield() restore_IRQs(flags); return 0; } - current->state = READY; + assert(current->state == RUNNING); + assert(next->state == READY); + current->state = READY; currentThread = next; currentThread->state = RUNNING; cpu_context_switch(¤t->cpuState, next->cpuState); @@ -152,10 +156,11 @@ int kthreadMsleep(unsigned long msec) { uint32_t flags; disable_IRQs(flags); - struct kthread *next = kthreadSelectNext(); struct kthread *current = currentThread; + assert(current->state == RUNNING); + current->state = SLEEPING; + struct kthread *next = kthreadSelectNext(); assert(next != current); - current->state = SLEEPING; current->jiffiesSleeping = msecs_to_jiffies(msec); assert(next->state == READY); currentThread = next; diff --git a/core/main.c b/core/main.c index eed2156..cab937b 100644 --- a/core/main.c +++ b/core/main.c @@ -28,7 +28,7 @@ void idleThread(void *arg) (void)arg; while (1) { printIntDetails((jiffies / HZ), GREEN, BLACK, 0, VGA_HEIGHT - 1); - //kthreadYield(); + kthreadYield(); } }