]> code.delx.au - pulseaudio/blob - src/pulsecore/rtclock.c
don't allow --start in system mode
[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
31 #include <pulse/timeval.h>
32 #include <pulsecore/macro.h>
33
34 #include "rtclock.h"
35
36 pa_usec_t pa_rtclock_age(const struct timeval *tv) {
37 struct timeval now;
38 pa_assert(tv);
39
40 return pa_timeval_diff(pa_rtclock_get(&now), tv);
41 }
42
43 struct timeval *pa_rtclock_get(struct timeval *tv) {
44 #ifdef HAVE_CLOCK_GETTIME
45 struct timespec ts;
46
47 #ifdef CLOCK_MONOTONIC
48 /* No locking or atomic ops for no_monotonic here */
49 static pa_bool_t no_monotonic = FALSE;
50
51 if (!no_monotonic)
52 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
53 no_monotonic = TRUE;
54
55 if (no_monotonic)
56 #endif
57 pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
58
59 pa_assert(tv);
60
61 tv->tv_sec = ts.tv_sec;
62 tv->tv_usec = ts.tv_nsec / 1000;
63
64 return tv;
65
66 #else /* HAVE_CLOCK_GETTIME */
67
68 return pa_gettimeofday(tv);
69
70 #endif
71 }
72
73 pa_bool_t pa_rtclock_hrtimer(void) {
74 #ifdef HAVE_CLOCK_GETTIME
75 struct timespec ts;
76
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;
80 #endif
81
82 pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
83 return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
84
85 #else /* HAVE_CLOCK_GETTIME */
86
87 return FALSE;
88
89 #endif
90 }
91
92 pa_usec_t pa_rtclock_usec(void) {
93 struct timeval tv;
94
95 return pa_timeval_load(pa_rtclock_get(&tv));
96 }
97
98 struct timeval* pa_rtclock_from_wallclock(struct timeval *tv) {
99
100 #ifdef HAVE_CLOCK_GETTIME
101 struct timeval wc_now, rt_now;
102
103 pa_gettimeofday(&wc_now);
104 pa_rtclock_get(&rt_now);
105
106 pa_assert(tv);
107
108 if (pa_timeval_cmp(&wc_now, tv) < 0)
109 pa_timeval_add(&rt_now, pa_timeval_diff(tv, &wc_now));
110 else
111 pa_timeval_sub(&rt_now, pa_timeval_diff(&wc_now, tv));
112
113 *tv = rt_now;
114 #endif
115
116 return tv;
117 }