sched: minor refactor

This commit is contained in:
anna 2021-08-01 23:32:12 +02:00
parent 5a220eee5d
commit c36c2182d7
Signed by: fef
GPG key ID: EC22E476DC2D3D84
4 changed files with 35 additions and 48 deletions
arch/at91sam3x8e
include/arch
sched.c
include/ardix
kernel

View file

@ -29,7 +29,7 @@ struct reg_sw_snapshot {
* lr is saved by hardware, but we need to store it twice * lr is saved by hardware, but we need to store it twice
* because the IRQ entry overwrites it * 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 r2;
word_t r3; word_t r3;
word_t r12; word_t r12;
void *lr; /* alias r14 */ uintptr_t lr; /* alias r14 */
void *pc; /* alias r15 */ uintptr_t pc; /* alias r15 */
word_t psr; word_t psr;
}; };

View file

@ -9,8 +9,6 @@
#include <string.h> #include <string.h>
extern struct task *_sched_current_task;
void irq_sys_tick(void) void irq_sys_tick(void)
{ {
/* /*
@ -63,15 +61,15 @@ void arch_sched_task_init(struct task *task, void (*entry)(void))
task->sp = regs; task->sp = regs;
memset(regs, 0, sizeof(*regs)); memset(regs, 0, sizeof(*regs));
regs->hw.pc = entry; regs->hw.pc = (uintptr_t)entry;
regs->hw.psr = 0x01000000U; 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 */ REG_SYSTICK_VAL = 0U; /* Reset timer */
_sched_current_task->state = state; current->state = state;
arch_irq_invoke(IRQNO_PEND_SV); arch_irq_invoke(IRQNO_PEND_SV);
} }

View file

@ -6,20 +6,12 @@
#include <ardix/list.h> #include <ardix/list.h>
#include <ardix/types.h> #include <ardix/types.h>
#ifndef CONFIG_SCHED_MAXTASK #include <config.h>
/** The maximum number of tasks. */
#define CONFIG_SCHED_MAXTASK 8
#endif
#if CONFIG_SCHED_MAXTASK > 64 #if CONFIG_SCHED_MAXTASK > 64
#warning "CONFIG_SCHED_MAXTASK is > 64, this could have a significant performance impact" #warning "CONFIG_SCHED_MAXTASK is > 64, this could have a significant performance impact"
#endif #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 #ifndef CONFIG_STACKSZ
/** Per-task stack size in bytes */ /** Per-task stack size in bytes */
#define CONFIG_STACKSZ 4096U #define CONFIG_STACKSZ 4096U
@ -54,6 +46,8 @@ struct task {
pid_t pid; pid_t pid;
}; };
extern struct task *current;
/** /**
* Initialize the scheduler subsystem. * Initialize the scheduler subsystem.
* This sets up a hardware interrupt timer (SysTick for Cortex-M3). * 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. * @param state: State the task should enter.
* Allowed values are `TASK_SLEEP` and `TASK_IOWAIT`. * 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. * This file is part of Ardix.

View file

@ -15,13 +15,13 @@
extern uint32_t _sstack; extern uint32_t _sstack;
extern uint32_t _estack; extern uint32_t _estack;
static struct task *_sched_tasktab[CONFIG_SCHED_MAXTASK]; static struct task *tasktab[CONFIG_SCHED_MAXTASK];
struct task *_sched_current_task; struct task *current;
static void task_destroy(struct kent *kent) static void task_destroy(struct kent *kent)
{ {
struct task *task = container_of(kent, struct task, kent); struct task *task = container_of(kent, struct task, kent);
_sched_tasktab[task->pid] = NULL; tasktab[task->pid] = NULL;
free(task); free(task);
} }
@ -29,27 +29,27 @@ int sched_init(void)
{ {
int i; int i;
_sched_current_task = malloc(sizeof(*_sched_current_task)); current = malloc(sizeof(*current));
if (_sched_current_task == NULL) if (current == NULL)
return -ENOMEM; return -ENOMEM;
_sched_current_task->kent.parent = kent_root; current->kent.parent = kent_root;
_sched_current_task->kent.destroy = task_destroy; current->kent.destroy = task_destroy;
i = kent_init(&_sched_current_task->kent); i = kent_init(&current->kent);
if (i == 0) { if (i == 0) {
_sched_current_task->sp = &_sstack; current->sp = &_sstack;
_sched_current_task->stack_bottom = &_estack; current->stack_bottom = &_estack;
_sched_current_task->pid = 0; current->pid = 0;
_sched_current_task->state = TASK_READY; current->state = TASK_READY;
_sched_tasktab[0] = _sched_current_task; tasktab[0] = current;
for (i = 1; i < CONFIG_SCHED_MAXTASK; i++) for (i = 1; i < CONFIG_SCHED_MAXTASK; i++)
_sched_tasktab[i] = NULL; tasktab[i] = NULL;
i = arch_watchdog_init(); i = arch_watchdog_init();
if (i == 0) 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) void *sched_process_switch(void *curr_sp)
{ {
struct task *tmp; struct task *tmp;
pid_t nextpid = _sched_current_task->pid; pid_t nextpid = current->pid;
_sched_current_task->sp = curr_sp; current->sp = curr_sp;
if (_sched_current_task->state != TASK_SLEEP && _sched_current_task->state != TASK_IOWAIT) if (current->state != TASK_SLEEP && current->state != TASK_IOWAIT)
_sched_current_task->state = TASK_QUEUE; current->state = TASK_QUEUE;
while (1) { while (1) {
nextpid++; nextpid++;
nextpid %= CONFIG_SCHED_MAXTASK; nextpid %= CONFIG_SCHED_MAXTASK;
tmp = _sched_tasktab[nextpid]; tmp = tasktab[nextpid];
if (tmp != NULL && sched_task_should_run(tmp)) { if (tmp != NULL && sched_task_should_run(tmp)) {
_sched_current_task = tmp; current = tmp;
break; break;
} }
/* TODO: Add idle thread */ /* TODO: Add idle thread */
} }
_sched_current_task->state = TASK_READY; current->state = TASK_READY;
return _sched_current_task->sp; return current->sp;
} }
struct task *sched_fork(struct task *parent) struct task *sched_fork(struct task *parent)
@ -110,7 +110,7 @@ struct task *sched_fork(struct task *parent)
goto err_alloc; goto err_alloc;
for (pid = 0; pid < CONFIG_SCHED_MAXTASK; pid++) { for (pid = 0; pid < CONFIG_SCHED_MAXTASK; pid++) {
if (_sched_tasktab[pid] == NULL) if (tasktab[pid] == NULL)
break; break;
} }
if (pid == CONFIG_SCHED_MAXTASK) if (pid == CONFIG_SCHED_MAXTASK)
@ -131,11 +131,6 @@ err_alloc:
return NULL; return NULL;
} }
struct task *sched_current_task(void)
{
return _sched_current_task;
}
/* /*
* This file is part of Ardix. * This file is part of Ardix.
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>. * Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.