]> code.delx.au - pulseaudio/blob - src/pulsecore/rtclock.c
add monotonic clock abstraction pa_rtclock
[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 <sys/time.h>
31
32 #include <pulse/timeval.h>
33 #include <pulsecore/macro.h>
34
35 #include "rtclock.h"
36
37 struct timespec *pa_timespec_store(struct timespec *a, pa_usec_t u) {
38 pa_assert(a);
39
40 a->tv_sec = u / PA_USEC_PER_SEC;
41
42 u -= (pa_usec_t) a->tv_sec * PA_USEC_PER_SEC;
43
44 a->tv_nsec = u * 1000;
45
46 return a;
47 }
48
49 pa_usec_t pa_timespec_load(struct timespec *ts) {
50 pa_assert(ts);
51
52 return (pa_usec_t) ts->tv_sec * PA_USEC_PER_SEC + (pa_usec_t) (ts->tv_nsec / 1000);
53 }
54
55 pa_usec_t pa_timespec_diff(const struct timespec *a, const struct timespec *b) {
56 pa_usec_t r;
57
58 pa_assert(a);
59 pa_assert(b);
60
61 /* Check which whan is the earlier time and swap the two arguments if required. */
62 if (pa_timespec_cmp(a, b) < 0) {
63 const struct timespec *c;
64 c = a;
65 a = b;
66 b = c;
67 }
68
69 /* Calculate the second difference*/
70 r = ((pa_usec_t) a->tv_sec - b->tv_sec) * PA_USEC_PER_SEC;
71
72 /* Calculate the microsecond difference */
73 if (a->tv_nsec > b->tv_nsec)
74 r += (pa_usec_t) ((a->tv_nsec - b->tv_nsec) / 1000);
75 else if (a->tv_nsec < b->tv_nsec)
76 r -= (pa_usec_t) ((b->tv_nsec - a->tv_nsec) / 1000);
77
78 return r;
79 }
80
81 int pa_timespec_cmp(const struct timespec *a, const struct timespec *b) {
82 pa_assert(a);
83 pa_assert(b);
84
85 if (a->tv_sec < b->tv_sec)
86 return -1;
87
88 if (a->tv_sec > b->tv_sec)
89 return 1;
90
91 if (a->tv_nsec < b->tv_nsec)
92 return -1;
93
94 if (a->tv_nsec > b->tv_nsec)
95 return 1;
96
97 return 0;
98 }
99
100 struct timespec* pa_timespec_add(struct timespec *ts, pa_usec_t v) {
101 unsigned long secs;
102 pa_assert(ts);
103
104 secs = (unsigned long) (v/PA_USEC_PER_SEC);
105 ts->tv_sec += secs;
106 v -= ((pa_usec_t) secs) * PA_USEC_PER_SEC;
107
108 ts->tv_nsec += (long) (v*1000);
109
110 /* Normalize */
111 while (ts->tv_nsec >= PA_NSEC_PER_SEC) {
112 ts->tv_sec++;
113 ts->tv_nsec -= PA_NSEC_PER_SEC;
114 }
115
116 return ts;
117 }
118
119 pa_usec_t pa_rtclock_age(const struct timespec *ts) {
120 struct timespec now;
121 pa_assert(ts);
122
123 return pa_timespec_diff(pa_rtclock_get(&now), ts);
124 }
125
126 struct timespec *pa_rtclock_get(struct timespec *ts) {
127 static int no_monotonic = 0;
128
129 /* No locking or atomic ops for no_monotonic here */
130
131 pa_assert(ts);
132
133 if (!no_monotonic) {
134 if (clock_gettime(CLOCK_MONOTONIC, ts) >= 0)
135 return ts;
136
137 no_monotonic = 1;
138 }
139
140 pa_assert_se(clock_gettime(CLOCK_REALTIME, ts) == 0);
141 return ts;
142 }
143
144 int pa_rtclock_hrtimer(void) {
145 struct timespec ts;
146
147 if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
148 return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
149
150 pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
151 return ts.tv_sec == 0 && ts.tv_nsec <= PA_HRTIMER_THRESHOLD_USEC*1000;
152 }
153