]> code.delx.au - pulseaudio/blob - src/pulsecore/core-rtclock.c
Merge branch 'master' into dbus-work
[pulseaudio] / src / pulsecore / core-rtclock.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
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.
11
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.
16
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 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stddef.h>
28 #include <time.h>
29 #include <sys/time.h>
30 #include <errno.h>
31
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
34 #endif
35
36 #include <pulse/timeval.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/core-error.h>
39
40 #include "core-rtclock.h"
41
42 pa_usec_t pa_rtclock_age(const struct timeval *tv) {
43 struct timeval now;
44 pa_assert(tv);
45
46 return pa_timeval_diff(pa_rtclock_get(&now), tv);
47 }
48
49 struct timeval *pa_rtclock_get(struct timeval *tv) {
50 #ifdef HAVE_CLOCK_GETTIME
51 struct timespec ts;
52
53 #ifdef CLOCK_MONOTONIC
54 /* No locking or atomic ops for no_monotonic here */
55 static pa_bool_t no_monotonic = FALSE;
56
57 if (!no_monotonic)
58 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
59 no_monotonic = TRUE;
60
61 if (no_monotonic)
62 #endif
63 pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
64
65 pa_assert(tv);
66
67 tv->tv_sec = ts.tv_sec;
68 tv->tv_usec = ts.tv_nsec / PA_NSEC_PER_USEC;
69
70 return tv;
71
72 #else /* HAVE_CLOCK_GETTIME */
73
74 return pa_gettimeofday(tv);
75
76 #endif
77 }
78
79 pa_bool_t pa_rtclock_hrtimer(void) {
80 #ifdef HAVE_CLOCK_GETTIME
81 struct timespec ts;
82
83 #ifdef CLOCK_MONOTONIC
84 if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
85 return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
86 #endif
87
88 pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
89 return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
90
91 #else /* HAVE_CLOCK_GETTIME */
92
93 return FALSE;
94
95 #endif
96 }
97
98 #define TIMER_SLACK_NS (int) ((500 * PA_NSEC_PER_USEC))
99
100 void pa_rtclock_hrtimer_enable(void) {
101 #ifdef PR_SET_TIMERSLACK
102 int slack_ns;
103
104 if ((slack_ns = prctl(PR_GET_TIMERSLACK, 0, 0, 0, 0)) < 0) {
105 pa_log_info("PR_GET_TIMERSLACK/PR_SET_TIMERSLACK not supported.");
106 return;
107 }
108
109 pa_log_debug("Timer slack is set to %i us.", (int) (slack_ns/PA_NSEC_PER_USEC));
110
111 if (slack_ns > TIMER_SLACK_NS) {
112 slack_ns = TIMER_SLACK_NS;
113
114 pa_log_debug("Setting timer slack to %i us.", (int) (slack_ns/PA_NSEC_PER_USEC));
115
116 if (prctl(PR_SET_TIMERSLACK, slack_ns, 0, 0, 0) < 0) {
117 pa_log_warn("PR_SET_TIMERSLACK failed: %s", pa_cstrerror(errno));
118 return;
119 }
120 }
121
122 #endif
123 }
124
125 struct timeval* pa_rtclock_from_wallclock(struct timeval *tv) {
126
127 #ifdef HAVE_CLOCK_GETTIME
128 struct timeval wc_now, rt_now;
129
130 pa_gettimeofday(&wc_now);
131 pa_rtclock_get(&rt_now);
132
133 pa_assert(tv);
134
135 /* pa_timeval_sub() saturates on underflow! */
136
137 if (pa_timeval_cmp(&wc_now, tv) < 0)
138 pa_timeval_add(&rt_now, pa_timeval_diff(tv, &wc_now));
139 else
140 pa_timeval_sub(&rt_now, pa_timeval_diff(&wc_now, tv));
141
142 *tv = rt_now;
143 #endif
144
145 return tv;
146 }
147
148 pa_usec_t pa_timespec_load(const struct timespec *ts) {
149
150 if (PA_UNLIKELY(!ts))
151 return PA_USEC_INVALID;
152
153 return
154 (pa_usec_t) ts->tv_sec * PA_USEC_PER_SEC +
155 (pa_usec_t) ts->tv_nsec / PA_NSEC_PER_USEC;
156 }
157
158 struct timespec* pa_timespec_store(struct timespec *ts, pa_usec_t v) {
159 pa_assert(ts);
160
161 if (PA_UNLIKELY(v == PA_USEC_INVALID)) {
162 ts->tv_sec = PA_INT_TYPE_MAX(time_t);
163 ts->tv_nsec = (long) (PA_NSEC_PER_SEC-1);
164 return NULL;
165 }
166
167 ts->tv_sec = (time_t) (v / PA_USEC_PER_SEC);
168 ts->tv_nsec = (long) ((v % PA_USEC_PER_SEC) * PA_NSEC_PER_USEC);
169
170 return ts;
171 }
172
173 static struct timeval* wallclock_from_rtclock(struct timeval *tv) {
174
175 #ifdef HAVE_CLOCK_GETTIME
176 struct timeval wc_now, rt_now;
177
178 pa_gettimeofday(&wc_now);
179 pa_rtclock_get(&rt_now);
180
181 pa_assert(tv);
182
183 /* pa_timeval_sub() saturates on underflow! */
184
185 if (pa_timeval_cmp(&rt_now, tv) < 0)
186 pa_timeval_add(&wc_now, pa_timeval_diff(tv, &rt_now));
187 else
188 pa_timeval_sub(&wc_now, pa_timeval_diff(&rt_now, tv));
189
190 *tv = wc_now;
191 #endif
192
193 return tv;
194 }
195
196 struct timeval* pa_timeval_rtstore(struct timeval *tv, pa_usec_t v, pa_bool_t rtclock) {
197 pa_assert(tv);
198
199 if (v == PA_USEC_INVALID)
200 return NULL;
201
202 pa_timeval_store(tv, v);
203
204 if (rtclock)
205 tv->tv_usec |= PA_TIMEVAL_RTCLOCK;
206 else
207 wallclock_from_rtclock(tv);
208
209 return tv;
210 }