1
// ------------------------------------------------------------------------
2
// kvu_timestamp.h: Monotonic timestamps
3
// Copyright (C) 2009 Kai Vehmanen
6
// eca-style-version: 3
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License as published by
10
// the Free Software Foundation; either version 2 of the License, or
11
// (at your option) any later version.
13
// This program is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
// GNU General Public License for more details.
18
// You should have received a copy of the GNU General Public License
19
// along with this program; if not, write to the Free Software
20
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
// ------------------------------------------------------------------------
23
#ifndef INCLUDED_KVU_TIMESTAMP_H
24
#define INCLUDED_KVU_TIMESTAMP_H
29
* Returns non-zero if clock is monotonic.
31
* See POSIX 'CLOCK_MONOTONIC'
33
int kvu_clock_is_monotonic(void);
36
* Finds the resolution (precision) of the clock
39
* See POSIX clock_getres().
41
int kvu_clock_getres(struct kvu_timespec *res);
44
* Retrieves a timestamp
46
* See POSIX clock_gettime()
48
int kvu_clock_gettime(struct timespec *tp);
51
* Returns the timestamp as seconds
53
static inline double kvu_timespec_seconds(struct timespec *tp)
56
(static_cast<double>(tp->tv_nsec) / 1000000000.0);
60
* Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
62
static inline void kvu_timespec_clear(struct timespec *tvp)
64
tvp->tv_sec = tvp->tv_nsec = 0;
67
static inline void kvu_timespec_assign(struct timespec *x, const struct timespec *y)
69
x->tv_sec = y->tv_sec;
70
x->tv_nsec = y->tv_nsec;
76
* Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
78
static inline void kvu_timespec_add(const struct timespec *a,
79
const struct timespec *b,
80
struct timespec *result)
82
(result)->tv_sec = (a)->tv_sec + (b)->tv_sec;
83
(result)->tv_nsec = (a)->tv_nsec + (b)->tv_nsec;
84
if ((result)->tv_nsec >= 1000000000) {
86
(result)->tv_nsec -= 1000000000;
93
* Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
95
static inline int kvu_timespec_cmp_lt(const struct timespec *a,
96
const struct timespec *b)
98
return (((a)->tv_sec == (b)->tv_sec) ?
99
((a)->tv_nsec < (b)->tv_nsec) :
100
((a)->tv_sec < (b)->tv_sec));
106
* Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
108
static inline int kvu_timespec_cmp_gt(const struct timespec *a,
109
const struct timespec *b)
111
return (((a)->tv_sec == (b)->tv_sec) ?
112
((a)->tv_nsec > (b)->tv_nsec) :
113
((a)->tv_sec > (b)->tv_sec));
119
* Adapted from macro definitions in glibc's sys/time.h (LGPL 2.1)
121
static inline void kvu_timespec_sub(const struct timespec *a,
122
const struct timespec *b,
123
struct timespec *result)
125
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec;
126
(result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec;
127
if ((result)->tv_nsec < 0) {
129
(result)->tv_nsec += 1000000000;
133
#endif /* INCLUDED_KVU_TIMESTAMP_H */