ardix/kernel/sched.c

111 lines
3.5 KiB
C
Raw Normal View History

2020-10-11 19:35:30 +02:00
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
2020-06-12 12:29:27 +02:00
#include <arch/sched.h>
#include <arch/watchdog.h>
2020-11-30 02:26:17 +01:00
#include <ardix/atomic.h>
2020-11-30 02:26:17 +01:00
#include <ardix/malloc.h>
#include <ardix/sched.h>
2020-06-14 05:51:01 +02:00
#include <ardix/string.h>
#include <ardix/types.h>
2020-11-30 02:26:17 +01:00
#include <errno.h>
#include <stddef.h>
extern uint32_t _sstack;
extern uint32_t _estack;
2021-02-01 00:07:45 +01:00
static struct task *_sched_tasktab[CONFIG_SCHED_MAXTASK];
struct task *_sched_current_task;
int sched_init(void)
{
int i;
2021-02-01 00:07:45 +01:00
_sched_current_task = malloc(sizeof(*_sched_current_task));
if (_sched_current_task == NULL)
2020-11-30 02:26:17 +01:00
return -ENOMEM;
2021-02-01 00:07:45 +01:00
_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;
2021-02-01 00:07:45 +01:00
for (i = 1; i < CONFIG_SCHED_MAXTASK; i++)
_sched_tasktab[i] = NULL;
i = arch_watchdog_init();
if (i == 0)
2021-02-01 00:07:45 +01:00
i = arch_sched_hwtimer_init(CONFIG_SCHED_MAXTASK);
return i;
}
2020-06-14 05:51:01 +02:00
/**
2021-02-01 00:07:45 +01:00
* Determine whether the specified task is a candidate for execution.
2020-06-14 05:51:01 +02:00
*
2021-02-01 00:07:45 +01:00
* @param task: the task
* @returns whether `task` could be run next
2020-06-14 05:51:01 +02:00
*/
2021-02-01 00:07:45 +01:00
static inline bool sched_task_should_run(const struct task *task)
2020-06-14 05:51:01 +02:00
{
2021-02-01 00:07:45 +01:00
enum task_state state = task->state;
2020-06-14 05:51:01 +02:00
2021-02-01 00:07:45 +01:00
if (state == TASK_QUEUE || state == TASK_READY || state == TASK_IOWAIT)
2020-06-14 05:51:01 +02:00
return true;
return false;
}
void *sched_process_switch(void *curr_sp)
{
2021-02-01 00:07:45 +01:00
struct task *tmp;
pid_t nextpid = _sched_current_task->pid;
_sched_current_task->sp = curr_sp;
2021-02-01 00:07:45 +01:00
if (_sched_current_task->state != TASK_SLEEP && _sched_current_task->state != TASK_IOWAIT)
_sched_current_task->state = TASK_QUEUE;
2020-06-14 05:51:01 +02:00
2020-11-30 02:26:17 +01:00
while (1) {
nextpid++;
2021-02-01 00:07:45 +01:00
nextpid %= CONFIG_SCHED_MAXTASK;
2021-02-17 14:08:43 +01:00
tmp = _sched_tasktab[nextpid];
if (tmp != NULL && sched_task_should_run(tmp)) {
2021-02-01 00:07:45 +01:00
_sched_current_task = tmp;
break;
}
2020-11-30 02:26:17 +01:00
/* TODO: Add idle thread */
}
2021-02-01 00:07:45 +01:00
_sched_current_task->state = TASK_READY;
return _sched_current_task->sp;
}
2020-10-11 19:35:30 +02:00
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/