2006-05-18 06:45:43 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2006-05-18 06:45:43 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2006-05-18 06:45:43 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-18 06:45:43 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2014-11-26 14:14:51 +01:00
|
|
|
License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
|
2006-05-18 06:45:43 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2009-09-18 04:10:26 +02:00
|
|
|
#include <pulsecore/core-util.h>
|
2006-05-18 06:45:43 +00:00
|
|
|
|
|
|
|
|
#include "timeval.h"
|
|
|
|
|
|
|
|
|
|
struct timeval *pa_gettimeofday(struct timeval *tv) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(tv);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2011-01-18 11:26:57 +01:00
|
|
|
#if defined(OS_IS_WIN32)
|
2006-05-18 06:45:43 +00:00
|
|
|
/*
|
|
|
|
|
* Copied from implementation by Steven Edwards (LGPL).
|
|
|
|
|
* Found on wine mailing list.
|
|
|
|
|
*/
|
|
|
|
|
#if defined(_MSC_VER) || defined(__BORLANDC__)
|
|
|
|
|
#define EPOCHFILETIME (116444736000000000i64)
|
|
|
|
|
#else
|
|
|
|
|
#define EPOCHFILETIME (116444736000000000LL)
|
|
|
|
|
#endif
|
2011-06-23 22:21:03 +02:00
|
|
|
{
|
2009-09-18 04:10:26 +02:00
|
|
|
FILETIME ft;
|
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
|
int64_t t;
|
2006-05-18 06:45:43 +00:00
|
|
|
|
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
|
li.LowPart = ft.dwLowDateTime;
|
|
|
|
|
li.HighPart = ft.dwHighDateTime;
|
|
|
|
|
t = li.QuadPart; /* In 100-nanosecond intervals */
|
|
|
|
|
t -= EPOCHFILETIME; /* Offset to the Epoch time */
|
|
|
|
|
t /= 10; /* In microseconds */
|
2007-10-28 19:13:50 +00:00
|
|
|
tv->tv_sec = (time_t) (t / PA_USEC_PER_SEC);
|
|
|
|
|
tv->tv_usec = (suseconds_t) (t % PA_USEC_PER_SEC);
|
2011-06-23 22:21:03 +02:00
|
|
|
}
|
2011-01-18 11:26:57 +01:00
|
|
|
#elif defined(HAVE_GETTIMEOFDAY)
|
|
|
|
|
pa_assert_se(gettimeofday(tv, NULL) == 0);
|
2006-05-18 06:45:43 +00:00
|
|
|
#else
|
|
|
|
|
#error "Platform lacks gettimeofday() or equivalent function."
|
|
|
|
|
#endif
|
2011-01-18 11:26:57 +01:00
|
|
|
|
|
|
|
|
return tv;
|
2006-05-18 06:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
|
|
|
|
|
pa_usec_t r;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
2011-08-24 18:24:46 +02:00
|
|
|
/* Check which is the earlier time and swap the two arguments if required. */
|
2009-09-18 04:11:02 +02:00
|
|
|
if (PA_UNLIKELY(pa_timeval_cmp(a, b) < 0)) {
|
2006-05-18 06:45:43 +00:00
|
|
|
const struct timeval *c;
|
|
|
|
|
c = a;
|
|
|
|
|
a = b;
|
|
|
|
|
b = c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate the second difference*/
|
2008-08-19 22:39:54 +02:00
|
|
|
r = ((pa_usec_t) a->tv_sec - (pa_usec_t) b->tv_sec) * PA_USEC_PER_SEC;
|
2006-05-18 06:45:43 +00:00
|
|
|
|
|
|
|
|
/* Calculate the microsecond difference */
|
|
|
|
|
if (a->tv_usec > b->tv_usec)
|
2009-09-18 04:11:02 +02:00
|
|
|
r += (pa_usec_t) a->tv_usec - (pa_usec_t) b->tv_usec;
|
2006-05-18 06:45:43 +00:00
|
|
|
else if (a->tv_usec < b->tv_usec)
|
2009-09-18 04:11:02 +02:00
|
|
|
r -= (pa_usec_t) b->tv_usec - (pa_usec_t) a->tv_usec;
|
2006-05-18 06:45:43 +00:00
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2006-05-18 06:45:43 +00:00
|
|
|
|
|
|
|
|
if (a->tv_sec < b->tv_sec)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (a->tv_sec > b->tv_sec)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
if (a->tv_usec < b->tv_usec)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (a->tv_usec > b->tv_usec)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_usec_t pa_timeval_age(const struct timeval *tv) {
|
|
|
|
|
struct timeval now;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(tv);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-18 06:45:43 +00:00
|
|
|
return pa_timeval_diff(pa_gettimeofday(&now), tv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct timeval* pa_timeval_add(struct timeval *tv, pa_usec_t v) {
|
2009-09-18 04:10:26 +02:00
|
|
|
time_t secs;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(tv);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-09-18 04:10:26 +02:00
|
|
|
secs = (time_t) (v/PA_USEC_PER_SEC);
|
2006-05-18 06:45:43 +00:00
|
|
|
|
2009-09-18 04:10:26 +02:00
|
|
|
if (PA_UNLIKELY(tv->tv_sec > PA_INT_TYPE_MAX(time_t) - secs))
|
|
|
|
|
goto overflow;
|
|
|
|
|
|
|
|
|
|
tv->tv_sec += secs;
|
|
|
|
|
v -= (pa_usec_t) secs * PA_USEC_PER_SEC;
|
2007-10-28 19:13:50 +00:00
|
|
|
tv->tv_usec += (suseconds_t) v;
|
2006-05-18 06:45:43 +00:00
|
|
|
|
|
|
|
|
/* Normalize */
|
2009-09-18 04:10:26 +02:00
|
|
|
while ((pa_usec_t) tv->tv_usec >= PA_USEC_PER_SEC) {
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(tv->tv_sec >= PA_INT_TYPE_MAX(time_t)))
|
|
|
|
|
goto overflow;
|
|
|
|
|
|
2006-05-18 06:45:43 +00:00
|
|
|
tv->tv_sec++;
|
2008-08-19 22:39:54 +02:00
|
|
|
tv->tv_usec -= (suseconds_t) PA_USEC_PER_SEC;
|
2006-05-18 06:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tv;
|
2009-09-18 04:10:26 +02:00
|
|
|
|
|
|
|
|
overflow:
|
|
|
|
|
tv->tv_sec = PA_INT_TYPE_MAX(time_t);
|
|
|
|
|
tv->tv_usec = (suseconds_t) (PA_USEC_PER_SEC-1);
|
|
|
|
|
return tv;
|
2006-05-18 06:45:43 +00:00
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
struct timeval* pa_timeval_sub(struct timeval *tv, pa_usec_t v) {
|
2009-09-18 04:10:26 +02:00
|
|
|
time_t secs;
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert(tv);
|
|
|
|
|
|
2009-09-18 04:10:26 +02:00
|
|
|
secs = (time_t) (v/PA_USEC_PER_SEC);
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(tv->tv_sec < secs))
|
|
|
|
|
goto underflow;
|
|
|
|
|
|
|
|
|
|
tv->tv_sec -= secs;
|
|
|
|
|
v -= (pa_usec_t) secs * PA_USEC_PER_SEC;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
if (tv->tv_usec >= (suseconds_t) v)
|
|
|
|
|
tv->tv_usec -= (suseconds_t) v;
|
|
|
|
|
else {
|
2009-09-18 04:10:26 +02:00
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(tv->tv_sec <= 0))
|
|
|
|
|
goto underflow;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
tv->tv_sec --;
|
2008-08-19 22:39:54 +02:00
|
|
|
tv->tv_usec += (suseconds_t) (PA_USEC_PER_SEC - v);
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tv;
|
2009-09-18 04:10:26 +02:00
|
|
|
|
|
|
|
|
underflow:
|
|
|
|
|
tv->tv_sec = 0;
|
|
|
|
|
tv->tv_usec = 0;
|
|
|
|
|
return tv;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
struct timeval* pa_timeval_store(struct timeval *tv, pa_usec_t v) {
|
|
|
|
|
pa_assert(tv);
|
|
|
|
|
|
2009-09-18 04:11:59 +02:00
|
|
|
if (PA_UNLIKELY(v == PA_USEC_INVALID)) {
|
|
|
|
|
tv->tv_sec = PA_INT_TYPE_MAX(time_t);
|
|
|
|
|
tv->tv_usec = (suseconds_t) (PA_USEC_PER_SEC-1);
|
|
|
|
|
|
|
|
|
|
return tv;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
tv->tv_sec = (time_t) (v / PA_USEC_PER_SEC);
|
|
|
|
|
tv->tv_usec = (suseconds_t) (v % PA_USEC_PER_SEC);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
return tv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_usec_t pa_timeval_load(const struct timeval *tv) {
|
2009-09-18 04:11:59 +02:00
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(!tv))
|
|
|
|
|
return PA_USEC_INVALID;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
(pa_usec_t) tv->tv_sec * PA_USEC_PER_SEC +
|
|
|
|
|
(pa_usec_t) tv->tv_usec;
|
|
|
|
|
}
|