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
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* 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.
|
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
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Scheduler implementation
|
|
|
|
*
|
2014-01-28 11:50:12 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2014-11-05 11:29:25 +01:00
|
|
|
* @author René Kijewski <rene.kijewski@fu-berlin.de>
|
2013-11-16 19:26:02 +01:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2014-05-14 10:46:15 +02:00
|
|
|
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "sched.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"
|
2015-02-25 16:31:03 +01:00
|
|
|
#include "log.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2015-07-29 19:59:16 +02:00
|
|
|
#ifdef MODULE_SCHEDSTATISTICS
|
|
|
|
#include "xtimer.h"
|
2013-11-16 19:26:02 +01:00
|
|
|
#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
|
|
|
|
2015-07-06 17:48:36 +02:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
/* For PRIu16 etc. */
|
|
|
|
#include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
|
2016-01-04 22:29:34 +01:00
|
|
|
volatile thread_t *sched_threads[KERNEL_PID_LAST + 1];
|
|
|
|
volatile thread_t *sched_active_thread;
|
2011-06-24 17:53:09 +02:00
|
|
|
|
2014-08-06 09:27:23 +02:00
|
|
|
volatile kernel_pid_t sched_active_pid = KERNEL_PID_UNDEF;
|
2011-06-24 17:53:09 +02:00
|
|
|
|
2014-10-28 00:53:49 +01:00
|
|
|
clist_node_t *sched_runqueues[SCHED_PRIO_LEVELS];
|
2010-09-22 15:10:42 +02:00
|
|
|
static uint32_t runqueue_bitcache = 0;
|
|
|
|
|
2015-07-29 19:59:16 +02:00
|
|
|
#ifdef MODULE_SCHEDSTATISTICS
|
2013-06-20 18:18:29 +02:00
|
|
|
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];
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
|
|
|
|
2014-10-18 18:41:40 +02:00
|
|
|
int 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
|
|
|
|
2016-01-04 22:29:34 +01:00
|
|
|
thread_t *active_thread = (thread_t *)sched_active_thread;
|
2014-10-18 18:41:40 +02:00
|
|
|
|
|
|
|
/* 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);
|
2016-01-04 22:29:34 +01:00
|
|
|
thread_t *next_thread = clist_get_container(sched_runqueues[nextrq], thread_t, rq_entry);
|
2014-10-18 18:41:40 +02:00
|
|
|
|
2014-11-11 17:55:09 +01:00
|
|
|
DEBUG("sched_run: active thread: %" PRIkernel_pid ", next thread: %" PRIkernel_pid "\n",
|
2014-10-18 18:41:40 +02:00
|
|
|
(active_thread == NULL) ? KERNEL_PID_UNDEF : active_thread->pid,
|
|
|
|
next_thread->pid);
|
|
|
|
|
|
|
|
if (active_thread == next_thread) {
|
2014-11-11 17:55:09 +01:00
|
|
|
DEBUG("sched_run: done, sched_active_thread was not changed.\n");
|
2014-10-18 18:41:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:59:16 +02:00
|
|
|
#ifdef MODULE_SCHEDSTATISTICS
|
|
|
|
unsigned long time = xtimer_now();
|
2014-07-07 14:48:07 +02:00
|
|
|
#endif
|
|
|
|
|
2014-10-18 18:41:40 +02:00
|
|
|
if (active_thread) {
|
|
|
|
if (active_thread->status == STATUS_RUNNING) {
|
|
|
|
active_thread->status = STATUS_PENDING;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SCHED_TEST_STACK
|
2014-10-18 18:41:40 +02:00
|
|
|
if (*((uintptr_t *) active_thread->stack_start) != (uintptr_t) active_thread->stack_start) {
|
2015-02-25 16:31:03 +01:00
|
|
|
LOG_WARNING("scheduler(): stack overflow detected, pid=%" PRIkernel_pid "\n", active_thread->pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-29 19:59:16 +02:00
|
|
|
#ifdef MODULE_SCHEDSTATISTICS
|
2014-10-18 18:41:40 +02:00
|
|
|
schedstat *active_stat = &sched_pidlist[active_thread->pid];
|
|
|
|
if (active_stat->laststart) {
|
|
|
|
active_stat->runtime_ticks += time - active_stat->laststart;
|
2014-07-07 14:48:07 +02:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
2014-07-07 14:48:07 +02:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2015-07-29 19:59:16 +02:00
|
|
|
#ifdef MODULE_SCHEDSTATISTICS
|
2014-10-18 18:41:40 +02:00
|
|
|
schedstat *next_stat = &sched_pidlist[next_thread->pid];
|
|
|
|
next_stat->laststart = time;
|
|
|
|
next_stat->schedules++;
|
|
|
|
if (sched_cb) {
|
|
|
|
sched_cb(time, next_thread->pid);
|
2014-06-10 16:55:04 +02:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-10-18 18:41:40 +02:00
|
|
|
next_thread->status = STATUS_RUNNING;
|
|
|
|
sched_active_pid = next_thread->pid;
|
2016-01-04 22:29:34 +01:00
|
|
|
sched_active_thread = (volatile thread_t *) next_thread;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-11-11 17:55:09 +01:00
|
|
|
DEBUG("sched_run: done, changed sched_active_thread.\n");
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-10-18 18:41:40 +02:00
|
|
|
return 1;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:59:16 +02:00
|
|
|
#ifdef MODULE_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
|
|
|
|
2016-01-04 22:29:34 +01:00
|
|
|
void sched_set_status(thread_t *process, unsigned int status)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-02-17 12:28:54 +01:00
|
|
|
if (status >= STATUS_ON_RUNQUEUE) {
|
|
|
|
if (!(process->status >= STATUS_ON_RUNQUEUE)) {
|
2014-11-11 17:55:09 +01:00
|
|
|
DEBUG("sched_set_status: adding thread %" PRIkernel_pid " to runqueue %" PRIu16 ".\n",
|
2014-11-05 11:29:25 +01:00
|
|
|
process->pid, 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) {
|
2014-11-11 17:55:09 +01:00
|
|
|
DEBUG("sched_set_status: removing thread %" PRIkernel_pid " to runqueue %" PRIu16 ".\n",
|
2014-11-05 11:29:25 +01:00
|
|
|
process->pid, 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-04-25 18:20:42 +02:00
|
|
|
void sched_switch(uint16_t other_prio)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2016-01-04 22:29:34 +01:00
|
|
|
thread_t *active_thread = (thread_t *) sched_active_thread;
|
2014-11-05 11:29:25 +01:00
|
|
|
uint16_t current_prio = active_thread->priority;
|
2014-11-05 11:38:40 +01:00
|
|
|
int on_runqueue = (active_thread->status >= STATUS_ON_RUNQUEUE);
|
2014-02-15 18:49:49 +01:00
|
|
|
|
2014-11-05 11:38:40 +01:00
|
|
|
DEBUG("sched_switch: active pid=%" PRIkernel_pid" prio=%" PRIu16 " on_runqueue=%i "
|
2014-11-10 07:01:23 +01:00
|
|
|
", other_prio=%" PRIu16 "\n",
|
|
|
|
active_thread->pid, current_prio, on_runqueue, other_prio);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-11-05 11:38:40 +01:00
|
|
|
if (!on_runqueue || (current_prio > other_prio)) {
|
2014-11-10 07:01:23 +01:00
|
|
|
if (inISR()) {
|
2014-11-05 11:38:40 +01:00
|
|
|
DEBUG("sched_switch: setting sched_context_switch_request.\n");
|
2010-11-11 09:55:08 +01:00
|
|
|
sched_context_switch_request = 1;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-05 11:38:40 +01:00
|
|
|
DEBUG("sched_switch: yielding immediately.\n");
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
2010-11-11 09:55:08 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-05 11:38:40 +01:00
|
|
|
else {
|
|
|
|
DEBUG("sched_switch: continuing without yield.\n");
|
|
|
|
}
|
2010-11-11 09:55:08 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 09:41:37 +02:00
|
|
|
NORETURN void sched_task_exit(void)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-11-11 17:55:09 +01:00
|
|
|
DEBUG("sched_task_exit: ending thread %" PRIkernel_pid "...\n", sched_active_thread->pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-12-04 17:17:24 +01:00
|
|
|
(void) disableIRQ();
|
2014-08-13 09:13:32 +02:00
|
|
|
sched_threads[sched_active_pid] = NULL;
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_num_threads--;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2016-01-04 22:29:34 +01:00
|
|
|
sched_set_status((thread_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
|
|
|
}
|