Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_thread.h @ 96621a83

History | View | Annotate | Download (8.083 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  Thomas Schöpping et al.
4

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
/**
20
 * @file    aos_thread.h
21
 * @brief   Thread macros and inline functions.
22
 *
23
 * @addtogroup aos_threads
24
 * @{
25
 */
26

    
27
#ifndef AMIROOS_THREAD_H
28
#define AMIROOS_THREAD_H
29

    
30
#include <amiroos.h>
31

    
32
/******************************************************************************/
33
/* CONSTANTS                                                                  */
34
/******************************************************************************/
35

    
36
/**
37
 * @brief   Minimum thread priority.
38
 */
39
#define AOS_THD_LOWPRIO_MIN           ((tprio_t)(LOWPRIO))
40

    
41
/**
42
 * @brief   Maximum priority for background threads.
43
 */
44
#define AOS_THD_LOWPRIO_MAX           ((tprio_t)(LOWPRIO + ((NORMALPRIO - LOWPRIO) / 2)))
45

    
46
/**
47
 * @brief   Minimum priority for normal/standard threads.
48
 */
49
#define AOS_THD_NORMALPRIO_MIN        ((tprio_t)(AOS_THD_LOWPRIO_MAX + 1))
50

    
51
/**
52
 * @brief   Maximum priority for normal/standard threads.
53
 */
54
#define AOS_THD_NORMALPRIO_MAX        ((tprio_t)(NORMALPRIO))
55

    
56
/**
57
 * @brief   Minimum priority for important threads.
58
 */
59
#define AOS_THD_HIGHPRIO_MIN          ((tprio_t)(NORMALPRIO + 1))
60

    
61
/**
62
 * @brief   Maximum priority for important threads.
63
 */
64
#define AOS_THD_HIGHPRIO_MAX          ((tprio_t)(NORMALPRIO + ((HIGHPRIO - NORMALPRIO) / 2)))
65

    
66
/**
67
 * @brief   Minimum priority for real-time threads.
68
 */
69
#define AOS_THD_RTPRIO_MIN            ((tprio_t)(AOS_THD_HIGHPRIO_MAX + 1))
70

    
71
/**
72
 * @brief   Maximum priority for real-time threads.
73
 */
74
#define AOS_THD_RTPRIO_MAX            ((tprio_t)(HIGHPRIO - 1))
75

    
76
/**
77
 * @brief   Priority for the system control thread.
78
 */
79
#define AOS_THD_CTRLPRIO              ((tprio_t)(HIGHPRIO))
80

    
81
/**
82
 * @brief   Maximum timeframe that can be slept in system ticks.
83
 */
84
#define AOS_THD_MAX_SLEEP_ST          TIME_MAX_INTERVAL
85

    
86
/**
87
 * @brief   Maximum timeframe that can be slept in seconds.
88
 */
89
#define AOS_THD_MAX_SLEEP_S           (chTimeI2S(AOS_THD_MAX_SLEEP_ST) - 1)
90

    
91
/**
92
 * @brief   Maximum timeframe that can be slept in milliseconds.
93
 */
94
#define AOS_THD_MAX_SLEEP_MS          (chTimeI2MS(AOS_THD_MAX_SLEEP_ST) - 1)
95

    
96
/**
97
 * @brief   Maximum timeframe that can be slept in microseconds.
98
 */
99
#define AOS_THD_MAX_SLEEP_US          (chTimeI2US(AOS_THD_MAX_SLEEP_ST) - 1)
100

    
101
/******************************************************************************/
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
#if defined(__cplusplus)
122
extern "C" {
123
#endif /* defined(_cplusplus) */
124
  void aosThdSleepUntilS(const aos_timestamp_t t);
125
#if ((AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE)) || defined(__DOXYGEN__)
126
  size_t aosThdGetStackPeakUtilization(thread_t* thread);
127
#endif /* (AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE) */
128
#if defined(__cplusplus)
129
}
130
#endif /* defined(_cplusplus) */
131

    
132
/******************************************************************************/
133
/* INLINE FUNCTIONS                                                           */
134
/******************************************************************************/
135

    
136
/**
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
  aosThdSleepUntilS(ut);
148

    
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
  aosThdSleepUntilS(ut);
164

    
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
  aosThdSleepUntilS(ut);
180

    
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
  aosThdSleepUntilS(ut);
196

    
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
static inline void aosThdSleepUntil(const aos_timestamp_t t)
262
{
263
  chSysLock();
264
  aosThdSleepUntilS(t);
265
  chSysUnlock();
266

    
267
  return;
268
}
269

    
270
#if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__)
271

    
272
/**
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
inline size_t aosThdGetStacksize(thread_t* thread)
280
{
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
#endif /* (AMIROOS_CFG_DBG == true) */
296

    
297
#endif /* AMIROOS_THREAD_H */
298

    
299
/** @} */