sched: add timer tasks and schedule_timeout()
This commit is contained in:
34
sched/core.c
34
sched/core.c
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user