]> git.corax.cc Git - corax/commitdiff
Minor improvements to the scheduler:
authorMatthias Kruk <m@m10k.eu>
Tue, 29 Oct 2019 05:07:20 +0000 (14:07 +0900)
committerMatthias Kruk <m@m10k.eu>
Tue, 29 Oct 2019 05:07:20 +0000 (14:07 +0900)
 - 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

kernel/core/sched.c
kernel/core/sched.h [new file with mode: 0644]

index 10a76e30db62e59c8443da3b069122501b003688..c52d415ffe17847d7622a876c4e66ec2421480ac 100644 (file)
@@ -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 (file)
index 0000000..a37208f
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef __CORE_SCHED_H
+#define __CORE_SCHED_H
+
+#include <config.h>
+
+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 */