1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/core/sched.c

184 lines
5.0 KiB
C
Raw Normal View History

/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup core_sched
* @{
*
* @file sched.c
* @brief Scheduler implementation
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author René Kijewski <rene.kijewski@fu-berlin.de>
*
* @}
*/
#include <stdint.h>
2014-05-14 10:46:15 +02:00
2013-12-16 17:54:58 +01:00
#include "sched.h"
#include "kernel.h"
#include "kernel_internal.h"
#include "clist.h"
#include "bitarithm.h"
#include "irq.h"
#include "thread.h"
2014-02-15 18:49:27 +01:00
#include "irq.h"
#if SCHEDSTATISTICS
#include "hwtimer.h"
#endif
#define ENABLE_DEBUG (0)
2013-12-16 17:54:58 +01:00
#include "debug.h"
volatile int sched_num_threads = 0;
volatile unsigned int sched_context_switch_request;
2014-08-13 09:33:25 +02:00
volatile tcb_t *sched_threads[KERNEL_PID_LAST + 1];
volatile tcb_t *sched_active_thread;
volatile kernel_pid_t sched_active_pid = KERNEL_PID_UNDEF;
clist_node_t *sched_runqueues[SCHED_PRIO_LEVELS];
static uint32_t runqueue_bitcache = 0;
#if SCHEDSTATISTICS
static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL;
2014-08-13 09:33:25 +02:00
schedstat sched_pidlist[KERNEL_PID_LAST + 1];
#endif
int sched_run(void)
{
2010-10-28 11:22:57 +02:00
sched_context_switch_request = 0;
tcb_t *active_thread = (tcb_t *)sched_active_thread;
/* The bitmask in runqueue_bitcache is never empty,
* since the threading should not be started before at least the idle thread was started.
*/
int nextrq = bitarithm_lsb(runqueue_bitcache);
tcb_t *next_thread = clist_get_container(sched_runqueues[nextrq], tcb_t, rq_entry);
DEBUG("scheduler: active thread: %" PRIkernel_pid ", next thread: %" PRIkernel_pid "\n",
(active_thread == NULL) ? KERNEL_PID_UNDEF : active_thread->pid,
next_thread->pid);
if (active_thread == next_thread) {
DEBUG("scheduler: done, sched_active_thread was not changed.\n");
return 0;
}
#ifdef SCHEDSTATISTICS
unsigned long time = hwtimer_now();
#endif
if (active_thread) {
if (active_thread->status == STATUS_RUNNING) {
active_thread->status = STATUS_PENDING;
}
#ifdef SCHED_TEST_STACK
if (*((uintptr_t *) active_thread->stack_start) != (uintptr_t) active_thread->stack_start) {
printf("scheduler(): stack overflow detected, pid=%" PRIkernel_pid "\n", active_thread->pid);
}
#endif
#ifdef SCHEDSTATISTICS
schedstat *active_stat = &sched_pidlist[active_thread->pid];
if (active_stat->laststart) {
active_stat->runtime_ticks += time - active_stat->laststart;
}
#endif
}
#if SCHEDSTATISTICS
schedstat *next_stat = &sched_pidlist[next_thread->pid];
next_stat->laststart = time;
next_stat->schedules++;
if (sched_cb) {
sched_cb(time, next_thread->pid);
}
#endif
next_thread->status = STATUS_RUNNING;
sched_active_pid = next_thread->pid;
sched_active_thread = (volatile tcb_t *) next_thread;
DEBUG("scheduler: done, changed sched_active_thread.\n");
return 1;
}
#if SCHEDSTATISTICS
void sched_register_cb(void (*callback)(uint32_t, uint32_t))
{
sched_cb = callback;
}
#endif
void sched_set_status(tcb_t *process, unsigned int status)
{
if (status >= STATUS_ON_RUNQUEUE) {
if (!(process->status >= STATUS_ON_RUNQUEUE)) {
DEBUG("sched_set_status: adding process %" PRIkernel_pid " to runqueue %" PRIu16 ".\n",
process->pid, process->priority);
clist_add(&sched_runqueues[process->priority], &(process->rq_entry));
runqueue_bitcache |= 1 << process->priority;
}
}
else {
if (process->status >= STATUS_ON_RUNQUEUE) {
DEBUG("sched_set_status: removing process %" PRIkernel_pid " to runqueue %" PRIu16 ".\n",
process->pid, process->priority);
clist_remove(&sched_runqueues[process->priority], &(process->rq_entry));
if (!sched_runqueues[process->priority]) {
runqueue_bitcache &= ~(1 << process->priority);
}
}
}
process->status = status;
}
void sched_switch(uint16_t other_prio)
{
int in_isr = inISR();
tcb_t *active_thread = (tcb_t *) sched_active_thread;
uint16_t current_prio = active_thread->priority;
DEBUG("sched_switch: active pid=%" PRIkernel_pid" prio=%" PRIu16
", other_prio=%" PRIu16 " in_isr=%i\n",
active_thread->pid, current_prio, other_prio, in_isr);
if (current_prio > other_prio) {
if (in_isr) {
2010-11-11 09:55:08 +01:00
sched_context_switch_request = 1;
}
else {
thread_yield_higher();
2010-11-11 09:55:08 +01:00
}
}
}
NORETURN void sched_task_exit(void)
{
DEBUG("sched_task_exit(): ending task %" PRIkernel_pid "...\n", sched_active_thread->pid);
dINT();
sched_threads[sched_active_pid] = NULL;
sched_num_threads--;
sched_set_status((tcb_t *)sched_active_thread, STATUS_STOPPED);
sched_active_thread = NULL;
2010-10-28 11:22:57 +02:00
cpu_switch_context_exit();
}