2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2014-04-07 11:16:32 +02:00
|
|
|
* Copyright (C) 2014 Freie Universität Berlin
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2014-08-23 15:43:13 +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
|
|
|
*/
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
2013-11-27 16:28:31 +01:00
|
|
|
* @defgroup core_sched Scheduler
|
|
|
|
* @ingroup core
|
|
|
|
* @brief The RIOT scheduler
|
2014-07-16 21:31:34 +02:00
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* RIOT features a tickless, preemptive, priority based scheduler.
|
|
|
|
* Context switches can occur either preemptively (i.e. on interrupts),
|
|
|
|
* voluntarily, or when a blocking operation (like `msg_receive()`) is
|
|
|
|
* executed.
|
|
|
|
* Being tickless means it does not have a timer that fires
|
|
|
|
* periodically in order to emulate concurrent execution by switching
|
|
|
|
* threads continuously.
|
|
|
|
*
|
|
|
|
* ## Priorities:
|
|
|
|
*
|
|
|
|
* Every thread is given a priority on creation. The priority values
|
|
|
|
* are "order" or "nice" values, i.e. a higher value means a lower
|
|
|
|
* priority.
|
|
|
|
*
|
|
|
|
* ### Example:
|
|
|
|
*
|
|
|
|
* Given threads with priorities A=6, B=1, and C=3, B has the highest
|
|
|
|
* priority.
|
2015-02-18 10:59:37 +01:00
|
|
|
*
|
2014-07-16 21:31:34 +02:00
|
|
|
* A higher priority means that the scheduler will run this thread
|
|
|
|
* whenever it becomes runnable instead of a thread with a lower
|
|
|
|
* priority.
|
|
|
|
* In case of equal priorities, the threads are scheduled in a
|
|
|
|
* semi-cooperative fashion. That means that unless an interrupt
|
|
|
|
* happens, threads with the same priority will only switch due to
|
|
|
|
* voluntary or implicit context switches.
|
|
|
|
*
|
|
|
|
* ## Interrupts:
|
|
|
|
*
|
|
|
|
* When an interrupt occurs, e.g. because a timer fired or a network
|
|
|
|
* packet was received, the active context is saved and an interrupt
|
|
|
|
* service routine (ISR) that handles the interrupt is executed in
|
|
|
|
* another context.
|
|
|
|
* When the ISR is finished, the `::sched_context_switch_request` flag
|
|
|
|
* can be checked. In case it is set, the `sched_run()` function is
|
|
|
|
* called to determine the next active thread. (In the special case
|
|
|
|
* that the ISR knows that it can not enable a thread switch, this
|
|
|
|
* check can of course be omitted.)
|
|
|
|
* If the flag is not set, the original context is being restored and
|
|
|
|
* the thread resumes immediately.
|
|
|
|
*
|
|
|
|
* ## Voluntary Context Switches:
|
|
|
|
*
|
|
|
|
* There are two function calls that can lead to a voluntary context
|
|
|
|
* switch: `thread_yield()` and `thread_sleep()`.
|
|
|
|
* While the latter disables (think blocks) the thread until it is
|
|
|
|
* woken (think unblocked) again via `thread_wakeup()`, the former only
|
|
|
|
* leads to a context switch in case there is another runnable thread
|
|
|
|
* with at least the same priority.
|
|
|
|
*
|
|
|
|
* ## Implicit Context Switches:
|
|
|
|
*
|
|
|
|
* Some functions that unblock another thread, e.g. `msg_send()` or
|
|
|
|
* `mutex_unlock()`, can cause a thread switch, if the target had a
|
|
|
|
* higher priority.
|
|
|
|
*
|
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @{
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2013-06-21 22:37:14 +02:00
|
|
|
* @file sched.h
|
2014-08-05 17:08:53 +02:00
|
|
|
* @brief Scheduler API definition
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _SCHEDULER_H
|
|
|
|
#define _SCHEDULER_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "bitarithm.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "tcb.h"
|
2014-04-30 09:41:37 +02:00
|
|
|
#include "attributes.h"
|
2014-08-13 09:08:50 +02:00
|
|
|
#include "kernel_types.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-10-13 14:44:28 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-04-07 11:16:32 +02:00
|
|
|
/**
|
|
|
|
* @def SCHED_PRIO_LEVELS
|
|
|
|
* @brief The number of thread priority levels
|
|
|
|
*/
|
2014-08-18 10:42:12 +02:00
|
|
|
#ifndef SCHED_PRIO_LEVELS
|
2010-09-22 15:10:42 +02:00
|
|
|
#define SCHED_PRIO_LEVELS 16
|
|
|
|
#endif
|
|
|
|
|
2013-08-14 13:04:19 +02:00
|
|
|
/**
|
2014-05-14 10:46:15 +02:00
|
|
|
* @brief Triggers the scheduler to schedule the next thread
|
2014-10-18 18:41:40 +02:00
|
|
|
* @returns 1 if sched_active_thread/sched_active_pid was changed, 0 otherwise.
|
2013-08-14 13:04:19 +02:00
|
|
|
*/
|
2014-10-18 18:41:40 +02:00
|
|
|
int sched_run(void);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-08-14 13:04:19 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the status of the specified process
|
|
|
|
*
|
2014-02-11 18:15:43 +01:00
|
|
|
* @param[in] process Pointer to the thread control block of the
|
2013-08-14 13:04:19 +02:00
|
|
|
* targeted process
|
|
|
|
* @param[in] status The new status of this thread
|
|
|
|
*/
|
2011-03-08 11:43:21 +01:00
|
|
|
void sched_set_status(tcb_t *process, unsigned int status);
|
2013-08-14 13:04:19 +02:00
|
|
|
|
|
|
|
/**
|
2014-11-05 11:38:40 +01:00
|
|
|
* @brief Yield if approriate.
|
2013-08-14 13:04:19 +02:00
|
|
|
*
|
2014-11-05 11:38:40 +01:00
|
|
|
* @details Either yield if other_prio is higher than the current priority,
|
|
|
|
* or if the current thread is not on the runqueue.
|
|
|
|
*
|
|
|
|
* Depending on whether the current execution is in an ISR (inISR()),
|
|
|
|
* thread_yield_higher() is called or @ref sched_context_switch_request is set,
|
|
|
|
* respectively.
|
|
|
|
*
|
|
|
|
* @param[in] other_prio The priority of the target thread.
|
2014-02-11 18:15:43 +01:00
|
|
|
*/
|
2014-04-25 18:20:42 +02:00
|
|
|
void sched_switch(uint16_t other_prio);
|
2013-08-14 13:04:19 +02:00
|
|
|
|
|
|
|
/**
|
2014-04-08 21:32:21 +02:00
|
|
|
* @brief Call context switching at thread exit
|
2013-08-14 13:04:19 +02:00
|
|
|
*/
|
2014-04-30 09:41:37 +02:00
|
|
|
NORETURN void cpu_switch_context_exit(void);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-08-14 13:04:19 +02:00
|
|
|
/**
|
2013-08-14 14:15:11 +02:00
|
|
|
* Flag indicating whether a context switch is necessary after handling an
|
|
|
|
* interrupt. Supposed to be set in an ISR.
|
2013-08-14 13:04:19 +02:00
|
|
|
*/
|
2011-06-24 17:53:09 +02:00
|
|
|
extern volatile unsigned int sched_context_switch_request;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-08-14 13:04:19 +02:00
|
|
|
/**
|
|
|
|
* Thread table
|
|
|
|
*/
|
2014-08-13 09:33:25 +02:00
|
|
|
extern volatile tcb_t *sched_threads[KERNEL_PID_LAST + 1];
|
2013-08-14 13:04:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently active thread
|
|
|
|
*/
|
2014-04-10 22:28:35 +02:00
|
|
|
extern volatile tcb_t *sched_active_thread;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-08-14 13:04:19 +02:00
|
|
|
/**
|
|
|
|
* Number of running (non-terminated) threads
|
|
|
|
*/
|
2014-04-10 22:28:35 +02:00
|
|
|
extern volatile int sched_num_threads;
|
2013-08-14 13:04:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process ID of active thread
|
|
|
|
*/
|
2014-07-06 22:57:56 +02:00
|
|
|
extern volatile kernel_pid_t sched_active_pid;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-10-18 01:24:49 +02:00
|
|
|
/**
|
2014-10-28 00:53:49 +01:00
|
|
|
* List of runqueues per priority level
|
2014-10-18 01:24:49 +02:00
|
|
|
*/
|
2014-10-28 00:53:49 +01:00
|
|
|
extern clist_node_t *sched_runqueues[SCHED_PRIO_LEVELS];
|
2014-10-18 01:24:49 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
#if SCHEDSTATISTICS
|
2013-08-14 13:04:19 +02:00
|
|
|
/**
|
|
|
|
* Scheduler statistics
|
|
|
|
*/
|
2013-06-20 18:18:29 +02:00
|
|
|
typedef struct {
|
2014-04-30 19:17:05 +02:00
|
|
|
unsigned int laststart; /**< Time stamp of the last time this thread was
|
|
|
|
scheduled to run */
|
|
|
|
unsigned int schedules; /**< How often the thread was scheduled to run */
|
|
|
|
unsigned long runtime_ticks; /**< The total runtime of this thread in ticks */
|
2013-06-20 18:18:29 +02:00
|
|
|
} schedstat;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-08-14 13:04:19 +02:00
|
|
|
/**
|
|
|
|
* Thread statistics table
|
|
|
|
*/
|
2014-08-13 09:33:25 +02:00
|
|
|
extern schedstat sched_pidlist[KERNEL_PID_LAST + 1];
|
2013-08-14 10:58:08 +02:00
|
|
|
|
2013-08-14 13:04:19 +02:00
|
|
|
/**
|
2014-04-07 11:16:32 +02:00
|
|
|
* @brief Register a callback that will be called on every scheduler run
|
|
|
|
*
|
|
|
|
* @param[in] callback The callback functions the will be called
|
2013-08-14 13:04:19 +02:00
|
|
|
*/
|
2013-08-14 10:58:08 +02:00
|
|
|
void sched_register_cb(void (*callback)(uint32_t, uint32_t));
|
2014-04-07 11:16:32 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
|
|
|
|
2014-10-09 01:18:16 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif // _SCHEDULER_H
|
2014-04-07 11:16:32 +02:00
|
|
|
/** @} */
|