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
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
37 #include <CoreServices/CoreServices.h>
38 #include <mach/mach.h>
39 #include <mach/mach_time.h>
43 #include <pulse/timeval.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/core-error.h>
47 #include "core-rtclock.h"
49 pa_usec_t
pa_rtclock_age(const struct timeval
*tv
) {
53 return pa_timeval_diff(pa_rtclock_get(&now
), tv
);
56 struct timeval
*pa_rtclock_get(struct timeval
*tv
) {
58 #if defined(OS_IS_DARWIN)
59 uint64_t val
, abs_time
= mach_absolute_time();
62 nanos
= AbsoluteToNanoseconds(*(AbsoluteTime
*) &abs_time
);
63 val
= *(uint64_t *) &nanos
;
65 tv
->tv_sec
= val
/ PA_NSEC_PER_SEC
;
66 tv
->tv_usec
= (val
% PA_NSEC_PER_SEC
) / PA_NSEC_PER_USEC
;
70 #elif defined(HAVE_CLOCK_GETTIME)
73 #ifdef CLOCK_MONOTONIC
74 /* No locking or atomic ops for no_monotonic here */
75 static pa_bool_t no_monotonic
= FALSE
;
78 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) < 0)
82 #endif /* CLOCK_MONOTONIC */
83 pa_assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
87 tv
->tv_sec
= ts
.tv_sec
;
88 tv
->tv_usec
= ts
.tv_nsec
/ PA_NSEC_PER_USEC
;
91 #endif /* HAVE_CLOCK_GETTIME */
93 return pa_gettimeofday(tv
);
96 pa_bool_t
pa_rtclock_hrtimer(void) {
98 #if defined (OS_IS_DARWIN)
99 mach_timebase_info_data_t tbi
;
102 mach_timebase_info(&tbi
);
104 /* nsec = nticks * (N/D) - we want 1 tick == resolution !? */
105 time_nsec
= tbi
.numer
/ tbi
.denom
;
106 return time_nsec
<= (long) (PA_HRTIMER_THRESHOLD_USEC
*PA_NSEC_PER_USEC
);
108 #elif defined(HAVE_CLOCK_GETTIME)
111 #ifdef CLOCK_MONOTONIC
113 if (clock_getres(CLOCK_MONOTONIC
, &ts
) >= 0)
114 return ts
.tv_sec
== 0 && ts
.tv_nsec
<= (long) (PA_HRTIMER_THRESHOLD_USEC
*PA_NSEC_PER_USEC
);
116 #endif /* CLOCK_MONOTONIC */
118 pa_assert_se(clock_getres(CLOCK_REALTIME
, &ts
) == 0);
119 return ts
.tv_sec
== 0 && ts
.tv_nsec
<= (long) (PA_HRTIMER_THRESHOLD_USEC
*PA_NSEC_PER_USEC
);
121 #endif /* HAVE_CLOCK_GETTIME */
126 #define TIMER_SLACK_NS (int) ((500 * PA_NSEC_PER_USEC))
128 void pa_rtclock_hrtimer_enable(void) {
130 #ifdef PR_SET_TIMERSLACK
133 if ((slack_ns
= prctl(PR_GET_TIMERSLACK
, 0, 0, 0, 0)) < 0) {
134 pa_log_info("PR_GET_TIMERSLACK/PR_SET_TIMERSLACK not supported.");
138 pa_log_debug("Timer slack is set to %i us.", (int) (slack_ns
/PA_NSEC_PER_USEC
));
140 if (slack_ns
> TIMER_SLACK_NS
) {
141 slack_ns
= TIMER_SLACK_NS
;
143 pa_log_debug("Setting timer slack to %i us.", (int) (slack_ns
/PA_NSEC_PER_USEC
));
145 if (prctl(PR_SET_TIMERSLACK
, slack_ns
, 0, 0, 0) < 0) {
146 pa_log_warn("PR_SET_TIMERSLACK failed: %s", pa_cstrerror(errno
));
154 struct timeval
* pa_rtclock_from_wallclock(struct timeval
*tv
) {
156 #ifdef HAVE_CLOCK_GETTIME
157 struct timeval wc_now
, rt_now
;
159 pa_gettimeofday(&wc_now
);
160 pa_rtclock_get(&rt_now
);
164 /* pa_timeval_sub() saturates on underflow! */
166 if (pa_timeval_cmp(&wc_now
, tv
) < 0)
167 pa_timeval_add(&rt_now
, pa_timeval_diff(tv
, &wc_now
));
169 pa_timeval_sub(&rt_now
, pa_timeval_diff(&wc_now
, tv
));
177 pa_usec_t
pa_timespec_load(const struct timespec
*ts
) {
179 if (PA_UNLIKELY(!ts
))
180 return PA_USEC_INVALID
;
183 (pa_usec_t
) ts
->tv_sec
* PA_USEC_PER_SEC
+
184 (pa_usec_t
) ts
->tv_nsec
/ PA_NSEC_PER_USEC
;
187 struct timespec
* pa_timespec_store(struct timespec
*ts
, pa_usec_t v
) {
190 if (PA_UNLIKELY(v
== PA_USEC_INVALID
)) {
191 ts
->tv_sec
= PA_INT_TYPE_MAX(time_t);
192 ts
->tv_nsec
= (long) (PA_NSEC_PER_SEC
-1);
196 ts
->tv_sec
= (time_t) (v
/ PA_USEC_PER_SEC
);
197 ts
->tv_nsec
= (long) ((v
% PA_USEC_PER_SEC
) * PA_NSEC_PER_USEC
);
202 static struct timeval
* wallclock_from_rtclock(struct timeval
*tv
) {
204 #ifdef HAVE_CLOCK_GETTIME
205 struct timeval wc_now
, rt_now
;
207 pa_gettimeofday(&wc_now
);
208 pa_rtclock_get(&rt_now
);
212 /* pa_timeval_sub() saturates on underflow! */
214 if (pa_timeval_cmp(&rt_now
, tv
) < 0)
215 pa_timeval_add(&wc_now
, pa_timeval_diff(tv
, &rt_now
));
217 pa_timeval_sub(&wc_now
, pa_timeval_diff(&rt_now
, tv
));
225 struct timeval
* pa_timeval_rtstore(struct timeval
*tv
, pa_usec_t v
, pa_bool_t rtclock
) {
228 if (v
== PA_USEC_INVALID
)
231 pa_timeval_store(tv
, v
);
234 tv
->tv_usec
|= PA_TIMEVAL_RTCLOCK
;
236 wallclock_from_rtclock(tv
);