The RIOT scheduler. More...
The RIOT scheduler.
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.
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.
Given threads with priorities A=6, B=1, and C=3, B has the highest priority.
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.
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.
There are two function calls that can lead to a voluntary context switch:
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.
|Add definitions required on the native board. |
|Scheduler API definition. |
|The number of thread priority levels. |
|typedef struct _thread||thread_t|
|forward declaration for thread_t, defined in thread.h |
|Compilation with g++ may require the declaration of this function. More...|
|Triggers the scheduler to schedule the next thread. More...|
|void||sched_set_status (thread_t *process, unsigned int status)|
|Set the status of the specified process. More...|
|void||sched_switch (uint16_t other_prio)|
|Yield if approriate. More...|
|NORETURN void||cpu_switch_context_exit (void)|
|Call context switching at thread exit. |
|NORETURN void||sched_task_exit (void)|
|Removes thread from scheduler and set status to STATUS_STOPPED. |
|volatile unsigned int||sched_context_switch_request|
|Flag indicating whether a context switch is necessary after handling an interrupt. More...|
|volatile thread_t *||sched_threads [KERNEL_PID_LAST+1]|
|Thread table. |
|volatile thread_t *||sched_active_thread|
|Currently active thread. |
|Number of running (non-terminated) threads. |
|Process ID of active thread. |
|List of runqueues per priority level. |
Triggers the scheduler to schedule the next thread.
|void sched_set_status||(||thread_t *||process,|
Set the status of the specified process.
|[in]||process||Pointer to the thread control block of the targeted process|
|[in]||status||The new status of this thread|
Yield if approriate.
Either yield if other_prio is higher than the current priority, or if the current thread is not on the runqueue.
|[in]||other_prio||The priority of the target thread.|
Compilation with g++ may require the declaration of this function.
If implementation of this function is required, it can be realized in thread_arch.c.
|volatile unsigned int sched_context_switch_request|
Flag indicating whether a context switch is necessary after handling an interrupt.
Supposed to be set in an ISR.