summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/score/timestampimpl.h
blob: 22df1dd3a1c918c66f193689702f5783008301b5 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSScoreTimestamp
 *
 * @brief This header file provides interfaces of the
 *   @ref RTEMSScoreTimestamp which are only used by the implementation.
 */

/*
 *  COPYRIGHT (c) 1989-2009.
 *  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_TIMESTAMPIMPL_H
#define _RTEMS_SCORE_TIMESTAMPIMPL_H

/**
 * @addtogroup RTEMSScoreTimestamp
 *
 * @{
 */

#include <rtems/score/timestamp.h>
#include <rtems/score/basedefs.h>

#include <sys/time.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Sets timestamp to specified seconds and nanoseconds.
 *
 * This method sets the timestamp to the specified @a _seconds and @a _nanoseconds
 * value.
 *
 * @param[out] _time The timestamp instance to set.
 * @param _seconds The seconds portion of the timestamp.
 * @param _nanoseconds The nanoseconds portion of the timestamp.
 */
RTEMS_INLINE_ROUTINE void _Timestamp_Set(
  Timestamp_Control *_time,
  time_t             _seconds,
  long               _nanoseconds
)
{
  struct timespec _ts;

  _ts.tv_sec = _seconds;
  _ts.tv_nsec = _nanoseconds;

  *_time = tstosbt(_ts);
}

/**
 * @brief Sets the timestamp to zero.
 *
 * This method sets the timestamp to zero.
 * value.
 *
 * @param[out] _time The timestamp instance to zero.
 */

RTEMS_INLINE_ROUTINE void _Timestamp_Set_to_zero(
  Timestamp_Control *_time
)
{
  *_time = 0;
}

/**
 * @brief Checks if the left hand side timestamp is less than the right one.
 *
 * This method is the less than operator for timestamps.
 *
 * @param _lhs The left hand side timestamp.
 * @param _rhs The right hand side timestamp.
 *
 * @retval true @a _lhs is less than the @a _rhs.
 * @retval false @a _lhs is greater or equal than @a rhs.
 */

RTEMS_INLINE_ROUTINE bool _Timestamp_Less_than(
  const Timestamp_Control *_lhs,
  const Timestamp_Control *_rhs
)
{
  return *_lhs < *_rhs;
}

/**
 * @brief Checks if the left hand side timestamp is greater than the right one.
 *
 * This method is the greater than operator for timestamps.
 *
 * @param _lhs The left hand side timestamp.
 * @param _rhs The right hand side timestamp.
 *
 * @retval true @a _lhs is greater than the @a _rhs.
 * @retval false @a _lhs is less or equal than @a _rhs.
 */

RTEMS_INLINE_ROUTINE bool _Timestamp_Greater_than(
  const Timestamp_Control *_lhs,
  const Timestamp_Control *_rhs
)
{
  return *_lhs > *_rhs;
}

/**
 * @brief Checks if the timestamps are equal.
 *
 * This method is the is equal to than operator for timestamps.
 *
 * @param _lhs The left hand side timestamp.
 * @param _rhs The right hand side timestamp.
 *
 * @retval true @a _lhs is equal to  @a _rhs
 * @retval false @a _lhs is not equal to @a _rhs.
 */

RTEMS_INLINE_ROUTINE bool _Timestamp_Equal_to(
  const Timestamp_Control *_lhs,
  const Timestamp_Control *_rhs
)
{
  return *_lhs == *_rhs;
}

/**
 * @brief Adds two timestamps.
 *
 * This routine adds two timestamps.  The second argument is added
 * to the first.
 *
 * @param[in, out] _time The base time to be added to.
 * @param _add points The timestamp to add to the first argument.
 */
RTEMS_INLINE_ROUTINE void _Timestamp_Add_to(
  Timestamp_Control *_time,
  const Timestamp_Control *_add
)
{
  *_time += *_add;
}

/**
 * @brief Subtracts two timestamps.
 *
 * This routine subtracts two timestamps.  @a result is set to
 * @a end - @a start.
 *
 * @param _start The starting time.
 * @param _end The ending time.
 * @param[out] _result Contains the difference between starting and ending
 *      time after the method call.
 */
RTEMS_INLINE_ROUTINE void _Timestamp_Subtract(
  const Timestamp_Control *_start,
  const Timestamp_Control *_end,
  Timestamp_Control       *_result
)
{
  *_result = *_end - *_start;
}

/**
 * @brief Divides a timestamp by another timestamp.
 *
 * This routine divides a timestamp by another timestamp.  The
 * intended use is for calculating percentages to three decimal points.
 *
 * @param _lhs The left hand number.
 * @param _rhs The right hand number.
 * @param[out] _ival_percentage The integer portion of the average.
 * @param[out] _fval_percentage The thousandths of percentage.
 */
RTEMS_INLINE_ROUTINE void _Timestamp_Divide(
  const Timestamp_Control *_lhs,
  const Timestamp_Control *_rhs,
  uint32_t                *_ival_percentage,
  uint32_t                *_fval_percentage
)
{
  struct timespec _ts_left;
  struct timespec _ts_right;

  _ts_left = sbttots( *_lhs );
  _ts_right = sbttots( *_rhs );

  _Timespec_Divide(
    &_ts_left,
    &_ts_right,
    _ival_percentage,
    _fval_percentage
  );
}

/**
 * @brief Gets seconds portion of timestamp.
 *
 * This method returns the seconds portion of the specified timestamp.
 *
 * @param _time The timestamp.
 *
 * @return The seconds portion of @a _time.
 */
RTEMS_INLINE_ROUTINE time_t _Timestamp_Get_seconds(
  const Timestamp_Control *_time
)
{
  return (*_time >> 32);
}

/**
 * @brief Gets nanoseconds portion of timestamp.
 *
 * This method returns the nanoseconds portion of the specified timestamp.
 *
 * @param _time The timestamp.
 *
 * @return The nanoseconds portion of @a _time.
 */
RTEMS_INLINE_ROUTINE uint32_t _Timestamp_Get_nanoseconds(
  const Timestamp_Control *_time
)
{
  struct timespec _ts;

  _ts = sbttots( *_time );

  return (uint32_t) _ts.tv_nsec;
}

/**
 * @brief Gets the timestamp as nanoseconds.
 *
 * This method returns the timestamp as nanoseconds.
 *
 * @param _time The timestamp.
 *
 * @return The time in nanoseconds.
 */
RTEMS_INLINE_ROUTINE uint64_t _Timestamp_Get_as_nanoseconds(
  const Timestamp_Control *_time
)
{
  struct timespec _ts;

  _ts = sbttots( *_time );

  return _Timespec_Get_as_nanoseconds( &_ts );
}

/**
 * @brief Converts timestamp to struct timespec.
 *
 * @param _timestamp The timestamp.
 * @param[out] _timespec The timespec to be filled in by the method.
 */
RTEMS_INLINE_ROUTINE void _Timestamp_To_timespec(
  const Timestamp_Control *_timestamp,
  struct timespec         *_timespec
)
{
  *_timespec = sbttots( *_timestamp );
}

/**
 * @brief Converts timestamp to struct timeval.
 *
 * @param _timestamp The timestamp.
 * @param[out] _timeval The timeval to be filled in by the method.
 */
RTEMS_INLINE_ROUTINE void _Timestamp_To_timeval(
  const Timestamp_Control *_timestamp,
  struct timeval          *_timeval
)
{
  *_timeval = sbttotv( *_timestamp );
}

#ifdef __cplusplus
}
#endif

/** @} */

#endif
/* end of include file */