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