ChibiOS 21.11.5
chschd.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file rt/include/chschd.h
21 * @brief Scheduler macros and structures.
22 *
23 * @addtogroup scheduler
24 * @{
25 */
26
27#ifndef CHSCHD_H
28#define CHSCHD_H
29
30/*===========================================================================*/
31/* Module constants. */
32/*===========================================================================*/
33
34/**
35 * @name Wakeup status codes
36 * @{
37 */
38#define MSG_OK (msg_t)0 /**< @brief Normal wakeup message. */
39#define MSG_TIMEOUT (msg_t)-1 /**< @brief Wakeup caused by a timeout
40 condition. */
41#define MSG_RESET (msg_t)-2 /**< @brief Wakeup caused by a reset
42 condition. */
43/** @} */
44
45/**
46 * @name Priority constants
47 * @{
48 */
49#define NOPRIO (tprio_t)0 /**< @brief Ready list header
50 priority. */
51#define IDLEPRIO (tprio_t)1 /**< @brief Idle priority. */
52#define LOWPRIO (tprio_t)2 /**< @brief Lowest priority. */
53#define NORMALPRIO (tprio_t)128 /**< @brief Normal priority. */
54#define HIGHPRIO (tprio_t)255 /**< @brief Highest priority. */
55/** @} */
56
57/**
58 * @name Thread states
59 * @{
60 */
61#define CH_STATE_READY (tstate_t)0 /**< @brief Waiting on the
62 ready list. */
63#define CH_STATE_CURRENT (tstate_t)1 /**< @brief Currently running. */
64#define CH_STATE_WTSTART (tstate_t)2 /**< @brief Just created. */
65#define CH_STATE_SUSPENDED (tstate_t)3 /**< @brief Suspended state. */
66#define CH_STATE_QUEUED (tstate_t)4 /**< @brief On a queue. */
67#define CH_STATE_WTSEM (tstate_t)5 /**< @brief On a semaphore. */
68#define CH_STATE_WTMTX (tstate_t)6 /**< @brief On a mutex. */
69#define CH_STATE_WTCOND (tstate_t)7 /**< @brief On a cond.variable.*/
70#define CH_STATE_SLEEPING (tstate_t)8 /**< @brief Sleeping. */
71#define CH_STATE_WTEXIT (tstate_t)9 /**< @brief Waiting a thread. */
72#define CH_STATE_WTOREVT (tstate_t)10 /**< @brief One event. */
73#define CH_STATE_WTANDEVT (tstate_t)11 /**< @brief Several events. */
74#define CH_STATE_SNDMSGQ (tstate_t)12 /**< @brief Sending a message,
75 in queue. */
76#define CH_STATE_SNDMSG (tstate_t)13 /**< @brief Sent a message,
77 waiting answer. */
78#define CH_STATE_WTMSG (tstate_t)14 /**< @brief Waiting for a
79 message. */
80#define CH_STATE_FINAL (tstate_t)15 /**< @brief Thread terminated. */
81
82/**
83 * @brief Thread states as array of strings.
84 * @details Each element in an array initialized with this macro can be
85 * indexed using the numeric thread state values.
86 */
87#define CH_STATE_NAMES \
88 "READY", "CURRENT", "WTSTART", "SUSPENDED", "QUEUED", "WTSEM", "WTMTX", \
89 "WTCOND", "SLEEPING", "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", \
90 "SNDMSG", "WTMSG", "FINAL"
91/** @} */
92
93/**
94 * @name Thread flags and attributes
95 * @{
96 */
97#define CH_FLAG_MODE_MASK (tmode_t)3U /**< @brief Thread memory mode
98 mask. */
99#define CH_FLAG_MODE_STATIC (tmode_t)0U /**< @brief Static thread. */
100#define CH_FLAG_MODE_HEAP (tmode_t)1U /**< @brief Thread allocated
101 from a Memory Heap. */
102#define CH_FLAG_MODE_MPOOL (tmode_t)2U /**< @brief Thread allocated
103 from a Memory Pool. */
104#define CH_FLAG_TERMINATE (tmode_t)4U /**< @brief Termination requested
105 flag. */
106/** @} */
107
108/*===========================================================================*/
109/* Module pre-compile time settings. */
110/*===========================================================================*/
111
112/*===========================================================================*/
113/* Derived constants and error checks. */
114/*===========================================================================*/
115
116/*===========================================================================*/
117/* Module data structures and types. */
118/*===========================================================================*/
119
120/*===========================================================================*/
121/* Module macros. */
122/*===========================================================================*/
123
124/**
125 * @brief Returns the priority of the first thread on the given ready list.
126 *
127 * @notapi
128 */
129#define firstprio(rlp) ((rlp)->next->prio)
130
131/**
132 * @brief Current thread pointer get macro.
133 * @note This macro is not meant to be used in the application code but
134 * only from within the kernel, use @p chThdGetSelfX() instead.
135 */
136#define __sch_get_currthread() __instance_get_currthread(currcore)
137
138/*===========================================================================*/
139/* External declarations. */
140/*===========================================================================*/
141
142/*
143 * Scheduler APIs.
144 */
145#ifdef __cplusplus
146extern "C" {
147#endif
149 const os_instance_config_t *oicp);
151 void chSchGoSleepS(tstate_t newstate);
153 void chSchWakeupS(thread_t *ntp, msg_t msg);
154 void chSchRescheduleS(void);
155 bool chSchIsPreemptionRequired(void);
156 void chSchDoPreemption(void);
157 void chSchPreemption(void);
158 void chSchDoYieldS(void);
160#if CH_CFG_OPTIMIZE_SPEED == FALSE
162#endif /* CH_CFG_OPTIMIZE_SPEED == FALSE */
163#ifdef __cplusplus
164}
165#endif
166
167/*===========================================================================*/
168/* Module inline functions. */
169/*===========================================================================*/
170
171/* If the performance code path has been chosen then all the following
172 functions are inlined into the various kernel modules.*/
173#if CH_CFG_OPTIMIZE_SPEED == TRUE
174static inline void ch_sch_prio_insert(ch_queue_t *qp, ch_queue_t *tp) {
175
176 ch_queue_t *cp = qp;
177 do {
178 cp = cp->next;
179 } while ((cp != qp) &&
180 (threadref(cp)->hdr.pqueue.prio >= threadref(tp)->hdr.pqueue.prio));
181 tp->next = cp;
182 tp->prev = cp->prev;
183 tp->prev->next = tp;
184 cp->prev = tp;
185}
186#endif /* CH_CFG_OPTIMIZE_SPEED == TRUE */
187
188#endif /* CHSCHD_H */
189
190/** @} */
#define chSchWakeupS(ntp, msg)
Wakes up a thread.
#define chSchGoSleepS(newstate)
Puts the current thread to sleep into the specified state.
struct ch_queue ch_queue_t
Type of a generic bidirectional linked list header and element.
Definition chlists.h:62
#define threadref(p)
Safe cast of a queue pointer to a thread pointer.
Definition chearly.h:205
int32_t msg_t
Definition chearly.h:87
uint8_t tstate_t
Definition chearly.h:83
struct ch_os_instance os_instance_t
Type of an OS instance structure.
Definition chearly.h:137
struct ch_os_instance_config os_instance_config_t
Type of an system instance configuration.
struct ch_thread thread_t
Type of a thread structure.
Definition chearly.h:132
thread_t * chSchSelectFirst(void)
Makes runnable the fist thread in the ready list, does not reschedule internally.
Definition chschd.c:626
void chSchRescheduleS(void)
Performs a reschedule if a higher priority thread is runnable.
Definition chschd.c:457
thread_t * chSchReadyI(thread_t *tp)
Inserts a thread in the Ready List placing it behind its peers.
Definition chschd.c:279
void chSchObjectInit(os_instance_t *oip, const os_instance_config_t *oicp)
void chSchDoYieldS(void)
Yields the time slot.
Definition chschd.c:601
void chSchPreemption(void)
All-in-one preemption code.
Definition chschd.c:567
bool chSchIsPreemptionRequired(void)
Evaluates if preemption is required.
Definition chschd.c:484
void ch_sch_prio_insert(ch_queue_t *qp, ch_queue_t *tp)
Inserts a thread into a priority ordered queue.
Definition chschd.c:249
void chSchDoPreemption(void)
Switches to the first thread on the runnable queue.
Definition chschd.c:516
msg_t chSchGoSleepTimeoutS(tstate_t newstate, sysinterval_t timeout)
Puts the current thread to sleep into the specified state with timeout specification.
Definition chschd.c:358
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
ch_queue_t * prev
Previous in the queue.
Definition chlists.h:70
ch_queue_t * next
Next in the list/queue.
Definition chlists.h:69