Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_thread.h @ 1678f270

History | View | Annotate | Download (6.3 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  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 <aos_time.h>
31
#include <aos_system.h>
32

    
33
/**
34
 * @brief   Minimum thread priority.
35
 */
36
#define AOS_THD_LOWPRIO_MIN           ((tprio_t)(LOWPRIO))
37

    
38
/**
39
 * @brief   Maximum priority for background threads.
40
 */
41
#define AOS_THD_LOWPRIO_MAX           ((tprio_t)(LOWPRIO + ((NORMALPRIO - LOWPRIO) / 2)))
42

    
43
/**
44
 * @brief   Minimum priority for normal/standard threads.
45
 */
46
#define AOS_THD_NORMALPRIO_MIN        ((tprio_t)(AOS_THD_LOWPRIO_MAX + 1))
47

    
48
/**
49
 * @brief   Maximum priority for normal/standard threads.
50
 */
51
#define AOS_THD_NORMALPRIO_MAX        ((tprio_t)(NORMALPRIO))
52

    
53
/**
54
 * @brief   Minimum priority for important threads.
55
 */
56
#define AOS_THD_HIGHPRIO_MIN          ((tprio_t)(NORMALPRIO + 1))
57

    
58
/**
59
 * @brief   Maximum priority for important threads.
60
 */
61
#define AOS_THD_HIGHPRIO_MAX          ((tprio_t)(NORMALPRIO + ((HIGHPRIO - NORMALPRIO) / 2)))
62

    
63
/**
64
 * @brief   Minimum priority for real-time threads.
65
 */
66
#define AOS_THD_RTPRIO_MIN            ((tprio_t)(AOS_THD_HIGHPRIO_MAX + 1))
67

    
68
/**
69
 * @brief   Maximum priority for real-time threads.
70
 */
71
#define AOS_THD_RTPRIO_MAX            ((tprio_t)(HIGHPRIO - 1))
72

    
73
/**
74
 * @brief   Priority for the system control thread.
75
 */
76
#define AOS_THD_CTRLPRIO              ((tprio_t)(HIGHPRIO))
77

    
78
/**
79
 * @brief   Maximum timeframe that can be slept in system ticks.
80
 */
81
#define AOS_THD_MAX_SLEEP_ST          TIME_MAX_INTERVAL
82

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

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

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

    
98
#ifdef __cplusplus
99
extern "C" {
100
#endif
101
  void aosThdSleepUntilS(const aos_timestamp_t* t);
102
#if (AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE)
103
  size_t aosThdGetStackPeakUtilization(thread_t* thread);
104
#endif
105
#ifdef __cplusplus
106
}
107
#endif
108

    
109
/**
110
 * @brief   Lets the calling thread sleep the specified amount of microseconds.
111
 *
112
 * @param[in] us    Time to sleep in microseconds.
113
 */
114
static inline void aosThdUSleepS(const aos_interval_t us)
115
{
116
  aos_timestamp_t ut;
117

    
118
  aosSysGetUptimeX(&ut);
119
  ut += us;
120
  aosThdSleepUntilS(&ut);
121

    
122
  return;
123
}
124

    
125
/**
126
 * @brief   Lets the calling thread sleep the specified amount of milliseconds.
127
 *
128
 * @param[in] ms    Time to sleep in milliseconds.
129
 */
130
static inline void aosThdMSleepS(const uint32_t ms)
131
{
132
  aos_timestamp_t ut;
133

    
134
  aosSysGetUptimeX(&ut);
135
  ut += (aos_timestamp_t)ms * MICROSECONDS_PER_MILLISECOND;
136
  aosThdSleepUntilS(&ut);
137

    
138
  return;
139
}
140

    
141
/**
142
 * @brief   Lets the calling thread sleep the specified amount of seconds.
143
 *
144
 * @param[in] s     Time to sleep in seconds.
145
 */
146
static inline void aosThdSSleepS(const uint32_t s)
147
{
148
  aos_timestamp_t ut;
149

    
150
  aosSysGetUptimeX(&ut);
151
  ut += (aos_timestamp_t)s * MICROSECONDS_PER_SECOND;
152
  aosThdSleepUntilS(&ut);
153

    
154
  return;
155
}
156

    
157
/**
158
 * @brief   Lets the calling thread sleep the specified amount of seconds.
159
 *
160
 * @param[in] s     Time to sleep in seconds.
161
 */
162
static inline void aosThdSleepS(const float s)
163
{
164
  aos_timestamp_t ut;
165

    
166
  aosSysGetUptimeX(&ut);
167
  ut += (aos_timestamp_t)(s * MICROSECONDS_PER_SECOND);
168
  aosThdSleepUntilS(&ut);
169

    
170
  return;
171
}
172

    
173
/**
174
 * @brief   Lets the calling thread sleep the specified amount of microseconds.
175
 *
176
 * @param[in] us    Time to sleep in microseconds.
177
 */
178
static inline void aosThdUSleep(const uint32_t us)
179
{
180
  chSysLock();
181
  aosThdUSleepS(us);
182
  chSysUnlock();
183

    
184
  return;
185
}
186

    
187
/**
188
 * @brief   Lets the calling thread sleep the specified amount of milliseconds.
189
 *
190
 * @param[in] ms    Time to sleep in milliseconds.
191
 */
192
static inline void aosThdMSleep(const uint32_t ms)
193
{
194
  chSysLock();
195
  aosThdMSleepS(ms);
196
  chSysUnlock();
197

    
198
  return;
199
}
200

    
201
/**
202
 * @brief   Lets the calling thread sleep the specified amount of seconds.
203
 *
204
 * @param[in] s     Time to sleep in seconds.
205
 */
206
static inline void aosThdSSleep(const uint32_t s)
207
{
208
  chSysLock();
209
  aosThdSSleepS(s);
210
  chSysUnlock();
211

    
212
  return;
213
}
214

    
215
/**
216
 * @brief   Lets the calling thread sleep the specified amount of seconds.
217
 *
218
 * @param[in] s     Time to sleep in seconds.
219
 */
220
static inline void aosThdSleep(const float s)
221
{
222
  chSysLock();
223
  aosThdSleepS(s);
224
  chSysUnlock();
225

    
226
  return;
227
}
228

    
229
/**
230
 * @brief   Lets the calling thread sleep until the specifide system uptime.
231
 *
232
 * @param[in] t     Deadline until the thread will sleep.
233
 */
234
static inline void aosThdSleepUntil(const aos_timestamp_t* t)
235
{
236
  chSysLock();
237
  aosThdSleepUntilS(t);
238
  chSysUnlock();
239

    
240
  return;
241
}
242

    
243
#if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__)
244

    
245
/**
246
 * @brief   Retrieve the stack size of a specific thread in bytes.
247
 *
248
 * @param[in] thread    Thread to retrieve the stack size from.
249
 *
250
 * @return  Absolute stack size in bytes.
251
 */
252
static inline size_t aosThdGetStacksize(thread_t* thread)
253
{
254
  aosDbgCheck(thread != NULL);
255

    
256
  /*
257
   * Working area is structured like:
258
   *    thread.wabase (LSB)->[ stack | port specific data | thread structure ]
259
   * See the following macros for details:
260
   *  - THD_WORKING_AREA
261
   *  - THD_WORKING_AREA_SIZE
262
   *  - PORT_WA_SIZE
263
   */
264
  return ((uintptr_t)(thread) - (uintptr_t)(thread->wabase)) -
265
         ((size_t)PORT_GUARD_PAGE_SIZE + sizeof(struct port_intctx) + sizeof(struct port_extctx) + (size_t)PORT_INT_REQUIRED_STACK);
266
}
267

    
268
#endif /* AMIROOS_CFG_DBG == true */
269

    
270
#endif /* _AMIROOS_THREAD_H_ */
271

    
272
/** @} */