summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/score/timespec.h
blob: 2090f19b32dec62a970c307acf1b77ea90915743 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup Timespec
 *
 * @brief This header file provides the interfaces of the
 * @ref RTEMSScoreTimespec.
 */

/*
 * COPYRIGHT (C) 1989, 2021 On-Line Applications Research Corporation (OAR).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _RTEMS_SCORE_TIMESPEC_H
#define _RTEMS_SCORE_TIMESPEC_H

/**
 * @defgroup RTEMSScoreTimespec Timespec Helpers
 *
 * @ingroup RTEMSScore
 *
 * @brief This group contains the implementation to support operations with
 *   variables of the POSIX defined struct timespec type.
 *
 * @{
 */

#include <stdbool.h> /* bool */
#include <stdint.h> /* uint32_t */
#include <time.h> /* struct timespec */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Set timespec to seconds nanosecond.
 *
 *  This method sets the timespec to the specified seconds and nanoseconds
 *  value.
 *
 *  @param[in] _time points to the timespec instance to validate.
 *  @param[in] _seconds is the seconds portion of the timespec
 *  @param[in] _nanoseconds is the nanoseconds portion of the timespec
 */
#define _Timespec_Set( _time, _seconds, _nanoseconds ) \
	do { \
	   (_time)->tv_sec = (_seconds); \
	   (_time)->tv_nsec = (_nanoseconds); \
	} while (0)

/**
 * @brief Sets the Timespec to Zero
 *
 *  This method sets the timespec to zero.
 *  value.
 *
 *  @param[in] _time points to the timespec instance to zero.
 */
#define _Timespec_Set_to_zero( _time ) \
	do { \
	   (_time)->tv_sec = 0; \
	   (_time)->tv_nsec = 0; \
	} while (0)

/**
 * @brief Get seconds portion of timespec.
 *
 *  This method returns the seconds portion of the specified timespec
 *
 *  @param[in] _time points to the timespec
 *
 *  @retval The seconds portion of @a _time.
 */
#define _Timespec_Get_seconds( _time ) \
	((_time)->tv_sec)

/**
 * @brief Get nanoseconds portion of timespec.
 *
 *  This method returns the nanoseconds portion of the specified timespec
 *
 *  @param[in] _time points to the timespec
 *
 *  @retval The nanoseconds portion of @a _time.
 */
#define _Timespec_Get_nanoseconds( _time ) \
	((_time)->tv_nsec)

/**
 *  @brief Gets the timestamp as nanoseconds.
 *
 *  This method returns the timestamp as nanoseconds.
 *
 *  @param time points to the timestamp.
 *
 *  @return The time in nanoseconds.
 */
uint64_t _Timespec_Get_as_nanoseconds(
  const struct timespec *time
);

/**
 * @brief Checks if the values in @a time are non-negative.
 *
 * @param[in] time is the timespec instance to validate.
 *
 * @retval true If @a time is filled with non-negative values.
 * @retval false If @a time is not filled with non-negative values.
 */
bool _Timespec_Is_non_negative(
  const struct timespec *time
);

/**
 * @brief Checks if timespec is valid.
 *
 * This method determines the validity of a timespec.
 *
 * @param time is the timespec instance to validate.
 *
 * @retval true The timespec is valid.
 * @retval false The timespec is invalid.
 */
bool _Timespec_Is_valid(
  const struct timespec *time
);

/**
 * @brief Checks if the left hand side timespec is less than the right one.
 *
 * This method is the less than operator for timespecs.
 *
 * @param lhs is the left hand side timespec.
 * @param rhs is the right hand side timespec.
 *
 * @retval true @a lhs is less than @a rhs.
 * @retval false @a lhs is greater than @a rhs.
 */
bool _Timespec_Less_than(
  const struct timespec *lhs,
  const struct timespec *rhs
);

/**
 * @brief The Timespec "greater than" operator.
 *
 *  This method is the greater than operator for timespecs.
 *
 *  @param[in] _lhs is the left hand side timespec
 *  @param[in] _rhs is the right hand side timespec
 *
 *  @retval This method returns true if @a lhs is greater than the @a rhs and
 *          false otherwise.
 */
#define _Timespec_Greater_than( _lhs, _rhs ) \
  _Timespec_Less_than( _rhs, _lhs )

/**
 * @brief The Timespec "equal to" operator.
 *
 *  This method is the is equal to than operator for timespecs.
 *
 *  @param[in] lhs is the left hand side timespec
 *  @param[in] rhs is the right hand side timespec
 *
 *  @retval This method returns true if @a lhs is equal to  @a rhs and
 *          false otherwise.
 */
#define _Timespec_Equal_to( lhs, rhs ) \
  ( ((lhs)->tv_sec  == (rhs)->tv_sec) &&   \
    ((lhs)->tv_nsec == (rhs)->tv_nsec)     \
  )

/**
 * @brief Adds two timespecs.
 *
 * This routine adds two timespecs.  The second argument is added
 * to the first.
 *
 * @param time The base time to be added to.
 * @param add The timespec to add to the first argument.
 *
 * @return The number of seconds @a time increased by.
 */
uint32_t _Timespec_Add_to(
  struct timespec       *time,
  const struct timespec *add
);

/**
 * @brief Converts timespec to number of ticks.
 *
 * This routine convert the @a time timespec to the corresponding number
 * of clock ticks.
 *
 * @param time The time to be converted.
 *
 * @return The number of ticks computed.
 */
uint32_t _Timespec_To_ticks(
  const struct timespec *time
);

/**
 * @brief Converts ticks to timespec.
 *
 *  This routine converts the @a ticks value to the corresponding
 *  timespec format @a time.
 *
 *  @param ticks The number of ticks to convert.
 *  @param[out] time The timespec format time result.
 */
void _Timespec_From_ticks(
  uint32_t         ticks,
  struct timespec *time
);

/**
 * @brief Subtracts two timespec.
 *
 * This routine subtracts two timespecs.  @a result is set to
 * @a end - @a start.
 *
 * @param start The starting time
 * @param end The ending time
 * @param[out] result The difference between starting and ending time.
 */
void _Timespec_Subtract(
  const struct timespec *start,
  const struct timespec *end,
  struct timespec       *result
);

/**
 * @brief Divides timespec by an integer.
 *
 * This routine divides a timespec by an integer value.  The expected
 * use is to assist in benchmark calculations where you typically
 * divide a duration by a number of iterations.
 *
 * @param time The total.
 * @param iterations The number of iterations.
 * @param[out] result The average time.
 */
void _Timespec_Divide_by_integer(
  const struct timespec *time,
  uint32_t               iterations,
  struct timespec       *result
);

/**
 * @brief Divides a timespec by another timespec.
 *
 * This routine divides a timespec by another timespec.  The
 * intended use is for calculating percentages to three decimal points.
 *
 * @param lhs The left hand timespec.
 * @param rhs The right hand timespec.
 * @param[out] ival_percentage The integer portion of the average.
 * @param[out] fval_percentage The thousandths of percentage.
 */
void _Timespec_Divide(
  const struct timespec *lhs,
  const struct timespec *rhs,
  uint32_t              *ival_percentage,
  uint32_t              *fval_percentage
);

#ifdef __cplusplus
}
#endif

/** @} */

#endif
/* end of include file */