]> code.delx.au - pulseaudio/blob - src/pulsecore/rtclock.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / rtclock.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stddef.h>
30 #include <time.h>
31 #include <sys/time.h>
32
33 #include <pulse/timeval.h>
34 #include <pulsecore/macro.h>
35
36 #include "rtclock.h"
37
38 pa_usec_t pa_rtclock_age(const struct timeval *tv) {
39 struct timeval now;
40 pa_assert(tv);
41
42 return pa_timeval_diff(pa_rtclock_get(&now), tv);
43 }
44
45 struct timeval *pa_rtclock_get(struct timeval *tv) {
46 #ifdef HAVE_CLOCK_GETTIME
47 struct timespec ts;
48
49 #ifdef CLOCK_MONOTONIC
50 /* No locking or atomic ops for no_monotonic here */
51 static pa_bool_t no_monotonic = FALSE;
52
53 if (!no_monotonic)
54 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
55 no_monotonic = TRUE;
56
57 if (no_monotonic)
58 #endif
59 pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
60
61 pa_assert(tv);
62
63 tv->tv_sec = ts.tv_sec;
64 tv->tv_usec = ts.tv_nsec / 1000;
65
66 return tv;
67
68 #else /* HAVE_CLOCK_GETTIME */
69
70 return pa_gettimeofday(tv);
71
72 #endif
73 }
74
75 pa_bool_t pa_rtclock_hrtimer(void) {
76 #ifdef HAVE_CLOCK_GETTIME
77 struct timespec ts;
78
79 #ifdef CLOCK_MONOTONIC
80 if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
81 return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
82 #endif
83
84 pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
85 return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
86
87 #else /* HAVE_CLOCK_GETTIME */
88
89 return FALSE;
90
91 #endif
92 }
93
94 pa_usec_t pa_rtclock_usec(void) {
95 struct timeval tv;
96
97 return pa_timeval_load(pa_rtclock_get(&tv));
98 }