]>
code.delx.au - pulseaudio/blob - src/pulse/timeval.c
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-util.h>
39 struct timeval
*pa_gettimeofday(struct timeval
*tv
) {
42 #if defined(OS_IS_WIN32)
44 * Copied from implementation by Steven Edwards (LGPL).
45 * Found on wine mailing list.
47 #if defined(_MSC_VER) || defined(__BORLANDC__)
48 #define EPOCHFILETIME (116444736000000000i64)
50 #define EPOCHFILETIME (116444736000000000LL)
57 GetSystemTimeAsFileTime(&ft
);
58 li
.LowPart
= ft
.dwLowDateTime
;
59 li
.HighPart
= ft
.dwHighDateTime
;
60 t
= li
.QuadPart
; /* In 100-nanosecond intervals */
61 t
-= EPOCHFILETIME
; /* Offset to the Epoch time */
62 t
/= 10; /* In microseconds */
63 tv
->tv_sec
= (time_t) (t
/ PA_USEC_PER_SEC
);
64 tv
->tv_usec
= (suseconds_t
) (t
% PA_USEC_PER_SEC
);
66 #elif defined(HAVE_GETTIMEOFDAY)
67 pa_assert_se(gettimeofday(tv
, NULL
) == 0);
69 #error "Platform lacks gettimeofday() or equivalent function."
75 pa_usec_t
pa_timeval_diff(const struct timeval
*a
, const struct timeval
*b
) {
81 /* Check which is the earlier time and swap the two arguments if required. */
82 if (PA_UNLIKELY(pa_timeval_cmp(a
, b
) < 0)) {
83 const struct timeval
*c
;
89 /* Calculate the second difference*/
90 r
= ((pa_usec_t
) a
->tv_sec
- (pa_usec_t
) b
->tv_sec
) * PA_USEC_PER_SEC
;
92 /* Calculate the microsecond difference */
93 if (a
->tv_usec
> b
->tv_usec
)
94 r
+= (pa_usec_t
) a
->tv_usec
- (pa_usec_t
) b
->tv_usec
;
95 else if (a
->tv_usec
< b
->tv_usec
)
96 r
-= (pa_usec_t
) b
->tv_usec
- (pa_usec_t
) a
->tv_usec
;
101 int pa_timeval_cmp(const struct timeval
*a
, const struct timeval
*b
) {
105 if (a
->tv_sec
< b
->tv_sec
)
108 if (a
->tv_sec
> b
->tv_sec
)
111 if (a
->tv_usec
< b
->tv_usec
)
114 if (a
->tv_usec
> b
->tv_usec
)
120 pa_usec_t
pa_timeval_age(const struct timeval
*tv
) {
124 return pa_timeval_diff(pa_gettimeofday(&now
), tv
);
127 struct timeval
* pa_timeval_add(struct timeval
*tv
, pa_usec_t v
) {
131 secs
= (time_t) (v
/PA_USEC_PER_SEC
);
133 if (PA_UNLIKELY(tv
->tv_sec
> PA_INT_TYPE_MAX(time_t) - secs
))
137 v
-= (pa_usec_t
) secs
* PA_USEC_PER_SEC
;
138 tv
->tv_usec
+= (suseconds_t
) v
;
141 while ((pa_usec_t
) tv
->tv_usec
>= PA_USEC_PER_SEC
) {
143 if (PA_UNLIKELY(tv
->tv_sec
>= PA_INT_TYPE_MAX(time_t)))
147 tv
->tv_usec
-= (suseconds_t
) PA_USEC_PER_SEC
;
153 tv
->tv_sec
= PA_INT_TYPE_MAX(time_t);
154 tv
->tv_usec
= (suseconds_t
) (PA_USEC_PER_SEC
-1);
158 struct timeval
* pa_timeval_sub(struct timeval
*tv
, pa_usec_t v
) {
162 secs
= (time_t) (v
/PA_USEC_PER_SEC
);
164 if (PA_UNLIKELY(tv
->tv_sec
< secs
))
168 v
-= (pa_usec_t
) secs
* PA_USEC_PER_SEC
;
170 if (tv
->tv_usec
>= (suseconds_t
) v
)
171 tv
->tv_usec
-= (suseconds_t
) v
;
174 if (PA_UNLIKELY(tv
->tv_sec
<= 0))
178 tv
->tv_usec
+= (suseconds_t
) (PA_USEC_PER_SEC
- v
);
189 struct timeval
* pa_timeval_store(struct timeval
*tv
, pa_usec_t v
) {
192 if (PA_UNLIKELY(v
== PA_USEC_INVALID
)) {
193 tv
->tv_sec
= PA_INT_TYPE_MAX(time_t);
194 tv
->tv_usec
= (suseconds_t
) (PA_USEC_PER_SEC
-1);
199 tv
->tv_sec
= (time_t) (v
/ PA_USEC_PER_SEC
);
200 tv
->tv_usec
= (suseconds_t
) (v
% PA_USEC_PER_SEC
);
205 pa_usec_t
pa_timeval_load(const struct timeval
*tv
) {
207 if (PA_UNLIKELY(!tv
))
208 return PA_USEC_INVALID
;
211 (pa_usec_t
) tv
->tv_sec
* PA_USEC_PER_SEC
+
212 (pa_usec_t
) tv
->tv_usec
;