1 | /** |
---|
2 | * @file rtems/score/timespec.h |
---|
3 | * |
---|
4 | * This include file contains helpers for manipulating timespecs. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 1989-2009. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.org/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef _RTEMS_SCORE_TIMESPEC_H |
---|
17 | #define _RTEMS_SCORE_TIMESPEC_H |
---|
18 | |
---|
19 | /** |
---|
20 | * @defgroup Timespec Helpers |
---|
21 | * |
---|
22 | * @ingroup Score |
---|
23 | * |
---|
24 | * This handler encapsulates functionality related to manipulating |
---|
25 | * POSIX struct timespecs. |
---|
26 | */ |
---|
27 | /**@{*/ |
---|
28 | |
---|
29 | #include <stdbool.h> /* bool */ |
---|
30 | #include <stdint.h> /* uint32_t */ |
---|
31 | #include <time.h> /* struct timespec */ |
---|
32 | |
---|
33 | #ifdef __cplusplus |
---|
34 | extern "C" { |
---|
35 | #endif |
---|
36 | |
---|
37 | /** |
---|
38 | * @brief Set timespec to seconds nanosecond. |
---|
39 | * |
---|
40 | * This method sets the timespec to the specified seconds and nanoseconds |
---|
41 | * value. |
---|
42 | * |
---|
43 | * @param[in] _time points to the timespec instance to validate. |
---|
44 | * @param[in] _seconds is the seconds portion of the timespec |
---|
45 | * @param[in] _nanoseconds is the nanoseconds portion of the timespec |
---|
46 | */ |
---|
47 | #define _Timespec_Set( _time, _seconds, _nanoseconds ) \ |
---|
48 | do { \ |
---|
49 | (_time)->tv_sec = (_seconds); \ |
---|
50 | (_time)->tv_nsec = (_nanoseconds); \ |
---|
51 | } while (0) |
---|
52 | |
---|
53 | /** |
---|
54 | * @brief Sets the Timespec to Zero |
---|
55 | * |
---|
56 | * This method sets the timespec to zero. |
---|
57 | * value. |
---|
58 | * |
---|
59 | * @param[in] _time points to the timespec instance to zero. |
---|
60 | */ |
---|
61 | #define _Timespec_Set_to_zero( _time ) \ |
---|
62 | do { \ |
---|
63 | (_time)->tv_sec = 0; \ |
---|
64 | (_time)->tv_nsec = 0; \ |
---|
65 | } while (0) |
---|
66 | |
---|
67 | /** |
---|
68 | * @brief Get seconds portion of timespec. |
---|
69 | * |
---|
70 | * This method returns the seconds portion of the specified timespec |
---|
71 | * |
---|
72 | * @param[in] _time points to the timespec |
---|
73 | * |
---|
74 | * @retval The seconds portion of @a _time. |
---|
75 | */ |
---|
76 | #define _Timespec_Get_seconds( _time ) \ |
---|
77 | ((_time)->tv_sec) |
---|
78 | |
---|
79 | /** |
---|
80 | * @brief Get nanoseconds portion of timespec. |
---|
81 | * |
---|
82 | * This method returns the nanoseconds portion of the specified timespec |
---|
83 | * |
---|
84 | * @param[in] _time points to the timespec |
---|
85 | * |
---|
86 | * @retval The nanoseconds portion of @a _time. |
---|
87 | */ |
---|
88 | #define _Timespec_Get_nanoseconds( _time ) \ |
---|
89 | ((_time)->tv_nsec) |
---|
90 | |
---|
91 | /** |
---|
92 | * @brief Get the timestamp as nanoseconds. |
---|
93 | * |
---|
94 | * This method returns the timestamp as nanoseconds. |
---|
95 | * |
---|
96 | * @param[in] time points to the timestamp. |
---|
97 | * @param[in] nanoseconds the nanoseconds since the last tick. |
---|
98 | * |
---|
99 | * @retval The time in nanoseconds. |
---|
100 | */ |
---|
101 | uint64_t _Timespec_Get_As_nanoseconds( |
---|
102 | const struct timespec *time, |
---|
103 | const uint32_t nanoseconds |
---|
104 | ); |
---|
105 | |
---|
106 | /** |
---|
107 | * @brief Check if timespec is valid. |
---|
108 | * |
---|
109 | * This method determines the validity of a timespec. |
---|
110 | * |
---|
111 | * @param[in] time is the timespec instance to validate. |
---|
112 | * |
---|
113 | * @retval This method returns true if @a time is valid and |
---|
114 | * false otherwise. |
---|
115 | */ |
---|
116 | bool _Timespec_Is_valid( |
---|
117 | const struct timespec *time |
---|
118 | ); |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief The Timespec "less than" operator. |
---|
122 | * |
---|
123 | * This method is the less than operator for timespecs. |
---|
124 | * |
---|
125 | * @param[in] lhs is the left hand side timespec |
---|
126 | * @param[in] rhs is the right hand side timespec |
---|
127 | * |
---|
128 | * @retval This method returns true if @a lhs is less than the @a rhs and |
---|
129 | * false otherwise. |
---|
130 | */ |
---|
131 | bool _Timespec_Less_than( |
---|
132 | const struct timespec *lhs, |
---|
133 | const struct timespec *rhs |
---|
134 | ); |
---|
135 | |
---|
136 | /** |
---|
137 | * @brief The Timespec "greater than" operator. |
---|
138 | * |
---|
139 | * This method is the greater than operator for timespecs. |
---|
140 | * |
---|
141 | * @param[in] _lhs is the left hand side timespec |
---|
142 | * @param[in] _rhs is the right hand side timespec |
---|
143 | * |
---|
144 | * @retval This method returns true if @a lhs is greater than the @a rhs and |
---|
145 | * false otherwise. |
---|
146 | */ |
---|
147 | #define _Timespec_Greater_than( _lhs, _rhs ) \ |
---|
148 | _Timespec_Less_than( _rhs, _lhs ) |
---|
149 | |
---|
150 | /** |
---|
151 | * @brief The Timespec "equal to" operator. |
---|
152 | * |
---|
153 | * This method is the is equal to than operator for timespecs. |
---|
154 | * |
---|
155 | * @param[in] lhs is the left hand side timespec |
---|
156 | * @param[in] rhs is the right hand side timespec |
---|
157 | * |
---|
158 | * @retval This method returns true if @a lhs is equal to @a rhs and |
---|
159 | * false otherwise. |
---|
160 | */ |
---|
161 | #define _Timespec_Equal_to( lhs, rhs ) \ |
---|
162 | ( ((lhs)->tv_sec == (rhs)->tv_sec) && \ |
---|
163 | ((lhs)->tv_nsec == (rhs)->tv_nsec) \ |
---|
164 | ) |
---|
165 | |
---|
166 | /** |
---|
167 | * @brief Add two timespecs. |
---|
168 | * |
---|
169 | * This routine adds two timespecs. The second argument is added |
---|
170 | * to the first. |
---|
171 | * |
---|
172 | * @param[in] time is the base time to be added to |
---|
173 | * @param[in] add is the timespec to add to the first argument |
---|
174 | * |
---|
175 | * @retval This method returns the number of seconds @a time increased by. |
---|
176 | */ |
---|
177 | uint32_t _Timespec_Add_to( |
---|
178 | struct timespec *time, |
---|
179 | const struct timespec *add |
---|
180 | ); |
---|
181 | |
---|
182 | /** |
---|
183 | * @brief Convert timespec to number of ticks. |
---|
184 | * |
---|
185 | * This routine convert the @a time timespec to the corresponding number |
---|
186 | * of clock ticks. |
---|
187 | * |
---|
188 | * @param[in] time is the time to be converted |
---|
189 | * |
---|
190 | * @retval This method returns the number of ticks computed. |
---|
191 | */ |
---|
192 | uint32_t _Timespec_To_ticks( |
---|
193 | const struct timespec *time |
---|
194 | ); |
---|
195 | |
---|
196 | /** |
---|
197 | * @brief Convert ticks to timespec. |
---|
198 | * |
---|
199 | * This routine converts the @a ticks value to the corresponding |
---|
200 | * timespec format @a time. |
---|
201 | * |
---|
202 | * @param[in] time is the timespec format time result |
---|
203 | * @param[in] ticks is the number of ticks to convert |
---|
204 | */ |
---|
205 | void _Timespec_From_ticks( |
---|
206 | uint32_t ticks, |
---|
207 | struct timespec *time |
---|
208 | ); |
---|
209 | |
---|
210 | /** |
---|
211 | * @brief Subtract two timespec. |
---|
212 | * |
---|
213 | * This routine subtracts two timespecs. @a result is set to |
---|
214 | * @a end - @a start. |
---|
215 | * |
---|
216 | * @param[in] start is the starting time |
---|
217 | * @param[in] end is the ending time |
---|
218 | * @param[in] result is the difference between starting and ending time. |
---|
219 | * |
---|
220 | * @retval This method fills in @a result. |
---|
221 | */ |
---|
222 | void _Timespec_Subtract( |
---|
223 | const struct timespec *start, |
---|
224 | const struct timespec *end, |
---|
225 | struct timespec *result |
---|
226 | ); |
---|
227 | |
---|
228 | /** |
---|
229 | * @brief Divide timespec by an integer. |
---|
230 | * |
---|
231 | * This routine divides a timespec by an integer value. The expected |
---|
232 | * use is to assist in benchmark calculations where you typically |
---|
233 | * divide a duration by a number of iterations. |
---|
234 | * |
---|
235 | * @param[in] time is the total |
---|
236 | * @param[in] iterations is the number of iterations |
---|
237 | * @param[in] result is the average time. |
---|
238 | * |
---|
239 | * @retval This method fills in @a result. |
---|
240 | */ |
---|
241 | void _Timespec_Divide_by_integer( |
---|
242 | const struct timespec *time, |
---|
243 | uint32_t iterations, |
---|
244 | struct timespec *result |
---|
245 | ); |
---|
246 | |
---|
247 | /** |
---|
248 | * @brief Divide a timespec by anonther timespec. |
---|
249 | * |
---|
250 | * This routine divides a timespec by another timespec. The |
---|
251 | * intended use is for calculating percentages to three decimal points. |
---|
252 | * |
---|
253 | * @param[in] lhs is the left hand number |
---|
254 | * @param[in] rhs is the right hand number |
---|
255 | * @param[in] ival_percentage is the integer portion of the average |
---|
256 | * @param[in] fval_percentage is the thousandths of percentage |
---|
257 | * |
---|
258 | * @retval This method fills in @a result. |
---|
259 | */ |
---|
260 | void _Timespec_Divide( |
---|
261 | const struct timespec *lhs, |
---|
262 | const struct timespec *rhs, |
---|
263 | uint32_t *ival_percentage, |
---|
264 | uint32_t *fval_percentage |
---|
265 | ); |
---|
266 | |
---|
267 | #ifdef __cplusplus |
---|
268 | } |
---|
269 | #endif |
---|
270 | |
---|
271 | /**@}*/ |
---|
272 | |
---|
273 | #endif |
---|
274 | /* end of include file */ |
---|