Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_thread.h @ e7c4f797

History | View | Annotate | Download (8.345 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 96621a83 Thomas Schöpping
Copyright (C) 2016..2020  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 cda14729 Thomas Schöpping
#if ((AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE)) || defined(__DOXYGEN__)
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 e7c4f797 Thomas Schöpping
#if ((CH_CFG_USE_REGISTRY == TRUE) || (CH_CFG_USE_THREADHIERARCHY == TRUE)) || defined(__DOXYGEN__)
129
  thread_t* aosThreadGetFirst(void);
130
  thread_t* aosThreadGetNext(thread_t* thread);
131
#endif /* (CH_CFG_USE_REGISTRY == TRUE) || (CH_CFG_USE_THREADHIERARCHY == TRUE) */
132 7de0cc90 Thomas Schöpping
#if defined(__cplusplus)
133 e545e620 Thomas Schöpping
}
134 7de0cc90 Thomas Schöpping
#endif /* defined(_cplusplus) */
135 e545e620 Thomas Schöpping
136 f3ac1c96 Thomas Schöpping
/******************************************************************************/
137
/* INLINE FUNCTIONS                                                           */
138
/******************************************************************************/
139
140 e545e620 Thomas Schöpping
/**
141
 * @brief   Lets the calling thread sleep the specified amount of microseconds.
142
 *
143
 * @param[in] us    Time to sleep in microseconds.
144
 */
145
static inline void aosThdUSleepS(const aos_interval_t us)
146
{
147
  aos_timestamp_t ut;
148
149
  aosSysGetUptimeX(&ut);
150
  ut += us;
151 d180e1ba Thomas Schöpping
  aosThdSleepUntilS(ut);
152 e545e620 Thomas Schöpping
153
  return;
154
}
155
156
/**
157
 * @brief   Lets the calling thread sleep the specified amount of milliseconds.
158
 *
159
 * @param[in] ms    Time to sleep in milliseconds.
160
 */
161
static inline void aosThdMSleepS(const uint32_t ms)
162
{
163
  aos_timestamp_t ut;
164
165
  aosSysGetUptimeX(&ut);
166
  ut += (aos_timestamp_t)ms * MICROSECONDS_PER_MILLISECOND;
167 d180e1ba Thomas Schöpping
  aosThdSleepUntilS(ut);
168 e545e620 Thomas Schöpping
169
  return;
170
}
171
172
/**
173
 * @brief   Lets the calling thread sleep the specified amount of seconds.
174
 *
175
 * @param[in] s     Time to sleep in seconds.
176
 */
177
static inline void aosThdSSleepS(const uint32_t s)
178
{
179
  aos_timestamp_t ut;
180
181
  aosSysGetUptimeX(&ut);
182
  ut += (aos_timestamp_t)s * MICROSECONDS_PER_SECOND;
183 d180e1ba Thomas Schöpping
  aosThdSleepUntilS(ut);
184 e545e620 Thomas Schöpping
185
  return;
186
}
187
188
/**
189
 * @brief   Lets the calling thread sleep the specified amount of seconds.
190
 *
191
 * @param[in] s     Time to sleep in seconds.
192
 */
193
static inline void aosThdSleepS(const float s)
194
{
195
  aos_timestamp_t ut;
196
197
  aosSysGetUptimeX(&ut);
198
  ut += (aos_timestamp_t)(s * MICROSECONDS_PER_SECOND);
199 d180e1ba Thomas Schöpping
  aosThdSleepUntilS(ut);
200 e545e620 Thomas Schöpping
201
  return;
202
}
203
204
/**
205
 * @brief   Lets the calling thread sleep the specified amount of microseconds.
206
 *
207
 * @param[in] us    Time to sleep in microseconds.
208
 */
209
static inline void aosThdUSleep(const uint32_t us)
210
{
211
  chSysLock();
212
  aosThdUSleepS(us);
213
  chSysUnlock();
214
215
  return;
216
}
217
218
/**
219
 * @brief   Lets the calling thread sleep the specified amount of milliseconds.
220
 *
221
 * @param[in] ms    Time to sleep in milliseconds.
222
 */
223
static inline void aosThdMSleep(const uint32_t ms)
224
{
225
  chSysLock();
226
  aosThdMSleepS(ms);
227
  chSysUnlock();
228
229
  return;
230
}
231
232
/**
233
 * @brief   Lets the calling thread sleep the specified amount of seconds.
234
 *
235
 * @param[in] s     Time to sleep in seconds.
236
 */
237
static inline void aosThdSSleep(const uint32_t s)
238
{
239
  chSysLock();
240
  aosThdSSleepS(s);
241
  chSysUnlock();
242
243
  return;
244
}
245
246
/**
247
 * @brief   Lets the calling thread sleep the specified amount of seconds.
248
 *
249
 * @param[in] s     Time to sleep in seconds.
250
 */
251
static inline void aosThdSleep(const float s)
252
{
253
  chSysLock();
254
  aosThdSleepS(s);
255
  chSysUnlock();
256
257
  return;
258
}
259
260
/**
261
 * @brief   Lets the calling thread sleep until the specifide system uptime.
262
 *
263
 * @param[in] t     Deadline until the thread will sleep.
264
 */
265 d180e1ba Thomas Schöpping
static inline void aosThdSleepUntil(const aos_timestamp_t t)
266 e545e620 Thomas Schöpping
{
267
  chSysLock();
268
  aosThdSleepUntilS(t);
269
  chSysUnlock();
270
271
  return;
272
}
273
274 5c9e9b9d Thomas Schöpping
#if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__)
275
276 aed3754b Thomas Schöpping
/**
277
 * @brief   Retrieve the stack size of a specific thread in bytes.
278
 *
279
 * @param[in] thread    Thread to retrieve the stack size from.
280
 *
281
 * @return  Absolute stack size in bytes.
282
 */
283 d871cc15 Thomas Schöpping
inline size_t aosThdGetStacksize(thread_t* thread)
284 aed3754b Thomas Schöpping
{
285
  aosDbgCheck(thread != NULL);
286
287
  /*
288
   * Working area is structured like:
289
   *    thread.wabase (LSB)->[ stack | port specific data | thread structure ]
290
   * See the following macros for details:
291
   *  - THD_WORKING_AREA
292
   *  - THD_WORKING_AREA_SIZE
293
   *  - PORT_WA_SIZE
294
   */
295
  return ((uintptr_t)(thread) - (uintptr_t)(thread->wabase)) -
296
         ((size_t)PORT_GUARD_PAGE_SIZE + sizeof(struct port_intctx) + sizeof(struct port_extctx) + (size_t)PORT_INT_REQUIRED_STACK);
297
}
298
299 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_DBG == true) */
300 5c9e9b9d Thomas Schöpping
301 6ff06bbf Thomas Schöpping
#endif /* AMIROOS_THREAD_H */
302 53710ca3 Marc Rothmann
303
/** @} */