1 | /** |
---|
2 | * @file rtems/score/timespec.h |
---|
3 | * |
---|
4 | * This include file contains helpers for manipulating timespecs. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 1989-2008. |
---|
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 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_SCORE_TIMESPEC_H |
---|
19 | #define _RTEMS_SCORE_TIMESPEC_H |
---|
20 | |
---|
21 | /** |
---|
22 | * @defgroup Timespec Helpers |
---|
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 | /** @brief Set Timespec to Seconds Nanosecond |
---|
38 | * |
---|
39 | * This method sets the timespec to the specified seconds and nanoseconds |
---|
40 | * value. |
---|
41 | * |
---|
42 | * @param[in] _time points to the timespec instance to validate. |
---|
43 | * @param[in] _seconds is the seconds portion of the timespec |
---|
44 | * @param[in] _nanoseconds is the nanoseconds portion of the timespec |
---|
45 | */ |
---|
46 | #define _Timespec_Set( _time, _seconds, _nanoseconds ) \ |
---|
47 | do { \ |
---|
48 | (_time)->tv_sec = (_seconds); \ |
---|
49 | (_time)->tv_nsec = (_nanoseconds); \ |
---|
50 | } while (0) |
---|
51 | |
---|
52 | /** @brief Zero Timespec |
---|
53 | * |
---|
54 | * This method sets the timespec to zero. |
---|
55 | * value. |
---|
56 | * |
---|
57 | * @param[in] _time points to the timespec instance to zero. |
---|
58 | */ |
---|
59 | #define _Timespec_Set_to_zero( _time ) \ |
---|
60 | do { \ |
---|
61 | (_time)->tv_sec = 0; \ |
---|
62 | (_time)->tv_nsec = 0; \ |
---|
63 | } while (0) |
---|
64 | |
---|
65 | /** @brief Get Seconds Portion of Timespec |
---|
66 | * |
---|
67 | * This method returns the seconds portion of the specified timespec |
---|
68 | * |
---|
69 | * @param[in] _time points to the timespec |
---|
70 | * |
---|
71 | * @return The seconds portion of @a _time. |
---|
72 | */ |
---|
73 | #define _Timespec_Get_seconds( _time ) \ |
---|
74 | ((_time)->tv_sec) |
---|
75 | |
---|
76 | /** @brief Get Nanoseconds Portion of Timespec |
---|
77 | * |
---|
78 | * This method returns the nanoseconds portion of the specified timespec |
---|
79 | * |
---|
80 | * @param[in] _time points to the timespec |
---|
81 | * |
---|
82 | * @return The nanoseconds portion of @a _time. |
---|
83 | */ |
---|
84 | #define _Timespec_Get_nanoseconds( _time ) \ |
---|
85 | ((_time)->tv_nsec) |
---|
86 | |
---|
87 | /** @brief Is Timespec Valid |
---|
88 | * |
---|
89 | * This method determines the validity of a timespec. |
---|
90 | * |
---|
91 | * @param[in] time is the timespec instance to validate. |
---|
92 | * |
---|
93 | * @return This method returns true if @a time is valid and |
---|
94 | * false otherwise. |
---|
95 | */ |
---|
96 | bool _Timespec_Is_valid( |
---|
97 | const struct timespec *time |
---|
98 | ); |
---|
99 | |
---|
100 | /** @brief Timespec Less Than Operator |
---|
101 | * |
---|
102 | * This method is the less than operator for timespecs. |
---|
103 | * |
---|
104 | * @param[in] lhs is the left hand side timespec |
---|
105 | * @param[in] rhs is the right hand side timespec |
---|
106 | * |
---|
107 | * @return This method returns true if @a lhs is less than the @a rhs and |
---|
108 | * false otherwise. |
---|
109 | */ |
---|
110 | bool _Timespec_Less_than( |
---|
111 | const struct timespec *lhs, |
---|
112 | const struct timespec *rhs |
---|
113 | ); |
---|
114 | |
---|
115 | /** @brief Timespec Greater Than Operator |
---|
116 | * |
---|
117 | * This method is the greater than operator for timespecs. |
---|
118 | * |
---|
119 | * @param[in] lhs is the left hand side timespec |
---|
120 | * @param[in] rhs is the right hand side timespec |
---|
121 | * |
---|
122 | * @return This method returns true if @a lhs is greater than the @a rhs and |
---|
123 | * false otherwise. |
---|
124 | */ |
---|
125 | bool _Timespec_Greater_than( |
---|
126 | const struct timespec *lhs, |
---|
127 | const struct timespec *rhs |
---|
128 | ); |
---|
129 | |
---|
130 | /** @brief Timespec equal to Operator |
---|
131 | * |
---|
132 | * This method is the is equal to than operator for timespecs. |
---|
133 | * |
---|
134 | * @param[in] lhs is the left hand side timespec |
---|
135 | * @param[in] rhs is the right hand side timespec |
---|
136 | * |
---|
137 | * @return This method returns true if @a lhs is equal to @a rhs and |
---|
138 | * false otherwise. |
---|
139 | */ |
---|
140 | #define _Timespec_Equal_to( lhs, rhs ) \ |
---|
141 | ( ((lhs)->tv_sec == (rhs)->tv_sec) && \ |
---|
142 | ((lhs)->tv_nsec == (rhs)->tv_nsec) \ |
---|
143 | ) |
---|
144 | |
---|
145 | /** @brief Add to a Timespec |
---|
146 | * |
---|
147 | * This routine adds two timespecs. The second argument is added |
---|
148 | * to the first. |
---|
149 | * |
---|
150 | * @param[in] time is the base time to be added to |
---|
151 | * @param[in] add is the timespec to add to the first argument |
---|
152 | * |
---|
153 | * @return This method returns the number of seconds @a time increased by. |
---|
154 | */ |
---|
155 | uint32_t _Timespec_Add_to( |
---|
156 | struct timespec *time, |
---|
157 | const struct timespec *add |
---|
158 | ); |
---|
159 | |
---|
160 | /** @brief Convert Timespec to Number of Ticks |
---|
161 | * |
---|
162 | * This routine convert the @a time timespec to the corresponding number |
---|
163 | * of clock ticks. |
---|
164 | * |
---|
165 | * @param[in] time is the time to be converted |
---|
166 | * |
---|
167 | * @return This method returns the number of ticks computed. |
---|
168 | */ |
---|
169 | uint32_t _Timespec_To_ticks( |
---|
170 | const struct timespec *time |
---|
171 | ); |
---|
172 | |
---|
173 | /** @brief Convert Ticks to Timespec |
---|
174 | * |
---|
175 | * This routine converts the @a ticks value to the corresponding |
---|
176 | * timespec format @a time. |
---|
177 | * |
---|
178 | * @param[in] time is the timespec format time result |
---|
179 | * @param[in] ticks is the number of ticks to convert |
---|
180 | */ |
---|
181 | void _Timespec_From_ticks( |
---|
182 | uint32_t ticks, |
---|
183 | struct timespec *time |
---|
184 | ); |
---|
185 | |
---|
186 | /** @brief Subtract Two Timespec |
---|
187 | * |
---|
188 | * This routine subtracts two timespecs. @a result is set to |
---|
189 | * @a end - @a start. |
---|
190 | * |
---|
191 | * @param[in] start is the starting time |
---|
192 | * @param[in] end is the ending time |
---|
193 | * @param[in] result is the difference between starting and ending time. |
---|
194 | * |
---|
195 | * @return This method fills in @a result. |
---|
196 | */ |
---|
197 | void _Timespec_Subtract( |
---|
198 | const struct timespec *start, |
---|
199 | const struct timespec *end, |
---|
200 | struct timespec *result |
---|
201 | ); |
---|
202 | |
---|
203 | /** @brief Divide Timespec By Integer |
---|
204 | * |
---|
205 | * This routine divides a timespec by an integer value. The expected |
---|
206 | * use is to assist in benchmark calculations where you typically |
---|
207 | * divide a duration by a number of iterations. |
---|
208 | * |
---|
209 | * @param[in] time is the total |
---|
210 | * @param[in] iterations is the number of iterations |
---|
211 | * @param[in] result is the average time. |
---|
212 | * |
---|
213 | * @return This method fills in @a result. |
---|
214 | */ |
---|
215 | void _Timespec_Divide_by_integer( |
---|
216 | const struct timespec *time, |
---|
217 | uint32_t iterations, |
---|
218 | struct timespec *result |
---|
219 | ); |
---|
220 | |
---|
221 | /** @brief Divide Timespec |
---|
222 | * |
---|
223 | * This routine divides a timespec by another timespec. The |
---|
224 | * intended use is for calculating percentages to three decimal points. |
---|
225 | * |
---|
226 | * @param[in] lhs is the left hand number |
---|
227 | * @param[in] rhs is the right hand number |
---|
228 | * @param[in] ival_percentage is the integer portion of the average |
---|
229 | * @param[in] fval_percentage is the thousandths of percentage |
---|
230 | * |
---|
231 | * @return This method fills in @a result. |
---|
232 | */ |
---|
233 | void _Timespec_Divide( |
---|
234 | const struct timespec *lhs, |
---|
235 | const struct timespec *rhs, |
---|
236 | uint32_t *ival_percentage, |
---|
237 | uint32_t *fval_percentage |
---|
238 | ); |
---|
239 | |
---|
240 | #ifdef __cplusplus |
---|
241 | } |
---|
242 | #endif |
---|
243 | |
---|
244 | /**@}*/ |
---|
245 | |
---|
246 | #endif |
---|
247 | /* end of include file */ |
---|