sched.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014-2017 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
81 #ifndef SCHED_H
82 #define SCHED_H
83 
84 #include <stddef.h>
85 #include <inttypes.h>
86 
87 #include "kernel_defines.h"
88 #include "native_sched.h"
89 #include "clist.h"
90 
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94 
99 #ifndef MAXTHREADS
100 #if defined(MODULE_CORE_THREAD)
101 #define MAXTHREADS 32
102 #else
103 #define MAXTHREADS 0
104 #endif
105 #endif
106 
110 #define KERNEL_PID_UNDEF 0
111 
115 #define KERNEL_PID_FIRST (KERNEL_PID_UNDEF + 1)
116 
120 #define KERNEL_PID_LAST (KERNEL_PID_FIRST + MAXTHREADS - 1)
121 
125 #define PRIkernel_pid PRIi16
126 
127 #if defined(DEVELHELP) || defined(DOXYGEN)
131 #ifndef SCHED_TEST_STACK
132 #define SCHED_TEST_STACK 1
133 #endif /* SCHED_TEST_STACK */
134 #endif /* DEVELHELP */
135 
139 typedef int16_t kernel_pid_t;
140 
148 static inline int pid_is_valid(kernel_pid_t pid)
149 {
150  return ((KERNEL_PID_FIRST <= pid) && (pid <= KERNEL_PID_LAST));
151 }
155 typedef struct _thread thread_t;
156 
163 typedef enum {
177  STATUS_NUMOF
185 #define STATUS_ON_RUNQUEUE STATUS_RUNNING
187 #define STATUS_NOT_FOUND ((thread_status_t)~0)
193 #ifndef SCHED_PRIO_LEVELS
194 #define SCHED_PRIO_LEVELS 16
195 #endif
196 
205 
214 
227 void sched_switch(uint16_t other_prio);
228 
233 
238 extern volatile unsigned int sched_context_switch_request;
239 
243 extern volatile thread_t *sched_threads[KERNEL_PID_LAST + 1];
244 
248 extern volatile int sched_num_threads;
249 
254 
259 
271 void sched_change_priority(thread_t *thread, uint8_t priority);
272 
286 void sched_arch_idle(void);
287 
288 #if IS_USED(MODULE_SCHED_CB) || defined(DOXYGEN)
298 typedef void (*sched_callback_t)(kernel_pid_t active, kernel_pid_t next);
299 
306 #endif /* MODULE_SCHED_CB */
307 
323 static inline void sched_runq_advance(uint8_t prio)
324 {
326 }
327 
328 #if (IS_USED(MODULE_SCHED_RUNQ_CALLBACK)) || defined(DOXYGEN)
345 extern void sched_runq_callback(uint8_t prio);
346 #endif
347 
355 static inline int sched_runq_is_empty(uint8_t prio)
356 {
357  return clist_is_empty(&sched_runqueues[prio]);
358 }
359 
367 static inline int sched_runq_exactly_one(uint8_t prio)
368 {
369  return clist_exactly_one(&sched_runqueues[prio]);
370 }
371 
379 static inline int sched_runq_more_than_one(uint8_t prio)
380 {
381  return clist_more_than_one(&sched_runqueues[prio]);
382 }
383 
384 #ifdef __cplusplus
385 }
386 #endif
387 
388 #endif /* SCHED_H */
Circular linked list.
static bool clist_more_than_one(clist_node_t *list)
Tells if a list has more than one element.
Definition: clist.h:494
static bool clist_is_empty(const clist_node_t *list)
Checks if *list is empty.
Definition: clist.h:118
static void clist_lpoprpush(clist_node_t *list)
Advances the circle list.
Definition: clist.h:203
static bool clist_exactly_one(clist_node_t *list)
Tells if a list has exactly one element.
Definition: clist.h:480
#define NORETURN
The NORETURN keyword tells the compiler to assume that the function cannot return.
#define SCHED_PRIO_LEVELS
The number of thread priority levels.
Definition: sched.h:194
static int sched_runq_more_than_one(uint8_t prio)
Tell if the number of threads in a runqueue greater than 1.
Definition: sched.h:379
void sched_register_cb(sched_callback_t callback)
Register a callback that will be called on every scheduler run.
void sched_arch_idle(void)
Set CPU to idle mode (CPU dependent)
static int pid_is_valid(kernel_pid_t pid)
Determine if the given pid is valid.
Definition: sched.h:148
volatile int sched_num_threads
Number of running (non-terminated) threads.
void sched_runq_callback(uint8_t prio)
Scheduler runqueue (change) callback.
static int sched_runq_exactly_one(uint8_t prio)
Tell if the number of threads in a runqueue is 1.
Definition: sched.h:367
void(* sched_callback_t)(kernel_pid_t active, kernel_pid_t next)
Scheduler run callback.
Definition: sched.h:298
#define KERNEL_PID_LAST
The last valid PID (inclusive).
Definition: sched.h:120
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:139
volatile unsigned int sched_context_switch_request
Flag indicating whether a context switch is necessary after handling an interrupt.
void sched_switch(uint16_t other_prio)
Yield if appropriate.
void sched_set_status(thread_t *process, thread_status_t status)
Set the status of the specified process.
NORETURN void sched_task_exit(void)
Removes thread from scheduler and set status to STATUS_STOPPED.
void sched_change_priority(thread_t *thread, uint8_t priority)
Change the priority of the given thread.
volatile thread_t * sched_threads[KERNEL_PID_LAST+1]
Thread table.
thread_status_t
Definition: sched.h:163
thread_t * sched_run(void)
Triggers the scheduler to schedule the next thread.
static int sched_runq_is_empty(uint8_t prio)
Tell if the number of threads in a runqueue is 0.
Definition: sched.h:355
static void sched_runq_advance(uint8_t prio)
Advance a runqueue.
Definition: sched.h:323
NORETURN void cpu_switch_context_exit(void)
Call context switching at thread exit.
#define KERNEL_PID_FIRST
The first valid PID (inclusive).
Definition: sched.h:115
clist_node_t sched_runqueues[SCHED_PRIO_LEVELS]
List of runqueues per priority level.
@ STATUS_COND_BLOCKED
waiting for a condition variable
Definition: sched.h:174
@ STATUS_RUNNING
currently running
Definition: sched.h:175
@ STATUS_MUTEX_BLOCKED
waiting for a locked mutex
Definition: sched.h:167
@ STATUS_STOPPED
has terminated
Definition: sched.h:164
@ STATUS_SLEEPING
sleeping
Definition: sched.h:166
@ STATUS_FLAG_BLOCKED_ANY
waiting for any flag from flag_mask
Definition: sched.h:171
@ STATUS_SEND_BLOCKED
waiting for message to be delivered
Definition: sched.h:169
@ STATUS_MBOX_BLOCKED
waiting for get/put on mbox
Definition: sched.h:173
@ STATUS_PENDING
waiting to be scheduled to run
Definition: sched.h:176
@ STATUS_NUMOF
number of supported thread states
Definition: sched.h:177
@ STATUS_REPLY_BLOCKED
waiting for a message response
Definition: sched.h:170
@ STATUS_ZOMBIE
has terminated & keeps thread's thread_t
Definition: sched.h:165
@ STATUS_FLAG_BLOCKED_ALL
waiting for all flags in flag_mask
Definition: sched.h:172
@ STATUS_RECEIVE_BLOCKED
waiting for a message
Definition: sched.h:168
Adds include for missing inttype definitions.
Common macros and compiler attributes/pragmas configuration.
Add definitions required on the native board.
thread_t holds thread's context data.
Definition: thread.h:168
thread_status_t status
thread's status
Definition: thread.h:170
uint8_t priority
thread's priority
Definition: thread.h:171
List node structure.
Definition: list.h:40