amiro-os / core / inc / aos_thread.h @ 2a9f9ad7
History | View | Annotate | Download (8.058 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | 84f0ce9e | Thomas Schöpping | Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 | e545e620 | Thomas Schöpping | |
5 | This program is free software: you can redistribute it and/or modify
|
||
6 | it under the terms of the GNU General Public License as published by
|
||
7 | the Free Software Foundation, either version 3 of the License, or
|
||
8 | (at your option) any later version.
|
||
9 | |||
10 | This program 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 | 53710ca3 | Marc Rothmann | /**
|
20 | * @file aos_thread.h
|
||
21 | * @brief Thread macros and inline functions.
|
||
22 | *
|
||
23 | * @addtogroup aos_threads
|
||
24 | * @{
|
||
25 | */
|
||
26 | |||
27 | 6ff06bbf | Thomas Schöpping | #ifndef AMIROOS_THREAD_H
|
28 | #define AMIROOS_THREAD_H
|
||
29 | e545e620 | Thomas Schöpping | |
30 | 3940ba8a | Thomas Schöpping | #include <amiroos.h> |
31 | |||
32 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
33 | /* CONSTANTS */
|
||
34 | /******************************************************************************/
|
||
35 | e545e620 | Thomas Schöpping | |
36 | /**
|
||
37 | * @brief Minimum thread priority.
|
||
38 | */
|
||
39 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_LOWPRIO_MIN ((tprio_t)(LOWPRIO))
|
40 | e545e620 | Thomas Schöpping | |
41 | /**
|
||
42 | * @brief Maximum priority for background threads.
|
||
43 | */
|
||
44 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_LOWPRIO_MAX ((tprio_t)(LOWPRIO + ((NORMALPRIO - LOWPRIO) / 2))) |
45 | e545e620 | Thomas Schöpping | |
46 | /**
|
||
47 | * @brief Minimum priority for normal/standard threads.
|
||
48 | */
|
||
49 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_NORMALPRIO_MIN ((tprio_t)(AOS_THD_LOWPRIO_MAX + 1)) |
50 | e545e620 | Thomas Schöpping | |
51 | /**
|
||
52 | * @brief Maximum priority for normal/standard threads.
|
||
53 | */
|
||
54 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_NORMALPRIO_MAX ((tprio_t)(NORMALPRIO))
|
55 | e545e620 | Thomas Schöpping | |
56 | /**
|
||
57 | * @brief Minimum priority for important threads.
|
||
58 | */
|
||
59 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_HIGHPRIO_MIN ((tprio_t)(NORMALPRIO + 1)) |
60 | e545e620 | Thomas Schöpping | |
61 | /**
|
||
62 | * @brief Maximum priority for important threads.
|
||
63 | */
|
||
64 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_HIGHPRIO_MAX ((tprio_t)(NORMALPRIO + ((HIGHPRIO - NORMALPRIO) / 2))) |
65 | e545e620 | Thomas Schöpping | |
66 | /**
|
||
67 | * @brief Minimum priority for real-time threads.
|
||
68 | */
|
||
69 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_RTPRIO_MIN ((tprio_t)(AOS_THD_HIGHPRIO_MAX + 1)) |
70 | e545e620 | Thomas Schöpping | |
71 | /**
|
||
72 | * @brief Maximum priority for real-time threads.
|
||
73 | */
|
||
74 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_RTPRIO_MAX ((tprio_t)(HIGHPRIO - 1)) |
75 | e545e620 | Thomas Schöpping | |
76 | /**
|
||
77 | * @brief Priority for the system control thread.
|
||
78 | */
|
||
79 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_CTRLPRIO ((tprio_t)(HIGHPRIO))
|
80 | e545e620 | Thomas Schöpping | |
81 | /**
|
||
82 | * @brief Maximum timeframe that can be slept in system ticks.
|
||
83 | */
|
||
84 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_MAX_SLEEP_ST TIME_MAX_INTERVAL
|
85 | e545e620 | Thomas Schöpping | |
86 | /**
|
||
87 | * @brief Maximum timeframe that can be slept in seconds.
|
||
88 | */
|
||
89 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_MAX_SLEEP_S (chTimeI2S(AOS_THD_MAX_SLEEP_ST) - 1) |
90 | e545e620 | Thomas Schöpping | |
91 | /**
|
||
92 | * @brief Maximum timeframe that can be slept in milliseconds.
|
||
93 | */
|
||
94 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_MAX_SLEEP_MS (chTimeI2MS(AOS_THD_MAX_SLEEP_ST) - 1) |
95 | e545e620 | Thomas Schöpping | |
96 | /**
|
||
97 | * @brief Maximum timeframe that can be slept in microseconds.
|
||
98 | */
|
||
99 | 1e5f7648 | Thomas Schöpping | #define AOS_THD_MAX_SLEEP_US (chTimeI2US(AOS_THD_MAX_SLEEP_ST) - 1) |
100 | e545e620 | Thomas Schöpping | |
101 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
102 | /* SETTINGS */
|
||
103 | /******************************************************************************/
|
||
104 | |||
105 | /******************************************************************************/
|
||
106 | /* CHECKS */
|
||
107 | /******************************************************************************/
|
||
108 | |||
109 | /******************************************************************************/
|
||
110 | /* DATA STRUCTURES AND TYPES */
|
||
111 | /******************************************************************************/
|
||
112 | |||
113 | /******************************************************************************/
|
||
114 | /* MACROS */
|
||
115 | /******************************************************************************/
|
||
116 | |||
117 | /******************************************************************************/
|
||
118 | /* EXTERN DECLARATIONS */
|
||
119 | /******************************************************************************/
|
||
120 | |||
121 | 7de0cc90 | Thomas Schöpping | #if defined(__cplusplus)
|
122 | e545e620 | Thomas Schöpping | extern "C" { |
123 | 7de0cc90 | Thomas Schöpping | #endif /* defined(_cplusplus) */ |
124 | d180e1ba | Thomas Schöpping | void aosThdSleepUntilS(const aos_timestamp_t t); |
125 | 5c9e9b9d | Thomas Schöpping | #if (AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE) |
126 | aed3754b | Thomas Schöpping | size_t aosThdGetStackPeakUtilization(thread_t* thread); |
127 | 7de0cc90 | Thomas Schöpping | #endif /* (AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE) */ |
128 | #if defined(__cplusplus)
|
||
129 | e545e620 | Thomas Schöpping | } |
130 | 7de0cc90 | Thomas Schöpping | #endif /* defined(_cplusplus) */ |
131 | e545e620 | Thomas Schöpping | |
132 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
133 | /* INLINE FUNCTIONS */
|
||
134 | /******************************************************************************/
|
||
135 | |||
136 | e545e620 | Thomas Schöpping | /**
|
137 | * @brief Lets the calling thread sleep the specified amount of microseconds.
|
||
138 | *
|
||
139 | * @param[in] us Time to sleep in microseconds.
|
||
140 | */
|
||
141 | static inline void aosThdUSleepS(const aos_interval_t us) |
||
142 | { |
||
143 | aos_timestamp_t ut; |
||
144 | |||
145 | aosSysGetUptimeX(&ut); |
||
146 | ut += us; |
||
147 | d180e1ba | Thomas Schöpping | aosThdSleepUntilS(ut); |
148 | e545e620 | Thomas Schöpping | |
149 | return;
|
||
150 | } |
||
151 | |||
152 | /**
|
||
153 | * @brief Lets the calling thread sleep the specified amount of milliseconds.
|
||
154 | *
|
||
155 | * @param[in] ms Time to sleep in milliseconds.
|
||
156 | */
|
||
157 | static inline void aosThdMSleepS(const uint32_t ms) |
||
158 | { |
||
159 | aos_timestamp_t ut; |
||
160 | |||
161 | aosSysGetUptimeX(&ut); |
||
162 | ut += (aos_timestamp_t)ms * MICROSECONDS_PER_MILLISECOND; |
||
163 | d180e1ba | Thomas Schöpping | aosThdSleepUntilS(ut); |
164 | e545e620 | Thomas Schöpping | |
165 | return;
|
||
166 | } |
||
167 | |||
168 | /**
|
||
169 | * @brief Lets the calling thread sleep the specified amount of seconds.
|
||
170 | *
|
||
171 | * @param[in] s Time to sleep in seconds.
|
||
172 | */
|
||
173 | static inline void aosThdSSleepS(const uint32_t s) |
||
174 | { |
||
175 | aos_timestamp_t ut; |
||
176 | |||
177 | aosSysGetUptimeX(&ut); |
||
178 | ut += (aos_timestamp_t)s * MICROSECONDS_PER_SECOND; |
||
179 | d180e1ba | Thomas Schöpping | aosThdSleepUntilS(ut); |
180 | e545e620 | Thomas Schöpping | |
181 | return;
|
||
182 | } |
||
183 | |||
184 | /**
|
||
185 | * @brief Lets the calling thread sleep the specified amount of seconds.
|
||
186 | *
|
||
187 | * @param[in] s Time to sleep in seconds.
|
||
188 | */
|
||
189 | static inline void aosThdSleepS(const float s) |
||
190 | { |
||
191 | aos_timestamp_t ut; |
||
192 | |||
193 | aosSysGetUptimeX(&ut); |
||
194 | ut += (aos_timestamp_t)(s * MICROSECONDS_PER_SECOND); |
||
195 | d180e1ba | Thomas Schöpping | aosThdSleepUntilS(ut); |
196 | e545e620 | Thomas Schöpping | |
197 | return;
|
||
198 | } |
||
199 | |||
200 | /**
|
||
201 | * @brief Lets the calling thread sleep the specified amount of microseconds.
|
||
202 | *
|
||
203 | * @param[in] us Time to sleep in microseconds.
|
||
204 | */
|
||
205 | static inline void aosThdUSleep(const uint32_t us) |
||
206 | { |
||
207 | chSysLock(); |
||
208 | aosThdUSleepS(us); |
||
209 | chSysUnlock(); |
||
210 | |||
211 | return;
|
||
212 | } |
||
213 | |||
214 | /**
|
||
215 | * @brief Lets the calling thread sleep the specified amount of milliseconds.
|
||
216 | *
|
||
217 | * @param[in] ms Time to sleep in milliseconds.
|
||
218 | */
|
||
219 | static inline void aosThdMSleep(const uint32_t ms) |
||
220 | { |
||
221 | chSysLock(); |
||
222 | aosThdMSleepS(ms); |
||
223 | chSysUnlock(); |
||
224 | |||
225 | return;
|
||
226 | } |
||
227 | |||
228 | /**
|
||
229 | * @brief Lets the calling thread sleep the specified amount of seconds.
|
||
230 | *
|
||
231 | * @param[in] s Time to sleep in seconds.
|
||
232 | */
|
||
233 | static inline void aosThdSSleep(const uint32_t s) |
||
234 | { |
||
235 | chSysLock(); |
||
236 | aosThdSSleepS(s); |
||
237 | chSysUnlock(); |
||
238 | |||
239 | return;
|
||
240 | } |
||
241 | |||
242 | /**
|
||
243 | * @brief Lets the calling thread sleep the specified amount of seconds.
|
||
244 | *
|
||
245 | * @param[in] s Time to sleep in seconds.
|
||
246 | */
|
||
247 | static inline void aosThdSleep(const float s) |
||
248 | { |
||
249 | chSysLock(); |
||
250 | aosThdSleepS(s); |
||
251 | chSysUnlock(); |
||
252 | |||
253 | return;
|
||
254 | } |
||
255 | |||
256 | /**
|
||
257 | * @brief Lets the calling thread sleep until the specifide system uptime.
|
||
258 | *
|
||
259 | * @param[in] t Deadline until the thread will sleep.
|
||
260 | */
|
||
261 | d180e1ba | Thomas Schöpping | static inline void aosThdSleepUntil(const aos_timestamp_t t) |
262 | e545e620 | Thomas Schöpping | { |
263 | chSysLock(); |
||
264 | aosThdSleepUntilS(t); |
||
265 | chSysUnlock(); |
||
266 | |||
267 | return;
|
||
268 | } |
||
269 | |||
270 | 5c9e9b9d | Thomas Schöpping | #if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__) |
271 | |||
272 | aed3754b | Thomas Schöpping | /**
|
273 | * @brief Retrieve the stack size of a specific thread in bytes.
|
||
274 | *
|
||
275 | * @param[in] thread Thread to retrieve the stack size from.
|
||
276 | *
|
||
277 | * @return Absolute stack size in bytes.
|
||
278 | */
|
||
279 | d871cc15 | Thomas Schöpping | inline size_t aosThdGetStacksize(thread_t* thread)
|
280 | aed3754b | Thomas Schöpping | { |
281 | aosDbgCheck(thread != NULL);
|
||
282 | |||
283 | /*
|
||
284 | * Working area is structured like:
|
||
285 | * thread.wabase (LSB)->[ stack | port specific data | thread structure ]
|
||
286 | * See the following macros for details:
|
||
287 | * - THD_WORKING_AREA
|
||
288 | * - THD_WORKING_AREA_SIZE
|
||
289 | * - PORT_WA_SIZE
|
||
290 | */
|
||
291 | return ((uintptr_t)(thread) - (uintptr_t)(thread->wabase)) -
|
||
292 | ((size_t)PORT_GUARD_PAGE_SIZE + sizeof(struct port_intctx) + sizeof(struct port_extctx) + (size_t)PORT_INT_REQUIRED_STACK); |
||
293 | } |
||
294 | |||
295 | 7de0cc90 | Thomas Schöpping | #endif /* (AMIROOS_CFG_DBG == true) */ |
296 | 5c9e9b9d | Thomas Schöpping | |
297 | 6ff06bbf | Thomas Schöpping | #endif /* AMIROOS_THREAD_H */ |
298 | 53710ca3 | Marc Rothmann | |
299 | /** @} */ |