Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_thread.h @ 33de2690

History | View | Annotate | Download (8.345 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 ((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
#if defined(__cplusplus)
133
}
134
#endif /* defined(_cplusplus) */
135

    
136
/******************************************************************************/
137
/* INLINE FUNCTIONS                                                           */
138
/******************************************************************************/
139

    
140
/**
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
  aosThdSleepUntilS(ut);
152

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

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

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

    
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
static inline void aosThdSleepUntil(const aos_timestamp_t t)
266
{
267
  chSysLock();
268
  aosThdSleepUntilS(t);
269
  chSysUnlock();
270

    
271
  return;
272
}
273

    
274
#if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__)
275

    
276
/**
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
inline size_t aosThdGetStacksize(thread_t* thread)
284
{
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
#endif /* (AMIROOS_CFG_DBG == true) */
300

    
301
#endif /* AMIROOS_THREAD_H */
302

    
303
/** @} */