sched: minor refactor
This commit is contained in:
parent
5a220eee5d
commit
c36c2182d7
4 changed files with 35 additions and 48 deletions
|
@ -29,7 +29,7 @@ struct reg_sw_snapshot {
|
|||
* lr is saved by hardware, but we need to store it twice
|
||||
* because the IRQ entry overwrites it
|
||||
*/
|
||||
void *lr; /* alias r14 */
|
||||
uintptr_t lr; /* alias r14 */
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -42,8 +42,8 @@ struct reg_hw_snapshot {
|
|||
word_t r2;
|
||||
word_t r3;
|
||||
word_t r12;
|
||||
void *lr; /* alias r14 */
|
||||
void *pc; /* alias r15 */
|
||||
uintptr_t lr; /* alias r14 */
|
||||
uintptr_t pc; /* alias r15 */
|
||||
word_t psr;
|
||||
};
|
||||
|
||||
|
|
|
@ -9,8 +9,6 @@
|
|||
|
||||
#include <string.h>
|
||||
|
||||
extern struct task *_sched_current_task;
|
||||
|
||||
void irq_sys_tick(void)
|
||||
{
|
||||
/*
|
||||
|
@ -63,15 +61,15 @@ void arch_sched_task_init(struct task *task, void (*entry)(void))
|
|||
task->sp = regs;
|
||||
|
||||
memset(regs, 0, sizeof(*regs));
|
||||
regs->hw.pc = entry;
|
||||
regs->hw.pc = (uintptr_t)entry;
|
||||
regs->hw.psr = 0x01000000U;
|
||||
regs->sw.lr = (void *)0xFFFFFFF9U;
|
||||
regs->sw.lr = 0xfffffff9U;
|
||||
}
|
||||
|
||||
void sched_yield(enum task_state state)
|
||||
void yield(enum task_state state)
|
||||
{
|
||||
REG_SYSTICK_VAL = 0U; /* Reset timer */
|
||||
_sched_current_task->state = state;
|
||||
current->state = state;
|
||||
arch_irq_invoke(IRQNO_PEND_SV);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,20 +6,12 @@
|
|||
#include <ardix/list.h>
|
||||
#include <ardix/types.h>
|
||||
|
||||
#ifndef CONFIG_SCHED_MAXTASK
|
||||
/** The maximum number of tasks. */
|
||||
#define CONFIG_SCHED_MAXTASK 8
|
||||
#endif
|
||||
#include <config.h>
|
||||
|
||||
#if CONFIG_SCHED_MAXTASK > 64
|
||||
#warning "CONFIG_SCHED_MAXTASK is > 64, this could have a significant performance impact"
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_SCHED_INTR_FREQ
|
||||
/** Frequency (in Hertz) at which a scheduling interrupt should be fired */
|
||||
#define CONFIG_SCHED_INTR_FREQ 10000U
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_STACKSZ
|
||||
/** Per-task stack size in bytes */
|
||||
#define CONFIG_STACKSZ 4096U
|
||||
|
@ -54,6 +46,8 @@ struct task {
|
|||
pid_t pid;
|
||||
};
|
||||
|
||||
extern struct task *current;
|
||||
|
||||
/**
|
||||
* Initialize the scheduler subsystem.
|
||||
* This sets up a hardware interrupt timer (SysTick for Cortex-M3).
|
||||
|
@ -86,7 +80,7 @@ struct task *sched_task_clone(struct task *task);
|
|||
* @param state: State the task should enter.
|
||||
* Allowed values are `TASK_SLEEP` and `TASK_IOWAIT`.
|
||||
*/
|
||||
void sched_yield(enum task_state state);
|
||||
void yield(enum task_state state);
|
||||
|
||||
/*
|
||||
* This file is part of Ardix.
|
||||
|
|
|
@ -15,13 +15,13 @@
|
|||
extern uint32_t _sstack;
|
||||
extern uint32_t _estack;
|
||||
|
||||
static struct task *_sched_tasktab[CONFIG_SCHED_MAXTASK];
|
||||
struct task *_sched_current_task;
|
||||
static struct task *tasktab[CONFIG_SCHED_MAXTASK];
|
||||
struct task *current;
|
||||
|
||||
static void task_destroy(struct kent *kent)
|
||||
{
|
||||
struct task *task = container_of(kent, struct task, kent);
|
||||
_sched_tasktab[task->pid] = NULL;
|
||||
tasktab[task->pid] = NULL;
|
||||
free(task);
|
||||
}
|
||||
|
||||
|
@ -29,27 +29,27 @@ int sched_init(void)
|
|||
{
|
||||
int i;
|
||||
|
||||
_sched_current_task = malloc(sizeof(*_sched_current_task));
|
||||
if (_sched_current_task == NULL)
|
||||
current = malloc(sizeof(*current));
|
||||
if (current == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
_sched_current_task->kent.parent = kent_root;
|
||||
_sched_current_task->kent.destroy = task_destroy;
|
||||
i = kent_init(&_sched_current_task->kent);
|
||||
current->kent.parent = kent_root;
|
||||
current->kent.destroy = task_destroy;
|
||||
i = kent_init(¤t->kent);
|
||||
if (i == 0) {
|
||||
_sched_current_task->sp = &_sstack;
|
||||
_sched_current_task->stack_bottom = &_estack;
|
||||
_sched_current_task->pid = 0;
|
||||
_sched_current_task->state = TASK_READY;
|
||||
_sched_tasktab[0] = _sched_current_task;
|
||||
current->sp = &_sstack;
|
||||
current->stack_bottom = &_estack;
|
||||
current->pid = 0;
|
||||
current->state = TASK_READY;
|
||||
tasktab[0] = current;
|
||||
|
||||
for (i = 1; i < CONFIG_SCHED_MAXTASK; i++)
|
||||
_sched_tasktab[i] = NULL;
|
||||
tasktab[i] = NULL;
|
||||
|
||||
i = arch_watchdog_init();
|
||||
|
||||
if (i == 0)
|
||||
i = arch_sched_hwtimer_init(CONFIG_SCHED_MAXTASK);
|
||||
i = arch_sched_hwtimer_init(CONFIG_SCHED_FREQ);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -79,26 +79,26 @@ static inline bool sched_task_should_run(const struct task *task)
|
|||
void *sched_process_switch(void *curr_sp)
|
||||
{
|
||||
struct task *tmp;
|
||||
pid_t nextpid = _sched_current_task->pid;
|
||||
_sched_current_task->sp = curr_sp;
|
||||
pid_t nextpid = current->pid;
|
||||
current->sp = curr_sp;
|
||||
|
||||
if (_sched_current_task->state != TASK_SLEEP && _sched_current_task->state != TASK_IOWAIT)
|
||||
_sched_current_task->state = TASK_QUEUE;
|
||||
if (current->state != TASK_SLEEP && current->state != TASK_IOWAIT)
|
||||
current->state = TASK_QUEUE;
|
||||
|
||||
while (1) {
|
||||
nextpid++;
|
||||
nextpid %= CONFIG_SCHED_MAXTASK;
|
||||
|
||||
tmp = _sched_tasktab[nextpid];
|
||||
tmp = tasktab[nextpid];
|
||||
if (tmp != NULL && sched_task_should_run(tmp)) {
|
||||
_sched_current_task = tmp;
|
||||
current = tmp;
|
||||
break;
|
||||
}
|
||||
/* TODO: Add idle thread */
|
||||
}
|
||||
|
||||
_sched_current_task->state = TASK_READY;
|
||||
return _sched_current_task->sp;
|
||||
current->state = TASK_READY;
|
||||
return current->sp;
|
||||
}
|
||||
|
||||
struct task *sched_fork(struct task *parent)
|
||||
|
@ -110,7 +110,7 @@ struct task *sched_fork(struct task *parent)
|
|||
goto err_alloc;
|
||||
|
||||
for (pid = 0; pid < CONFIG_SCHED_MAXTASK; pid++) {
|
||||
if (_sched_tasktab[pid] == NULL)
|
||||
if (tasktab[pid] == NULL)
|
||||
break;
|
||||
}
|
||||
if (pid == CONFIG_SCHED_MAXTASK)
|
||||
|
@ -131,11 +131,6 @@ err_alloc:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
struct task *sched_current_task(void)
|
||||
{
|
||||
return _sched_current_task;
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of Ardix.
|
||||
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
||||
|
|
Loading…
Reference in a new issue