summaryrefslogtreecommitdiffstats
path: root/cpukit/score/include/rtems/score/timespec.h
blob: 9a27f708bd69fc23eaf021f1b7920fba5f10399a (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
/** 
 *  @file  rtems/score/timespec.h
 *
 *  This include file contains helpers for manipulating timespecs.
 */

/*
 *  COPYRIGHT (c) 1989-2008.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#ifndef _RTEMS_SCORE_TIMESPEC_H
#define _RTEMS_SCORE_TIMESPEC_H

/**
 *  @defgroup Timespec Helpers
 *
 *  This handler encapsulates functionality related to manipulating
 *  POSIX struct timespecs.
 */
/**@{*/

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <rtems/score/tod.h>
#include <rtems/score/watchdog.h>

/** @brief Is Timespec Valid
 *
 *  This method determines the validatity of a timespec.
 *
 *  @param[in] time is the timespec instance to validate.
 *
 *  @return This method returns true if @a time is valid and 
 *          false otherwise.
 */
boolean _Timespec_Is_valid(
  const struct timespec *time
);

/** @brief Timespec Less Than Operator
 *
 *  This method is the less than operator for timespecs.
 *
 *  @param[in] lhs is the left hand side timespec
 *  @param[in] rhs is the left hand side timespec
 *
 *  @return This method returns true if @a lhs is less than the @a rhs and 
 *          false otherwise.
 */
boolean _Timespec_Less_than(
  const struct timespec *lhs,
  const struct timespec *rhs
);

/** @brief 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 left hand side timespec
 *
 *  @return This method returns true if @a lhs is greater than the @a rhs and 
 *          false otherwise.
 */
boolean _Timespec_Greater_than(
  const struct timespec *lhs,
  const struct timespec *rhs
);

/** @brief 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 left hand side timespec
 *
 *  @return 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 Add to a Timespec
 *
 *  This routine adds two timespecs.  The second argument is added
 *  to the first.
 *
 *  @param[in] time is the base time to be added to
 *  @param[in] add is the timespec to add to the first argument 
 *
 *  @return This method returns the number of seconds @a time increased by.
 */
uint32_t _Timespec_Add_to(
  struct timespec       *time,
  const struct timespec *add
);

/** @brief Convert Timespec to Number of Ticks
 *
 *  This routine convert the @a time timespec to the corresponding number
 *  of clock ticks.
 *
 *  @param[in] time is the time to be converted
 *
 *  @return This method returns the number of ticks computed.
 */
uint32_t _Timespec_To_ticks(
  const struct timespec *time
);

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

/** @brief Subtract Two Timespec
 *
 *  This routine subtracts two timespecs.  @a result is set to
 *  @a end - @a start.
 *
 *  @param[in] start is the starting time
 *  @param[in] end is the ending time
 *  @param[in] result is the difference between starting and ending time.
 *
 *  @return This method fills in @a result.
 */
void _Timespec_Subtract(
  const struct timespec *start,
  const struct timespec *end,
  struct timespec       *result
);

/** @brief Divide Timespec By Integet
 *
 *  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[in] time is the total
 *  @param[in] iterations is the number of iterations
 *  @param[in] result is the average time.
 *
 *  @return This method fills in @a result.
 */
void _Timespec_Divide_by_integer(
  const struct timespec *time,
  uint32_t               iterations,
  struct timespec       *result
);

/** @brief Divide Timespec
 *
 *  This routine divides a timespec by another timespec.  The 
 *  intended use is for calculating percentages to three decimal points.
 *
 *  @param[in] lhs is the left hand number
 *  @param[in] rhs is the righ hand number
 *  @param[in] ival_percentage is the integer portion of the average
 *  @param[in] fval_percentage is the thousandths of percentage
 *
 *  @return This method fills in @a result.
 */
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 */