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