4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/timeval.h>
33 #include <pulsecore/macro.h>
37 struct timespec
*pa_timespec_store(struct timespec
*a
, pa_usec_t u
) {
40 a
->tv_sec
= u
/ PA_USEC_PER_SEC
;
42 u
-= (pa_usec_t
) a
->tv_sec
* PA_USEC_PER_SEC
;
44 a
->tv_nsec
= u
* 1000;
49 struct timespec
*pa_timespec_reset(struct timespec
*a
) {
52 a
->tv_sec
= a
->tv_nsec
= 0;
56 pa_usec_t
pa_timespec_load(struct timespec
*ts
) {
59 return (pa_usec_t
) ts
->tv_sec
* PA_USEC_PER_SEC
+ (pa_usec_t
) (ts
->tv_nsec
/ 1000);
62 pa_usec_t
pa_timespec_diff(const struct timespec
*a
, const struct timespec
*b
) {
68 /* Check which whan is the earlier time and swap the two arguments if required. */
69 if (pa_timespec_cmp(a
, b
) < 0) {
70 const struct timespec
*c
;
76 /* Calculate the second difference*/
77 r
= ((pa_usec_t
) a
->tv_sec
- b
->tv_sec
) * PA_USEC_PER_SEC
;
79 /* Calculate the microsecond difference */
80 if (a
->tv_nsec
> b
->tv_nsec
)
81 r
+= (pa_usec_t
) ((a
->tv_nsec
- b
->tv_nsec
) / 1000);
82 else if (a
->tv_nsec
< b
->tv_nsec
)
83 r
-= (pa_usec_t
) ((b
->tv_nsec
- a
->tv_nsec
) / 1000);
88 int pa_timespec_cmp(const struct timespec
*a
, const struct timespec
*b
) {
92 if (a
->tv_sec
< b
->tv_sec
)
95 if (a
->tv_sec
> b
->tv_sec
)
98 if (a
->tv_nsec
< b
->tv_nsec
)
101 if (a
->tv_nsec
> b
->tv_nsec
)
107 struct timespec
* pa_timespec_add(struct timespec
*ts
, pa_usec_t v
) {
111 secs
= (unsigned long) (v
/PA_USEC_PER_SEC
);
113 v
-= ((pa_usec_t
) secs
) * PA_USEC_PER_SEC
;
115 ts
->tv_nsec
+= (long) (v
*1000);
118 while (ts
->tv_nsec
>= PA_NSEC_PER_SEC
) {
120 ts
->tv_nsec
-= PA_NSEC_PER_SEC
;
126 pa_usec_t
pa_rtclock_age(const struct timespec
*ts
) {
130 return pa_timespec_diff(pa_rtclock_get(&now
), ts
);
133 struct timespec
*pa_rtclock_get(struct timespec
*ts
) {
134 static int no_monotonic
= 0;
136 /* No locking or atomic ops for no_monotonic here */
141 if (clock_gettime(CLOCK_MONOTONIC
, ts
) >= 0)
147 pa_assert_se(clock_gettime(CLOCK_REALTIME
, ts
) == 0);
151 int pa_rtclock_hrtimer(void) {
154 if (clock_getres(CLOCK_MONOTONIC
, &ts
) >= 0)
155 return ts
.tv_sec
== 0 && ts
.tv_nsec
<= PA_HRTIMER_THRESHOLD_USEC
*1000;
157 pa_assert_se(clock_getres(CLOCK_REALTIME
, &ts
) == 0);
158 return ts
.tv_sec
== 0 && ts
.tv_nsec
<= PA_HRTIMER_THRESHOLD_USEC
*1000;