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.com/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 Zero Timespec |
---|
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 | * @return 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 | * @return The nanoseconds portion of @a _time. |
---|
87 | */ |
---|
88 | #define _Timespec_Get_nanoseconds( _time ) \ |
---|
89 | ((_time)->tv_nsec) |
---|
90 | |
---|
91 | /** |
---|
92 | * @brief Is Timespec Valid |
---|
93 | * |
---|
94 | * This method determines the validity of a timespec. |
---|
95 | * |
---|
96 | * @param[in] time is the timespec instance to validate. |
---|
97 | * |
---|
98 | * @return This method returns true if @a time is valid and |
---|
99 | * false otherwise. |
---|
100 | */ |
---|
101 | bool _Timespec_Is_valid( |
---|
102 | const struct timespec *time |
---|
103 | ); |
---|
104 | |
---|
105 | /** |
---|
106 | * @brief Timespec Less Than Operator |
---|
107 | * |
---|
108 | * This method is the less than operator for timespecs. |
---|
109 | * |
---|
110 | * @param[in] lhs is the left hand side timespec |
---|
111 | * @param[in] rhs is the right hand side timespec |
---|
112 | * |
---|
113 | * @return This method returns true if @a lhs is less than the @a rhs and |
---|
114 | * false otherwise. |
---|
115 | */ |
---|
116 | bool _Timespec_Less_than( |
---|
117 | const struct timespec *lhs, |
---|
118 | const struct timespec *rhs |
---|
119 | ); |
---|
120 | |
---|
121 | /** |
---|
122 | * @brief Timespec Greater Than Operator |
---|
123 | * |
---|
124 | * This method is the greater than operator for timespecs. |
---|
125 | * |
---|
126 | * @param[in] lhs is the left hand side timespec |
---|
127 | * @param[in] rhs is the right hand side timespec |
---|
128 | * |
---|
129 | * @return This method returns true if @a lhs is greater than the @a rhs and |
---|
130 | * false otherwise. |
---|
131 | */ |
---|
132 | #define _Timespec_Greater_than( _lhs, _rhs ) \ |
---|
133 | _Timespec_Less_than( _rhs, _lhs ) |
---|
134 | |
---|
135 | /** |
---|
136 | * @brief Timespec equal to Operator |
---|
137 | * |
---|
138 | * This method is the is equal to than operator for timespecs. |
---|
139 | * |
---|
140 | * @param[in] lhs is the left hand side timespec |
---|
141 | * @param[in] rhs is the right hand side timespec |
---|
142 | * |
---|
143 | * @return This method returns true if @a lhs is equal to @a rhs and |
---|
144 | * false otherwise. |
---|
145 | */ |
---|
146 | #define _Timespec_Equal_to( lhs, rhs ) \ |
---|
147 | ( ((lhs)->tv_sec == (rhs)->tv_sec) && \ |
---|
148 | ((lhs)->tv_nsec == (rhs)->tv_nsec) \ |
---|
149 | ) |
---|
150 | |
---|
151 | /** |
---|
152 | * @brief Add to a Timespec |
---|
153 | * |
---|
154 | * This routine adds two timespecs. The second argument is added |
---|
155 | * to the first. |
---|
156 | * |
---|
157 | * @param[in] time is the base time to be added to |
---|
158 | * @param[in] add is the timespec to add to the first argument |
---|
159 | * |
---|
160 | * @return This method returns the number of seconds @a time increased by. |
---|
161 | */ |
---|
162 | uint32_t _Timespec_Add_to( |
---|
163 | struct timespec *time, |
---|
164 | const struct timespec *add |
---|
165 | ); |
---|
166 | |
---|
167 | /** |
---|
168 | * @brief Convert Timespec to Number of Ticks |
---|
169 | * |
---|
170 | * This routine convert the @a time timespec to the corresponding number |
---|
171 | * of clock ticks. |
---|
172 | * |
---|
173 | * @param[in] time is the time to be converted |
---|
174 | * |
---|
175 | * @return This method returns the number of ticks computed. |
---|
176 | */ |
---|
177 | uint32_t _Timespec_To_ticks( |
---|
178 | const struct timespec *time |
---|
179 | ); |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief Convert Ticks to Timespec |
---|
183 | * |
---|
184 | * This routine converts the @a ticks value to the corresponding |
---|
185 | * timespec format @a time. |
---|
186 | * |
---|
187 | * @param[in] time is the timespec format time result |
---|
188 | * @param[in] ticks is the number of ticks to convert |
---|
189 | */ |
---|
190 | void _Timespec_From_ticks( |
---|
191 | uint32_t ticks, |
---|
192 | struct timespec *time |
---|
193 | ); |
---|
194 | |
---|
195 | /** |
---|
196 | * @brief Subtract Two Timespec |
---|
197 | * |
---|
198 | * This routine subtracts two timespecs. @a result is set to |
---|
199 | * @a end - @a start. |
---|
200 | * |
---|
201 | * @param[in] start is the starting time |
---|
202 | * @param[in] end is the ending time |
---|
203 | * @param[in] result is the difference between starting and ending time. |
---|
204 | * |
---|
205 | * @return This method fills in @a result. |
---|
206 | */ |
---|
207 | void _Timespec_Subtract( |
---|
208 | const struct timespec *start, |
---|
209 | const struct timespec *end, |
---|
210 | struct timespec *result |
---|
211 | ); |
---|
212 | |
---|
213 | /** |
---|
214 | * @brief Divide Timespec By Integer |
---|
215 | * |
---|
216 | * This routine divides a timespec by an integer value. The expected |
---|
217 | * use is to assist in benchmark calculations where you typically |
---|
218 | * divide a duration by a number of iterations. |
---|
219 | * |
---|
220 | * @param[in] time is the total |
---|
221 | * @param[in] iterations is the number of iterations |
---|
222 | * @param[in] result is the average time. |
---|
223 | * |
---|
224 | * @return This method fills in @a result. |
---|
225 | */ |
---|
226 | void _Timespec_Divide_by_integer( |
---|
227 | const struct timespec *time, |
---|
228 | uint32_t iterations, |
---|
229 | struct timespec *result |
---|
230 | ); |
---|
231 | |
---|
232 | /** |
---|
233 | * @brief Divide Timespec |
---|
234 | * |
---|
235 | * This routine divides a timespec by another timespec. The |
---|
236 | * intended use is for calculating percentages to three decimal points. |
---|
237 | * |
---|
238 | * @param[in] lhs is the left hand number |
---|
239 | * @param[in] rhs is the right hand number |
---|
240 | * @param[in] ival_percentage is the integer portion of the average |
---|
241 | * @param[in] fval_percentage is the thousandths of percentage |
---|
242 | * |
---|
243 | * @return This method fills in @a result. |
---|
244 | */ |
---|
245 | void _Timespec_Divide( |
---|
246 | const struct timespec *lhs, |
---|
247 | const struct timespec *rhs, |
---|
248 | uint32_t *ival_percentage, |
---|
249 | uint32_t *fval_percentage |
---|
250 | ); |
---|
251 | |
---|
252 | #ifdef __cplusplus |
---|
253 | } |
---|
254 | #endif |
---|
255 | |
---|
256 | /**@}*/ |
---|
257 | |
---|
258 | #endif |
---|
259 | /* end of include file */ |
---|