summaryrefslogtreecommitdiffstats
path: root/src/util/timespec.h
blob: ab112e4e621b574d33ed4b90401b997aa6ea7956 (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright © 2014 - 2015 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * \file timespec.h
 *
 * Helpers to deal with timespec structures.
 */

#ifndef TIMESPEC_H
#define TIMESPEC_H

#include <stdint.h>
#include <assert.h>
#include <time.h>
#include <stdbool.h>

#define NSEC_PER_SEC 1000000000

/**
 * Add timespecs
 *
 * \param r[out] result: a + b
 * \param a[in] operand
 * \param b[in] operand
 */
static inline void
timespec_add(struct timespec *r,
             const struct timespec *a, const struct timespec *b)
{
   r->tv_sec = a->tv_sec + b->tv_sec;
   r->tv_nsec = a->tv_nsec + b->tv_nsec;
   if (r->tv_nsec > NSEC_PER_SEC) {
      r->tv_sec++;
      r->tv_nsec -= NSEC_PER_SEC;
   }
}

/**
 * Subtract timespecs
 *
 * \param r[out] result: a - b
 * \param a[in] operand
 * \param b[in] operand
 */
static inline void
timespec_sub(struct timespec *r,
             const struct timespec *a, const struct timespec *b)
{
   r->tv_sec = a->tv_sec - b->tv_sec;
   r->tv_nsec = a->tv_nsec - b->tv_nsec;
   if (r->tv_nsec < 0) {
      r->tv_sec--;
      r->tv_nsec += NSEC_PER_SEC;
   }
}

/**
 * Add a nanosecond value to a timespec
 *
 * \param r[out] result: a + b
 * \param a[in] base operand as timespec
 * \param b[in] operand in nanoseconds
 */
static inline void
timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b)
{
   r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC);
   r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC);

   if (r->tv_nsec >= NSEC_PER_SEC) {
      r->tv_sec++;
      r->tv_nsec -= NSEC_PER_SEC;
   } else if (r->tv_nsec < 0) {
      r->tv_sec--;
      r->tv_nsec += NSEC_PER_SEC;
   }
}

/**
 * Add a millisecond value to a timespec
 *
 * \param r[out] result: a + b
 * \param a[in] base operand as timespec
 * \param b[in] operand in milliseconds
 */
static inline void
timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b)
{
   timespec_add_nsec(r, a, b * 1000000);
}

/**
 * Convert timespec to nanoseconds
 *
 * \param a timespec
 * \return nanoseconds
 */
static inline int64_t
timespec_to_nsec(const struct timespec *a)
{
   return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
}

/**
 * Subtract timespecs and return result in nanoseconds
 *
 * \param a[in] operand
 * \param b[in] operand
 * \return to_nanoseconds(a - b)
 */
static inline int64_t
timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b)
{
   struct timespec r;
   timespec_sub(&r, a, b);
   return timespec_to_nsec(&r);
}

/**
 * Convert timespec to milliseconds
 *
 * \param a timespec
 * \return milliseconds
 *
 * Rounding to integer milliseconds happens always down (floor()).
 */
static inline int64_t
timespec_to_msec(const struct timespec *a)
{
   return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
}

/**
 * Subtract timespecs and return result in milliseconds
 *
 * \param a[in] operand
 * \param b[in] operand
 * \return to_milliseconds(a - b)
 */
static inline int64_t
timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
{
   return timespec_sub_to_nsec(a, b) / 1000000;
}

/**
 * Convert timespec to microseconds
 *
 * \param a timespec
 * \return microseconds
 *
 * Rounding to integer microseconds happens always down (floor()).
 */
static inline int64_t
timespec_to_usec(const struct timespec *a)
{
   return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000;
}

/**
 * Convert timespec to protocol data
 *
 * \param a timespec
 * \param tv_sec_hi[out] the high bytes of the seconds part
 * \param tv_sec_lo[out] the low bytes of the seconds part
 * \param tv_nsec[out] the nanoseconds part
 *
 * The input timespec must be normalized (the nanoseconds part should
 * be less than 1 second) and non-negative.
 */
static inline void
timespec_to_proto(const struct timespec *a, uint32_t *tv_sec_hi,
                  uint32_t *tv_sec_lo, uint32_t *tv_nsec)
{
   assert(a->tv_sec >= 0);
   assert(a->tv_nsec >= 0 && a->tv_nsec < NSEC_PER_SEC);

   uint64_t sec64 = a->tv_sec;

   *tv_sec_hi = sec64 >> 32;
   *tv_sec_lo = sec64 & 0xffffffff;
   *tv_nsec = a->tv_nsec;
}

/**
 * Convert nanoseconds to timespec
 *
 * \param a timespec
 * \param b nanoseconds
 */
static inline void
timespec_from_nsec(struct timespec *a, int64_t b)
{
   a->tv_sec = b / NSEC_PER_SEC;
   a->tv_nsec = b % NSEC_PER_SEC;
}

/**
 * Convert microseconds to timespec
 *
 * \param a timespec
 * \param b microseconds
 */
static inline void
timespec_from_usec(struct timespec *a, int64_t b)
{
   timespec_from_nsec(a, b * 1000);
}

/**
 * Convert milliseconds to timespec
 *
 * \param a timespec
 * \param b milliseconds
 */
static inline void
timespec_from_msec(struct timespec *a, int64_t b)
{
   timespec_from_nsec(a, b * 1000000);
}

/**
 * Convert protocol data to timespec
 *
 * \param a[out] timespec
 * \param tv_sec_hi the high bytes of seconds part
 * \param tv_sec_lo the low bytes of seconds part
 * \param tv_nsec the nanoseconds part
 */
static inline void
timespec_from_proto(struct timespec *a, uint32_t tv_sec_hi,
                    uint32_t tv_sec_lo, uint32_t tv_nsec)
{
   a->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
   a->tv_nsec = tv_nsec;
}

/**
 * Check if a timespec is zero
 *
 * \param a timespec
 * \return whether the timespec is zero
 */
static inline bool
timespec_is_zero(const struct timespec *a)
{
   return a->tv_sec == 0 && a->tv_nsec == 0;
}

/**
 * Check if two timespecs are equal
 *
 * \param a[in] timespec to check
 * \param b[in] timespec to check
 * \return whether timespecs a and b are equal
 */
static inline bool
timespec_eq(const struct timespec *a, const struct timespec *b)
{
   return a->tv_sec == b->tv_sec &&
      a->tv_nsec == b->tv_nsec;
}

/**
 * Convert milli-Hertz to nanoseconds
 *
 * \param mhz frequency in mHz, not zero
 * \return period in nanoseconds
 */
static inline int64_t
millihz_to_nsec(uint32_t mhz)
{
   assert(mhz > 0);
   return 1000000000000LL / mhz;
}

/**
 * Checks whether a timespec value is after another
 *
 * \param a[in] timespec to compare
 * \param b[in] timespec to compare
 * \return whether a is after b
 */
static inline bool
timespec_after(const struct timespec *a, const struct timespec *b)
{
   return (a->tv_sec == b->tv_sec) ?
      (a->tv_nsec > b->tv_nsec) :
      (a->tv_sec > b->tv_sec);
}

/**
 * Checks whether a timespec value is after the current time
 *
 * \param clock_domain[in] clock in which to do the comparison
 * \param deadline[in] timespec to compare
 * \return whether deadline is after the current time
 */
static inline bool
timespec_passed(clockid_t clock_domain, const struct timespec *deadline)
{
   struct timespec current_time;
   clock_gettime(clock_domain, &current_time);
   return timespec_after(&current_time, deadline);
}

#endif /* TIMESPEC_H */