sched: add timer tasks and schedule_timeout()

This commit is contained in:
2023-04-30 21:09:36 +01:00
parent 085c3d2a89
commit 8a0291c9b3
8 changed files with 132 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
#include <socks/object.h>
#include <socks/sched.h>
#include <socks/clock.h>
#include <socks/cpu.h>
#include <socks/printk.h>
#include <socks/machine/thread.h>
@@ -49,6 +50,15 @@ kern_status_t sched_init(void)
return status;
}
static void expire_timers(struct cpu_data *cpu)
{
queue_foreach(struct timer, timer, &cpu->c_timers, t_entry) {
if (timer->t_expiry <= clock_ticks) {
timer->t_callback(timer);
}
}
}
void context_switch(struct thread *old, struct thread *new)
{
if (old->tr_parent->t_pmap != new->tr_parent->t_pmap) {
@@ -58,23 +68,27 @@ void context_switch(struct thread *old, struct thread *new)
switch_to(old, new);
}
void __schedule(void)
void __schedule(enum sched_mode mode)
{
ml_int_disable();
struct cpu_data *this_cpu = get_this_cpu();
struct runqueue *rq = &this_cpu->c_rq;
expire_timers(this_cpu);
unsigned long flags;
rq_lock(rq, &flags);
put_cpu(this_cpu);
struct thread *prev = rq->rq_cur;
prev->tr_quantum_cycles = 0;
prev->tr_flags &= ~THREAD_F_NEED_RESCHED;
if (prev->tr_quantum_cycles >= prev->tr_quantum_target) {
prev->tr_quantum_cycles = 0;
}
enum thread_state prev_state = READ_ONCE(prev->tr_state);
if (prev_state == THREAD_READY && prev != rq->rq_idle) {
if ((mode == SCHED_IRQ || prev_state == THREAD_READY) && prev != rq->rq_idle) {
rq_enqueue(rq, prev);
}
@@ -84,20 +98,22 @@ void __schedule(void)
next = rq->rq_idle;
}
if (mode == SCHED_NORMAL) {
next->tr_state = THREAD_READY;
}
rq->rq_cur = next;
rq_unlock(rq, flags);
if (prev != next) {
context_switch(prev, next);
} else {
ml_int_enable();
}
}
void schedule(void)
void schedule(enum sched_mode mode)
{
do {
__schedule();
__schedule(mode);
} while (need_resched());
}