]> code.delx.au - pulseaudio/blob - src/pulsecore/rtclock.c
Merge commit 'elmarco/legacy-dir'
[pulseaudio] / src / pulsecore / 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 <sys/prctl.h>
31 #include <errno.h>
32
33 #include <pulse/timeval.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-error.h>
36
37 #include "rtclock.h"
38
39 pa_usec_t pa_rtclock_age(const struct timeval *tv) {
40 struct timeval now;
41 pa_assert(tv);
42
43 return pa_timeval_diff(pa_rtclock_get(&now), tv);
44 }
45
46 struct timeval *pa_rtclock_get(struct timeval *tv) {
47 #ifdef HAVE_CLOCK_GETTIME
48 struct timespec ts;
49
50 #ifdef CLOCK_MONOTONIC
51 /* No locking or atomic ops for no_monotonic here */
52 static pa_bool_t no_monotonic = FALSE;
53
54 if (!no_monotonic)
55 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
56 no_monotonic = TRUE;
57
58 if (no_monotonic)
59 #endif
60 pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
61
62 pa_assert(tv);
63
64 tv->tv_sec = ts.tv_sec;
65 tv->tv_usec = ts.tv_nsec / 1000;
66
67 return tv;
68
69 #else /* HAVE_CLOCK_GETTIME */
70
71 return pa_gettimeofday(tv);
72
73 #endif
74 }
75
76 pa_bool_t pa_rtclock_hrtimer(void) {
77 #ifdef HAVE_CLOCK_GETTIME
78 struct timespec ts;
79
80 #ifdef CLOCK_MONOTONIC
81 if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
82 return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
83 #endif
84
85 pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
86 return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
87
88 #else /* HAVE_CLOCK_GETTIME */
89
90 return FALSE;
91
92 #endif
93 }
94
95 void pa_rtclock_hrtimer_enable(void) {
96 #ifdef PR_SET_TIMERSLACK
97 int slack_ns;
98
99 if ((slack_ns = prctl(PR_GET_TIMERSLACK, 0, 0, 0, 0)) < 0) {
100 pa_log_info("PR_GET_TIMERSLACK/PR_SET_TIMERSLACK not supported.");
101 return;
102 }
103
104 pa_log_debug("Timer slack set to %i us.", slack_ns/1000);
105
106 slack_ns = 500000000;
107
108 pa_log_debug("Setting timer slack to %i us.", slack_ns/1000);
109
110 if (prctl(PR_SET_TIMERSLACK, slack_ns, 0, 0, 0) < 0) {
111 pa_log_warn("PR_SET_TIMERSLACK failed: %s", pa_cstrerror(errno));
112 return;
113 }
114
115 #endif
116 }
117
118 pa_usec_t pa_rtclock_usec(void) {
119 struct timeval tv;
120
121 return pa_timeval_load(pa_rtclock_get(&tv));
122 }
123
124 struct timeval* pa_rtclock_from_wallclock(struct timeval *tv) {
125
126 #ifdef HAVE_CLOCK_GETTIME
127 struct timeval wc_now, rt_now;
128
129 pa_gettimeofday(&wc_now);
130 pa_rtclock_get(&rt_now);
131
132 pa_assert(tv);
133
134 if (pa_timeval_cmp(&wc_now, tv) < 0)
135 pa_timeval_add(&rt_now, pa_timeval_diff(tv, &wc_now));
136 else
137 pa_timeval_sub(&rt_now, pa_timeval_diff(&wc_now, tv));
138
139 *tv = rt_now;
140 #endif
141
142 return tv;
143 }
144
145 pa_usec_t pa_timespec_load(const struct timespec *ts) {
146 pa_assert(ts);
147
148 return
149 (pa_usec_t) ts->tv_sec * PA_USEC_PER_SEC +
150 (pa_usec_t) ts->tv_nsec / PA_NSEC_PER_USEC;
151 }