virtual-sink: Fix a crash when moving the sink to a new master right after setup.
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / pulsecore / core-rtclock.c
blobac2c097af3c9e0ddda9a8a64ebbc3e5e0e6b239a
1 /***
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
20 USA.
21 ***/
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stddef.h>
28 #include <time.h>
29 #include <sys/time.h>
30 #include <errno.h>
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
34 #endif
36 #ifdef OS_IS_DARWIN
37 #include <CoreServices/CoreServices.h>
38 #include <mach/mach.h>
39 #include <mach/mach_time.h>
40 #include <unistd.h>
41 #endif
43 #ifdef HAVE_WINDOWS_H
44 #include <windows.h>
45 #endif
47 #include <pulse/timeval.h>
48 #include <pulsecore/macro.h>
49 #include <pulsecore/core-error.h>
51 #include "core-rtclock.h"
53 #ifdef OS_IS_WIN32
54 static int64_t counter_freq = 0;
55 #endif
57 pa_usec_t pa_rtclock_age(const struct timeval *tv) {
58 struct timeval now;
59 pa_assert(tv);
61 return pa_timeval_diff(pa_rtclock_get(&now), tv);
64 struct timeval *pa_rtclock_get(struct timeval *tv) {
66 #if defined(OS_IS_DARWIN)
67 uint64_t val, abs_time = mach_absolute_time();
68 Nanoseconds nanos;
70 nanos = AbsoluteToNanoseconds(*(AbsoluteTime *) &abs_time);
71 val = *(uint64_t *) &nanos;
73 tv->tv_sec = val / PA_NSEC_PER_SEC;
74 tv->tv_usec = (val % PA_NSEC_PER_SEC) / PA_NSEC_PER_USEC;
76 return tv;
78 #elif defined(HAVE_CLOCK_GETTIME)
79 struct timespec ts;
81 #ifdef CLOCK_MONOTONIC
82 /* No locking or atomic ops for no_monotonic here */
83 static pa_bool_t no_monotonic = FALSE;
85 if (!no_monotonic)
86 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
87 no_monotonic = TRUE;
89 if (no_monotonic)
90 #endif /* CLOCK_MONOTONIC */
91 pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
93 pa_assert(tv);
95 tv->tv_sec = ts.tv_sec;
96 tv->tv_usec = ts.tv_nsec / PA_NSEC_PER_USEC;
98 return tv;
99 #elif defined(OS_IS_WIN32)
100 if (counter_freq > 0) {
101 LARGE_INTEGER count;
103 pa_assert_se(QueryPerformanceCounter(&count));
105 tv->tv_sec = count.QuadPart / counter_freq;
106 tv->tv_usec = (count.QuadPart % counter_freq) * PA_USEC_PER_SEC / counter_freq;
108 return tv;
110 #endif /* HAVE_CLOCK_GETTIME */
112 return pa_gettimeofday(tv);
115 pa_bool_t pa_rtclock_hrtimer(void) {
117 #if defined (OS_IS_DARWIN)
118 mach_timebase_info_data_t tbi;
119 uint64_t time_nsec;
121 mach_timebase_info(&tbi);
123 /* nsec = nticks * (N/D) - we want 1 tick == resolution !? */
124 time_nsec = tbi.numer / tbi.denom;
125 return time_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
127 #elif defined(HAVE_CLOCK_GETTIME)
128 struct timespec ts;
130 #ifdef CLOCK_MONOTONIC
132 if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
133 return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
135 #endif /* CLOCK_MONOTONIC */
137 pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
138 return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
140 #elif defined(OS_IS_WIN32)
142 if (counter_freq > 0)
143 return counter_freq >= (int64_t) (PA_USEC_PER_SEC/PA_HRTIMER_THRESHOLD_USEC);
145 #endif /* HAVE_CLOCK_GETTIME */
147 return FALSE;
150 #define TIMER_SLACK_NS (int) ((500 * PA_NSEC_PER_USEC))
152 void pa_rtclock_hrtimer_enable(void) {
154 #ifdef PR_SET_TIMERSLACK
155 int slack_ns;
157 if ((slack_ns = prctl(PR_GET_TIMERSLACK, 0, 0, 0, 0)) < 0) {
158 pa_log_info("PR_GET_TIMERSLACK/PR_SET_TIMERSLACK not supported.");
159 return;
162 pa_log_debug("Timer slack is set to %i us.", (int) (slack_ns/PA_NSEC_PER_USEC));
164 if (slack_ns > TIMER_SLACK_NS) {
165 slack_ns = TIMER_SLACK_NS;
167 pa_log_debug("Setting timer slack to %i us.", (int) (slack_ns/PA_NSEC_PER_USEC));
169 if (prctl(PR_SET_TIMERSLACK, slack_ns, 0, 0, 0) < 0) {
170 pa_log_warn("PR_SET_TIMERSLACK failed: %s", pa_cstrerror(errno));
171 return;
175 #elif defined(OS_IS_WIN32)
176 LARGE_INTEGER freq;
178 pa_assert_se(QueryPerformanceFrequency(&freq));
179 counter_freq = freq.QuadPart;
181 #endif
184 struct timeval* pa_rtclock_from_wallclock(struct timeval *tv) {
186 #ifdef HAVE_CLOCK_GETTIME
187 struct timeval wc_now, rt_now;
189 pa_gettimeofday(&wc_now);
190 pa_rtclock_get(&rt_now);
192 pa_assert(tv);
194 /* pa_timeval_sub() saturates on underflow! */
196 if (pa_timeval_cmp(&wc_now, tv) < 0)
197 pa_timeval_add(&rt_now, pa_timeval_diff(tv, &wc_now));
198 else
199 pa_timeval_sub(&rt_now, pa_timeval_diff(&wc_now, tv));
201 *tv = rt_now;
202 #endif
204 return tv;
207 #ifdef HAVE_CLOCK_GETTIME
208 pa_usec_t pa_timespec_load(const struct timespec *ts) {
210 if (PA_UNLIKELY(!ts))
211 return PA_USEC_INVALID;
213 return
214 (pa_usec_t) ts->tv_sec * PA_USEC_PER_SEC +
215 (pa_usec_t) ts->tv_nsec / PA_NSEC_PER_USEC;
218 struct timespec* pa_timespec_store(struct timespec *ts, pa_usec_t v) {
219 pa_assert(ts);
221 if (PA_UNLIKELY(v == PA_USEC_INVALID)) {
222 ts->tv_sec = PA_INT_TYPE_MAX(time_t);
223 ts->tv_nsec = (long) (PA_NSEC_PER_SEC-1);
224 return NULL;
227 ts->tv_sec = (time_t) (v / PA_USEC_PER_SEC);
228 ts->tv_nsec = (long) ((v % PA_USEC_PER_SEC) * PA_NSEC_PER_USEC);
230 return ts;
232 #endif
234 static struct timeval* wallclock_from_rtclock(struct timeval *tv) {
236 #ifdef HAVE_CLOCK_GETTIME
237 struct timeval wc_now, rt_now;
239 pa_gettimeofday(&wc_now);
240 pa_rtclock_get(&rt_now);
242 pa_assert(tv);
244 /* pa_timeval_sub() saturates on underflow! */
246 if (pa_timeval_cmp(&rt_now, tv) < 0)
247 pa_timeval_add(&wc_now, pa_timeval_diff(tv, &rt_now));
248 else
249 pa_timeval_sub(&wc_now, pa_timeval_diff(&rt_now, tv));
251 *tv = wc_now;
252 #endif
254 return tv;
257 struct timeval* pa_timeval_rtstore(struct timeval *tv, pa_usec_t v, pa_bool_t rtclock) {
258 pa_assert(tv);
260 if (v == PA_USEC_INVALID)
261 return NULL;
263 pa_timeval_store(tv, v);
265 if (rtclock)
266 tv->tv_usec |= PA_TIMEVAL_RTCLOCK;
267 else
268 wallclock_from_rtclock(tv);
270 return tv;