2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2014-04-07 11:16:32 +02:00
|
|
|
* Copyright (C) 2014 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2013-11-22 20:47:05 +01:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
2013-06-18 17:21:38 +02:00
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-04-07 11:16:32 +02:00
|
|
|
* @ingroup core_sched
|
2013-11-27 16:28:31 +01:00
|
|
|
* @{
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2014-04-07 11:16:32 +02:00
|
|
|
* @file sched.c
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Scheduler implementation
|
|
|
|
*
|
|
|
|
* @author Freie Universität Berlin, Computer Systems & Telematics
|
2014-01-28 11:50:12 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2013-11-16 19:26:02 +01:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @}
|
2013-11-27 17:54:30 +01:00
|
|
|
*
|
2014-02-11 18:15:43 +01:00
|
|
|
* TODO: setup dependency from SCHEDSTATISTICS to MODULE_HWTIMER
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2014-03-17 17:59:06 +01: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"
|
2014-03-17 17:59:06 +01:00
|
|
|
#include "irq.h"
|
2013-12-19 12:16:35 +01:00
|
|
|
#include "thread.h"
|
2014-02-15 18:49:27 +01:00
|
|
|
#include "irq.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-11-16 19:26:02 +01:00
|
|
|
#if SCHEDSTATISTICS
|
|
|
|
#include "hwtimer.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-24 00:36:06 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "debug.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
volatile int sched_num_threads = 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2011-06-24 17:53:09 +02:00
|
|
|
volatile unsigned int sched_context_switch_request;
|
|
|
|
|
|
|
|
volatile tcb_t *sched_threads[MAXTHREADS];
|
2014-04-10 22:28:35 +02:00
|
|
|
volatile tcb_t *sched_active_thread;
|
2011-06-24 17:53:09 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
volatile int sched_active_pid = -1;
|
|
|
|
volatile int thread_last_pid = -1;
|
2011-06-24 17:53:09 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
clist_node_t *sched_runqueues[SCHED_PRIO_LEVELS];
|
2010-09-22 15:10:42 +02:00
|
|
|
static uint32_t runqueue_bitcache = 0;
|
|
|
|
|
|
|
|
#if SCHEDSTATISTICS
|
2013-06-20 18:18:29 +02:00
|
|
|
static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL;
|
2014-04-10 22:28:35 +02:00
|
|
|
schedstat sched_pidlist[MAXTHREADS];
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
|
|
|
|
2014-05-07 12:36:32 +02:00
|
|
|
void sched_run(void)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2010-10-28 11:22:57 +02:00
|
|
|
sched_context_switch_request = 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
tcb_t *my_active_thread = (tcb_t *)sched_active_thread;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (my_active_thread) {
|
|
|
|
if (my_active_thread->status == STATUS_RUNNING) {
|
2013-06-20 18:18:29 +02:00
|
|
|
my_active_thread->status = STATUS_PENDING;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SCHED_TEST_STACK
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (*((unsigned int *)my_active_thread->stack_start) != (unsigned int) my_active_thread->stack_start) {
|
2010-10-28 11:22:57 +02:00
|
|
|
printf("scheduler(): stack overflow detected, task=%s pid=%u\n", my_active_thread->name, my_active_thread->pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-16 19:26:02 +01:00
|
|
|
#ifdef SCHEDSTATISTICS
|
|
|
|
unsigned long time = hwtimer_now();
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
if (my_active_thread && (sched_pidlist[my_active_thread->pid].laststart)) {
|
|
|
|
sched_pidlist[my_active_thread->pid].runtime_ticks += time - sched_pidlist[my_active_thread->pid].laststart;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
DEBUG("\nscheduler: previous task: %s\n", (my_active_thread == NULL) ? "none" : my_active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
if (sched_num_threads == 0) {
|
2010-09-22 15:10:42 +02:00
|
|
|
DEBUG("scheduler: no tasks left.\n");
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
while (!sched_num_threads) {
|
2013-06-20 18:18:29 +02:00
|
|
|
/* loop until a new task arrives */
|
|
|
|
;
|
2013-06-09 17:52:02 +02:00
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
DEBUG("scheduler: new task created.\n");
|
|
|
|
}
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
my_active_thread = NULL;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (!my_active_thread) {
|
2014-04-10 22:28:35 +02:00
|
|
|
int nextrq = bitarithm_lsb(runqueue_bitcache);
|
|
|
|
clist_node_t next = *(sched_runqueues[nextrq]);
|
2013-06-20 18:18:29 +02:00
|
|
|
DEBUG("scheduler: first in queue: %s\n", ((tcb_t *)next.data)->name);
|
2014-04-10 22:28:35 +02:00
|
|
|
clist_advance(&(sched_runqueues[nextrq]));
|
2013-06-20 18:18:29 +02:00
|
|
|
my_active_thread = (tcb_t *)next.data;
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_active_pid = (volatile int) my_active_thread->pid;
|
2010-09-22 15:10:42 +02:00
|
|
|
#if SCHEDSTATISTICS
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_pidlist[my_active_thread->pid].laststart = time;
|
|
|
|
sched_pidlist[my_active_thread->pid].schedules++;
|
|
|
|
if ((sched_cb) && (my_active_thread->pid != thread_last_pid)) {
|
2013-08-14 13:04:38 +02:00
|
|
|
sched_cb(hwtimer_now(), my_active_thread->pid);
|
2014-04-10 22:28:35 +02:00
|
|
|
thread_last_pid = my_active_thread->pid;
|
2013-08-14 13:04:38 +02:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
2013-02-28 18:11:20 +01:00
|
|
|
#ifdef MODULE_NSS
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
if (sched_active_thread && sched_active_thread->pid != thread_last_pid) {
|
|
|
|
thread_last_pid = sched_active_thread->pid;
|
2012-10-18 18:08:40 +02:00
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-02-28 18:11:20 +01:00
|
|
|
#endif
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("scheduler: next task: %s\n", my_active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
if (my_active_thread != sched_active_thread) {
|
|
|
|
if (sched_active_thread != NULL) { /* TODO: necessary? */
|
|
|
|
if (sched_active_thread->status == STATUS_RUNNING) {
|
|
|
|
sched_active_thread->status = STATUS_PENDING ;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
|
|
|
sched_set_status((tcb_t *)my_active_thread, STATUS_RUNNING);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_active_thread = (volatile tcb_t *) my_active_thread;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
DEBUG("scheduler: done.\n");
|
|
|
|
}
|
|
|
|
|
2011-10-05 15:13:34 +02:00
|
|
|
#if SCHEDSTATISTICS
|
2013-06-20 18:18:29 +02:00
|
|
|
void sched_register_cb(void (*callback)(uint32_t, uint32_t))
|
|
|
|
{
|
|
|
|
sched_cb = callback;
|
2011-10-05 15:13:34 +02:00
|
|
|
}
|
|
|
|
#endif
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void sched_set_status(tcb_t *process, unsigned int status)
|
|
|
|
{
|
2014-02-17 12:28:54 +01:00
|
|
|
if (status >= STATUS_ON_RUNQUEUE) {
|
|
|
|
if (!(process->status >= STATUS_ON_RUNQUEUE)) {
|
2010-09-22 15:10:42 +02:00
|
|
|
DEBUG("adding process %s to runqueue %u.\n", process->name, process->priority);
|
2014-04-10 22:28:35 +02:00
|
|
|
clist_add(&sched_runqueues[process->priority], &(process->rq_entry));
|
2010-09-22 15:10:42 +02:00
|
|
|
runqueue_bitcache |= 1 << process->priority;
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-02-17 12:28:54 +01:00
|
|
|
if (process->status >= STATUS_ON_RUNQUEUE) {
|
2010-09-22 15:10:42 +02:00
|
|
|
DEBUG("removing process %s from runqueue %u.\n", process->name, process->priority);
|
2014-04-10 22:28:35 +02:00
|
|
|
clist_remove(&sched_runqueues[process->priority], &(process->rq_entry));
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
if (!sched_runqueues[process->priority]) {
|
2010-09-22 15:10:42 +02:00
|
|
|
runqueue_bitcache &= ~(1 << process->priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
process->status = status;
|
|
|
|
}
|
|
|
|
|
2014-02-15 18:49:49 +01:00
|
|
|
void sched_switch(uint16_t current_prio, uint16_t other_prio)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-02-15 18:49:49 +01:00
|
|
|
int in_isr = inISR();
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
DEBUG("%s: %i %i %i\n", sched_active_thread->name, (int)current_prio, (int)other_prio, in_isr);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-08-12 19:27:04 +02:00
|
|
|
if (current_prio >= other_prio) {
|
2013-06-24 22:37:35 +02:00
|
|
|
if (in_isr) {
|
2010-11-11 09:55:08 +01:00
|
|
|
sched_context_switch_request = 1;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-11-11 09:55:08 +01:00
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 09:41:37 +02:00
|
|
|
NORETURN void sched_task_exit(void)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-04-10 22:28:35 +02:00
|
|
|
DEBUG("sched_task_exit(): ending task %s...\n", sched_active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
dINT();
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_threads[sched_active_thread->pid] = NULL;
|
|
|
|
sched_num_threads--;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_set_status((tcb_t *)sched_active_thread, STATUS_STOPPED);
|
2010-10-25 15:40:01 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_active_thread = NULL;
|
2010-10-28 11:22:57 +02:00
|
|
|
cpu_switch_context_exit();
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|