sched.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 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 
80 #ifndef SCHED_H
81 #define SCHED_H
82 
83 #include <stddef.h>
84 #include <inttypes.h>
85 
86 #include "kernel_defines.h"
87 #include "native_sched.h"
88 #include "clist.h"
89 
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93 
98 #ifndef MAXTHREADS
99 #define MAXTHREADS 32
100 #endif
101 
105 #define KERNEL_PID_UNDEF 0
106 
110 #define KERNEL_PID_FIRST (KERNEL_PID_UNDEF + 1)
111 
115 #define KERNEL_PID_LAST (KERNEL_PID_FIRST + MAXTHREADS - 1)
116 
120 #define PRIkernel_pid PRIi16
121 
125 typedef int16_t kernel_pid_t;
126 
134 static inline int pid_is_valid(kernel_pid_t pid)
135 {
136  return ((KERNEL_PID_FIRST <= pid) && (pid <= KERNEL_PID_LAST));
137 }
141 typedef struct _thread thread_t;
142 
149 typedef enum {
171 #define STATUS_ON_RUNQUEUE STATUS_RUNNING
173 #define STATUS_NOT_FOUND ((thread_status_t)-1)
179 #ifndef SCHED_PRIO_LEVELS
180 #define SCHED_PRIO_LEVELS 16
181 #endif
182 
190 thread_t *sched_run(void);
191 
200 
213 void sched_switch(uint16_t other_prio);
214 
219 
224 extern volatile unsigned int sched_context_switch_request;
225 
229 extern volatile thread_t *sched_threads[KERNEL_PID_LAST + 1];
230 
234 extern volatile int sched_num_threads;
235 
240 
244 NORETURN void sched_task_exit(void);
245 
259 void sched_arch_idle(void);
260 
261 #if IS_USED(MODULE_SCHED_CB) || defined(DOXYGEN)
262 
271 typedef void (*sched_callback_t)(kernel_pid_t active, kernel_pid_t next);
272 
278 void sched_register_cb(sched_callback_t callback);
279 #endif /* MODULE_SCHED_CB */
280 
281 #ifdef __cplusplus
282 }
283 #endif
284 
285 #endif /* SCHED_H */
286 
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
STATUS_STOPPED
@ STATUS_STOPPED
has terminated
Definition: sched.h:150
native_sched.h
Add definitions required on the native board.
STATUS_REPLY_BLOCKED
@ STATUS_REPLY_BLOCKED
waiting for a message response
Definition: sched.h:156
kernel_defines.h
Common macros and compiler attributes/pragmas configuration.
sched_num_threads
volatile int sched_num_threads
Number of running (non-terminated) threads.
sched_register_cb
void sched_register_cb(sched_callback_t callback)
Register a callback that will be called on every scheduler run.
pid_is_valid
static int pid_is_valid(kernel_pid_t pid)
Determine if the given pid is valid.
Definition: sched.h:134
sched_callback_t
void(* sched_callback_t)(kernel_pid_t active, kernel_pid_t next)
Scheduler run callback.
Definition: sched.h:271
STATUS_RUNNING
@ STATUS_RUNNING
currently running
Definition: sched.h:161
STATUS_RECEIVE_BLOCKED
@ STATUS_RECEIVE_BLOCKED
waiting for a message
Definition: sched.h:154
sched_run
thread_t * sched_run(void)
Triggers the scheduler to schedule the next thread.
STATUS_ZOMBIE
@ STATUS_ZOMBIE
has terminated & keeps thread's thread_t
Definition: sched.h:151
KERNEL_PID_FIRST
#define KERNEL_PID_FIRST
The first valid PID (inclusive).
Definition: sched.h:110
sched_task_exit
NORETURN void sched_task_exit(void)
Removes thread from scheduler and set status to STATUS_STOPPED.
STATUS_NUMOF
@ STATUS_NUMOF
number of supported thread states
Definition: sched.h:163
_thread
thread_t holds thread's context data.
Definition: thread.h:154
cpu_switch_context_exit
NORETURN void cpu_switch_context_exit(void)
Call context switching at thread exit.
STATUS_PENDING
@ STATUS_PENDING
waiting to be scheduled to run
Definition: sched.h:162
NORETURN
#define NORETURN
The NORETURN keyword tells the compiler to assume that the function cannot return.
Definition: kernel_defines.h:74
sched_context_switch_request
volatile unsigned int sched_context_switch_request
Flag indicating whether a context switch is necessary after handling an interrupt.
STATUS_SEND_BLOCKED
@ STATUS_SEND_BLOCKED
waiting for message to be delivered
Definition: sched.h:155
sched_arch_idle
void sched_arch_idle(void)
Set CPU to idle mode (CPU dependent)
sched_switch
void sched_switch(uint16_t other_prio)
Yield if appropriate.
STATUS_MBOX_BLOCKED
@ STATUS_MBOX_BLOCKED
waiting for get/put on mbox
Definition: sched.h:159
clist.h
Circular linked list.
STATUS_SLEEPING
@ STATUS_SLEEPING
sleeping
Definition: sched.h:152
STATUS_MUTEX_BLOCKED
@ STATUS_MUTEX_BLOCKED
waiting for a locked mutex
Definition: sched.h:153
thread_status_t
thread_status_t
Definition: sched.h:149
SCHED_PRIO_LEVELS
#define SCHED_PRIO_LEVELS
The number of thread priority levels.
Definition: sched.h:180
sched_runqueues
clist_node_t sched_runqueues[SCHED_PRIO_LEVELS]
List of runqueues per priority level.
list_node
List node structure.
Definition: list.h:40
sched_threads
volatile thread_t * sched_threads[KERNEL_PID_LAST+1]
Thread table.
KERNEL_PID_LAST
#define KERNEL_PID_LAST
The last valid PID (inclusive).
Definition: sched.h:115
sched_set_status
void sched_set_status(thread_t *process, thread_status_t status)
Set the status of the specified process.
STATUS_COND_BLOCKED
@ STATUS_COND_BLOCKED
waiting for a condition variable
Definition: sched.h:160
inttypes.h
Adds include for missing inttype definitions.
STATUS_FLAG_BLOCKED_ALL
@ STATUS_FLAG_BLOCKED_ALL
waiting for all flags in flag_mask
Definition: sched.h:158
_thread::status
thread_status_t status
thread's status
Definition: thread.h:156
STATUS_FLAG_BLOCKED_ANY
@ STATUS_FLAG_BLOCKED_ANY
waiting for any flag from flag_mask
Definition: sched.h:157