2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/timeval.h>
32 #include <pulsecore/macro.h>
36 pa_usec_t
pa_rtclock_age(const struct timeval
*tv
) {
40 return pa_timeval_diff(pa_rtclock_get(&now
), tv
);
43 struct timeval
*pa_rtclock_get(struct timeval
*tv
) {
44 #ifdef HAVE_CLOCK_GETTIME
47 #ifdef CLOCK_MONOTONIC
48 /* No locking or atomic ops for no_monotonic here */
49 static pa_bool_t no_monotonic
= FALSE
;
52 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) < 0)
57 pa_assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
61 tv
->tv_sec
= ts
.tv_sec
;
62 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
66 #else /* HAVE_CLOCK_GETTIME */
68 return pa_gettimeofday(tv
);
73 pa_bool_t
pa_rtclock_hrtimer(void) {
74 #ifdef HAVE_CLOCK_GETTIME
77 #ifdef CLOCK_MONOTONIC
78 if (clock_getres(CLOCK_MONOTONIC
, &ts
) >= 0)
79 return ts
.tv_sec
== 0 && ts
.tv_nsec
<= PA_HRTIMER_THRESHOLD_USEC
*1000;
82 pa_assert_se(clock_getres(CLOCK_REALTIME
, &ts
) == 0);
83 return ts
.tv_sec
== 0 && ts
.tv_nsec
<= PA_HRTIMER_THRESHOLD_USEC
*1000;
85 #else /* HAVE_CLOCK_GETTIME */
92 pa_usec_t
pa_rtclock_usec(void) {
95 return pa_timeval_load(pa_rtclock_get(&tv
));
98 struct timeval
* pa_rtclock_from_wallclock(struct timeval
*tv
) {
100 #ifdef HAVE_CLOCK_GETTIME
101 struct timeval wc_now
, rt_now
;
103 pa_gettimeofday(&wc_now
);
104 pa_rtclock_get(&rt_now
);
108 if (pa_timeval_cmp(&wc_now
, tv
) < 0)
109 pa_timeval_add(&rt_now
, pa_timeval_diff(tv
, &wc_now
));
111 pa_timeval_sub(&rt_now
, pa_timeval_diff(&wc_now
, tv
));