ChibiOS  21.6.0
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
147 extern "C" {
148 #endif
149  void chSchObjectInit(os_instance_t *oip,
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
175 static 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  (((thread_t *)cp)->hdr.pqueue.prio >= ((thread_t *)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 /** @} */
ch_queue::next
ch_queue_t * next
Next in the list/queue.
Definition: chlists.h:70
chSchReadyI
thread_t * chSchReadyI(thread_t *tp)
Inserts a thread in the Ready List placing it behind its peers.
Definition: chschd.c:276
ch_os_instance
System instance data structure.
Definition: chobjects.h:394
tstate_t
uint8_t tstate_t
Definition: chearly.h:84
msg_t
int32_t msg_t
Definition: chearly.h:88
chSchGoSleepTimeoutS
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:355
chSchPreemption
void chSchPreemption(void)
All-in-one preemption code.
Definition: chschd.c:564
chSchDoYieldS
void chSchDoYieldS(void)
Yields the time slot.
Definition: chschd.c:598
ch_thread
Structure representing a thread.
Definition: chobjects.h:156
chSchGoSleepS
void chSchGoSleepS(tstate_t newstate)
Puts the current thread to sleep into the specified state.
Definition: chschd.c:301
ch_queue
Structure representing a generic bidirectional linked list header and element.
Definition: chlists.h:69
chSchRescheduleS
void chSchRescheduleS(void)
Performs a reschedule if a higher priority thread is runnable.
Definition: chschd.c:454
ch_sch_prio_insert
void ch_sch_prio_insert(ch_queue_t *qp, ch_queue_t *tp)
Inserts a thread into a priority ordered queue.
Definition: chschd.c:246
chSchSelectFirstI
thread_t * chSchSelectFirstI(void)
Makes runnable the fist thread in the ready list, does not reschedule internally.
Definition: chschd.c:623
chSchDoPreemption
void chSchDoPreemption(void)
Switches to the first thread on the runnable queue.
Definition: chschd.c:513
chSchIsPreemptionRequired
bool chSchIsPreemptionRequired(void)
Evaluates if preemption is required.
Definition: chschd.c:481
sysinterval_t
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:119
ch_os_instance_config
Type of an system instance configuration.
Definition: chobjects.h:363
chSchWakeupS
void chSchWakeupS(thread_t *ntp, msg_t msg)
Wakes up a thread.
Definition: chschd.c:393