summaryrefslogtreecommitdiff
path: root/sys/kern
diff options
context:
space:
mode:
authorAptRock327 <dominik032009@gmail.com>2024-07-22 22:50:29 +0200
committerIan Moffett <ian@osmora.org>2024-07-22 17:49:07 -0400
commit322d41a2b01d70462fd2fa6a96d70b06a423e78b (patch)
tree57f9fd07f4c405b2ce97a06de6b37b1600a3916d /sys/kern
parent599ef07b89aa31c089398922ef65804f47f44b55 (diff)
kernel: sched: Implement MLFQ
Implements the Multilevel Feedback Queue scheduling algorithm. Signed-off-by: AptRock327 <dominik032009@gmail.com> Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/kern_sched.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c
index 1d79937..6d85330 100644
--- a/sys/kern/kern_sched.c
+++ b/sys/kern/kern_sched.c
@@ -140,6 +140,38 @@ this_td(void)
return ci->curtd;
}
+static inline void
+td_pri_raise(struct proc *td)
+{
+ if (td->priority > 0) {
+ td->priority--;
+ }
+}
+
+static inline void
+td_pri_lower(struct proc *td)
+{
+ if (td->priority < SCHED_NQUEUE - 1) {
+ td->priority++;
+ }
+}
+
+static inline void
+td_pri_update(struct proc *td)
+{
+ switch (policy) {
+ case SCHED_POLICY_MLFQ:
+ if (td->rested) {
+ td->rested = false;
+ td_pri_raise(td);
+ } else {
+ td_pri_lower(td);
+ }
+
+ break;
+ }
+}
+
/*
* Perform a context switch.
*/
@@ -154,6 +186,10 @@ sched_switch(struct trapframe *tf)
ci = this_cpu();
td = ci->curtd;
+ if (td != NULL) {
+ td_pri_update(td);
+ }
+
/*
* Get the next thread and use it only if it isn't
* in the middle of an exit, exec, or whatever.
@@ -205,6 +241,19 @@ sched_enter(void)
}
void
+sched_yield(void)
+{
+ struct proc *td = this_td();
+
+ if (td != NULL) {
+ td->rested = true;
+ }
+
+ sched_oneshot(false);
+ while (td->rested);
+}
+
+void
sched_init(void)
{
/* Setup the queues */