2009-03-01 13:13:25 +00:00
|
|
|
/*
|
2014-03-09 15:58:00 +00:00
|
|
|
* precise timer routines for Linux/UNIX
|
2009-03-01 13:13:25 +00:00
|
|
|
* copyright (C) LGB & A'rpi/ASTRAL
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2009-03-01 13:13:25 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2009-03-01 13:13:25 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-03-01 13:13:25 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2015-04-13 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-01 13:13:25 +00:00
|
|
|
*/
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2002-03-24 00:58:27 +00:00
|
|
|
#include <stdlib.h>
|
2001-10-25 23:34:14 +00:00
|
|
|
#include <time.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <sys/time.h>
|
2005-11-14 00:30:37 +00:00
|
|
|
#include "config.h"
|
2008-08-12 10:46:01 +00:00
|
|
|
#include "timer.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2014-03-09 15:58:00 +00:00
|
|
|
#if HAVE_NANOSLEEP
|
2013-05-17 17:54:37 +00:00
|
|
|
void mp_sleep_us(int64_t us)
|
2001-10-19 02:16:21 +00:00
|
|
|
{
|
2013-05-17 17:54:37 +00:00
|
|
|
if (us < 0)
|
|
|
|
return;
|
2001-10-19 02:16:21 +00:00
|
|
|
struct timespec ts;
|
2013-05-17 17:54:37 +00:00
|
|
|
ts.tv_sec = us / 1000000;
|
|
|
|
ts.tv_nsec = (us % 1000000) * 1000;
|
|
|
|
nanosleep(&ts, NULL);
|
2014-03-09 15:58:00 +00:00
|
|
|
}
|
2001-10-19 02:16:21 +00:00
|
|
|
#else
|
2014-03-09 15:58:00 +00:00
|
|
|
void mp_sleep_us(int64_t us)
|
|
|
|
{
|
|
|
|
if (us < 0)
|
|
|
|
return;
|
2013-05-17 17:54:37 +00:00
|
|
|
usleep(us);
|
2001-10-19 02:16:21 +00:00
|
|
|
}
|
2014-03-09 15:58:00 +00:00
|
|
|
#endif
|
2001-10-19 02:16:21 +00:00
|
|
|
|
timer: switch to CLOCK_MONOTONIC
Apparently, this is always _really_ monotonic, despite what the Linux
manpages say. So this should be much better than gettimeofday(). (At
times there were kernel bugs which broke the monotonic property.)
From the perspective of the player, time can still be discontinuous
(you could just stop the process with ^Z), but at least it's guaranteed
to be monotonic without further hacks required.
Also note that clock_gettime() returns the time in nanoseconds. We want
microseconds only, because that's the unit we chose internally. Another
problem is that nanoseconds can wrap pretty quickly (less than 300 years
in 63 bits), so it's just better to use microseconds. The devision won't
make the code that much slower (compilers can avoid a real division).
Note: this expects that the system provides clock_gettime() as well as
CLOCK_MONOTONIC. Both are optional according to POSIX. The only system
I know which doesn't have these, OSX, has seperate timer code anyway,
but I still don't know whether more obscure (yet supported) platforms
have a problem with this, so I'm playing safely. But this still expects
that CLOCK_MONOTONIC always works at runtime if it's defined.
2014-03-09 14:34:26 +00:00
|
|
|
#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)
|
|
|
|
uint64_t mp_raw_time_us(void)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts))
|
|
|
|
abort();
|
|
|
|
return ts.tv_sec * 1000000LL + ts.tv_nsec / 1000;
|
|
|
|
}
|
|
|
|
#else
|
2013-05-17 17:54:37 +00:00
|
|
|
uint64_t mp_raw_time_us(void)
|
2009-04-19 22:27:02 +00:00
|
|
|
{
|
2013-05-17 17:54:37 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
return tv.tv_sec * 1000000LL + tv.tv_usec;
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
timer: switch to CLOCK_MONOTONIC
Apparently, this is always _really_ monotonic, despite what the Linux
manpages say. So this should be much better than gettimeofday(). (At
times there were kernel bugs which broke the monotonic property.)
From the perspective of the player, time can still be discontinuous
(you could just stop the process with ^Z), but at least it's guaranteed
to be monotonic without further hacks required.
Also note that clock_gettime() returns the time in nanoseconds. We want
microseconds only, because that's the unit we chose internally. Another
problem is that nanoseconds can wrap pretty quickly (less than 300 years
in 63 bits), so it's just better to use microseconds. The devision won't
make the code that much slower (compilers can avoid a real division).
Note: this expects that the system provides clock_gettime() as well as
CLOCK_MONOTONIC. Both are optional according to POSIX. The only system
I know which doesn't have these, OSX, has seperate timer code anyway,
but I still don't know whether more obscure (yet supported) platforms
have a problem with this, so I'm playing safely. But this still expects
that CLOCK_MONOTONIC always works at runtime if it's defined.
2014-03-09 14:34:26 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2013-05-17 17:54:37 +00:00
|
|
|
void mp_raw_time_init(void)
|
2009-04-19 22:27:02 +00:00
|
|
|
{
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|