Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_thread.h @ 85e62aaf

History | View | Annotate | Download (6.186 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  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 53710ca3 Marc Rothmann
/**
20
 * @file    aos_thread.h
21
 * @brief   Thread macros and inline functions.
22
 *
23
 * @addtogroup aos_threads
24
 * @{
25
 */
26
27 e545e620 Thomas Schöpping
#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 1e5f7648 Thomas Schöpping
#define AOS_THD_LOWPRIO_MIN           ((tprio_t)(LOWPRIO))
37 e545e620 Thomas Schöpping
38
/**
39
 * @brief   Maximum priority for background threads.
40
 */
41 1e5f7648 Thomas Schöpping
#define AOS_THD_LOWPRIO_MAX           ((tprio_t)(LOWPRIO + ((NORMALPRIO - LOWPRIO) / 2)))
42 e545e620 Thomas Schöpping
43
/**
44
 * @brief   Minimum priority for normal/standard threads.
45
 */
46 1e5f7648 Thomas Schöpping
#define AOS_THD_NORMALPRIO_MIN        ((tprio_t)(AOS_THD_LOWPRIO_MAX + 1))
47 e545e620 Thomas Schöpping
48
/**
49
 * @brief   Maximum priority for normal/standard threads.
50
 */
51 1e5f7648 Thomas Schöpping
#define AOS_THD_NORMALPRIO_MAX        ((tprio_t)(NORMALPRIO))
52 e545e620 Thomas Schöpping
53
/**
54
 * @brief   Minimum priority for important threads.
55
 */
56 1e5f7648 Thomas Schöpping
#define AOS_THD_HIGHPRIO_MIN          ((tprio_t)(NORMALPRIO + 1))
57 e545e620 Thomas Schöpping
58
/**
59
 * @brief   Maximum priority for important threads.
60
 */
61 1e5f7648 Thomas Schöpping
#define AOS_THD_HIGHPRIO_MAX          ((tprio_t)(NORMALPRIO + ((HIGHPRIO - NORMALPRIO) / 2)))
62 e545e620 Thomas Schöpping
63
/**
64
 * @brief   Minimum priority for real-time threads.
65
 */
66 1e5f7648 Thomas Schöpping
#define AOS_THD_RTPRIO_MIN            ((tprio_t)(AOS_THD_HIGHPRIO_MAX + 1))
67 e545e620 Thomas Schöpping
68
/**
69
 * @brief   Maximum priority for real-time threads.
70
 */
71 1e5f7648 Thomas Schöpping
#define AOS_THD_RTPRIO_MAX            ((tprio_t)(HIGHPRIO - 1))
72 e545e620 Thomas Schöpping
73
/**
74
 * @brief   Priority for the system control thread.
75
 */
76 1e5f7648 Thomas Schöpping
#define AOS_THD_CTRLPRIO              ((tprio_t)(HIGHPRIO))
77 e545e620 Thomas Schöpping
78
/**
79
 * @brief   Maximum timeframe that can be slept in system ticks.
80
 */
81 1e5f7648 Thomas Schöpping
#define AOS_THD_MAX_SLEEP_ST          TIME_MAX_INTERVAL
82 e545e620 Thomas Schöpping
83
/**
84
 * @brief   Maximum timeframe that can be slept in seconds.
85
 */
86 1e5f7648 Thomas Schöpping
#define AOS_THD_MAX_SLEEP_S           (chTimeI2S(AOS_THD_MAX_SLEEP_ST) - 1)
87 e545e620 Thomas Schöpping
88
/**
89
 * @brief   Maximum timeframe that can be slept in milliseconds.
90
 */
91 1e5f7648 Thomas Schöpping
#define AOS_THD_MAX_SLEEP_MS          (chTimeI2MS(AOS_THD_MAX_SLEEP_ST) - 1)
92 e545e620 Thomas Schöpping
93
/**
94
 * @brief   Maximum timeframe that can be slept in microseconds.
95
 */
96 1e5f7648 Thomas Schöpping
#define AOS_THD_MAX_SLEEP_US          (chTimeI2US(AOS_THD_MAX_SLEEP_ST) - 1)
97 e545e620 Thomas Schöpping
98
#ifdef __cplusplus
99
extern "C" {
100
#endif
101
  void aosThdSleepUntilS(const aos_timestamp_t* t);
102 aed3754b Thomas Schöpping
#if (CH_DBG_FILL_THREADS == TRUE)
103
  size_t aosThdGetStackPeakUtilization(thread_t* thread);
104
#endif
105 e545e620 Thomas Schöpping
#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 aed3754b Thomas Schöpping
/**
244
 * @brief   Retrieve the stack size of a specific thread in bytes.
245
 *
246
 * @param[in] thread    Thread to retrieve the stack size from.
247
 *
248
 * @return  Absolute stack size in bytes.
249
 */
250
static inline size_t aosThdGetStacksize(thread_t* thread)
251
{
252
  aosDbgCheck(thread != NULL);
253
254
  /*
255
   * Working area is structured like:
256
   *    thread.wabase (LSB)->[ stack | port specific data | thread structure ]
257
   * See the following macros for details:
258
   *  - THD_WORKING_AREA
259
   *  - THD_WORKING_AREA_SIZE
260
   *  - PORT_WA_SIZE
261
   */
262
  return ((uintptr_t)(thread) - (uintptr_t)(thread->wabase)) -
263
         ((size_t)PORT_GUARD_PAGE_SIZE + sizeof(struct port_intctx) + sizeof(struct port_extctx) + (size_t)PORT_INT_REQUIRED_STACK);
264
}
265
266 e545e620 Thomas Schöpping
#endif /* _AMIROOS_THREAD_H_ */
267 53710ca3 Marc Rothmann
268
/** @} */