From: Matthias Kruk Date: Tue, 29 Oct 2019 05:07:20 +0000 (+0900) Subject: Minor improvements to the scheduler: X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=6aaf4185fb379da1f92860097567344038174b84;p=corax Minor improvements to the scheduler: - Make _unq() return an error if it didn't succeed - Add sched_dequeue() function - Modify sched_tick() not to put tasks with TASK_STATE_EXIT back onto the ready queue - Add a header with prototypes for the functions implemented in sched.c --- diff --git a/kernel/core/sched.c b/kernel/core/sched.c index 10a76e3..c52d415 100644 --- a/kernel/core/sched.c +++ b/kernel/core/sched.c @@ -54,8 +54,12 @@ static task_t* _dq(struct task_queue **q) return(ret_val); } -static void _unq(struct task_queue **q, task_t *t) +static int _unq(struct task_queue **q, task_t *t) { + int ret_val; + + ret_val = -ENOENT; + while(*q) { if((*q)->data == t) { struct task_queue *freeme; @@ -64,13 +68,14 @@ static void _unq(struct task_queue **q, task_t *t) *q = freeme->next; kfree(freeme); + ret_val = 0; break; } q = &((*q)->next); } - return; + return(ret_val); } void sched_tick(void) @@ -79,7 +84,8 @@ void sched_tick(void) cur_task = task_get_current(); - if(cur_task) { + /* skip all this if the task is exitting */ + if(cur_task && cur_task->t_state != TASK_STATE_EXIT) { /* check if running task has not used up it's quantum yet */ if(cur_task->t_rslice) { cur_task->t_rslice--; @@ -91,7 +97,7 @@ void sched_tick(void) cur_task->t_state = TASK_STATE_READY; /* put the task back on the ready queue */ - _nq(&_readyq, cur_task); + _nq(&_readyq, cur_task); } /* get the next task from the ready queue */ @@ -155,6 +161,28 @@ int sched_enqueue(task_t *t) return(ret_val); } +int sched_dequeue(task_t *t) +{ + int ret_val; + + ret_val = -EINVAL; + + if(t) { + /* remove the task from the ready queue */ + ret_val = _unq(&_readyq, t); + + /* + * Attempt to remove the task from the wait queue + * if it wasn't on the ready queue. + */ + if(ret_val < 0) { + ret_val = _unq(&_waitq, t); + } + } + + return(ret_val); +} + int sched_signal(pid_t pid) { task_t *cur_task; diff --git a/kernel/core/sched.h b/kernel/core/sched.h new file mode 100644 index 0000000..a37208f --- /dev/null +++ b/kernel/core/sched.h @@ -0,0 +1,12 @@ +#ifndef __CORE_SCHED_H +#define __CORE_SCHED_H + +#include + +void sched_tick(void); +void sched_wait(pid_t); +int sched_enqueue(task_t*); +int sched_dequeue(task_t*); +int sched_signal(pid_t); + +#endif /* __CORE_SCHED_H */