From 402b2ed2f093bdf8d22e691a6d5b893623decece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= Date: Wed, 24 May 2006 19:35:31 +0000 Subject: [PATCH] =?UTF-8?q?Use=20the=20monotonic=20clock=20from=20POSIX=20?= =?UTF-8?q?real=20time=20extension=20when=20available.=20That=20one=20goes?= =?UTF-8?q?=20at=20the=20same=20=E2=80=9Cspeed=E2=80=9D=20as=20the=20real?= =?UTF-8?q?=20time=20clock,=20but=20it=20is=20warp-less.=20Now=20it=20shou?= =?UTF-8?q?ld=20be=20safe=20to=20call=20ntpdate=20while=20performing.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configure.ac | 9 +++++++-- src/misc/mtime.c | 50 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index 9d834d06af..465099a70e 100644 --- a/configure.ac +++ b/configure.ac @@ -718,8 +718,13 @@ VLC_ADD_LDFLAGS([vlc plugin],[${THREAD_LIB}]) dnl Don't link with rt when using GNU-pth if test "${THREAD_LIB}" != "-lpth" && test "${THREAD_LIB}" != "-lst"; then - dnl HP/UX port - AC_CHECK_LIB(rt,sem_init, [VLC_ADD_LDFLAGS([vlc],[-lrt])]) + AC_CHECK_LIB(rt, clock_gettime, [ + VLC_ADD_LDFLAGS([vlc],[-lrt]) + AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [Define to 1 if you have clock_gettime.]) + ], [ + dnl HP/UX port + AC_CHECK_LIB(rt,sem_init, [VLC_ADD_LDFLAGS([vlc],[-lrt])]) + ]) have_nanosleep=false AC_CHECK_FUNCS(nanosleep,have_nanosleep=:,[ diff --git a/src/misc/mtime.c b/src/misc/mtime.c index b0334ebf80..ada992a2b5 100644 --- a/src/misc/mtime.c +++ b/src/misc/mtime.c @@ -6,6 +6,7 @@ * $Id$ * * Authors: Vincent Seguin + * Rémi Denis-Courmont * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,15 +23,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ -/* - * TODO: - * see if using Linux real-time extensions is possible and profitable - */ - /***************************************************************************** * Preamble *****************************************************************************/ #include /* sprintf() */ +#include /* clock_gettime(), clock_nanosleep() */ +#include /* ldiv() */ #include @@ -106,6 +104,7 @@ char *secstotimestr( char *psz_buffer, int i_seconds ) return( psz_buffer ); } + /** * Return high precision date * @@ -188,15 +187,23 @@ mtime_t mdate( void ) return usec_time; } +#elif defined (HAVE_CLOCK_GETTIME) + struct timespec ts; + +# ifdef _POSIX_MONOTONIC_CLOCK + /* Try to use POSIX monotonic clock if available */ + if( clock_gettime( CLOCK_MONOTONIC, &ts ) ) +# endif + /* Run-time fallback to real-time clock (always available) */ + (void)clock_gettime( CLOCK_REALTIME, &ts ); + + return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000); #else struct timeval tv_date; - /* gettimeofday() could return an error, and should be tested. However, the - * only possible error, according to 'man', is EFAULT, which can not happen - * here, since tv is a local variable. */ - gettimeofday( &tv_date, NULL ); + /* gettimeofday() cannot fail given &tv_date is a valid address */ + (void)gettimeofday( &tv_date, NULL ); return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec ); - #endif } @@ -231,6 +238,29 @@ void mwait( mtime_t date ) } msleep( delay ); +#elif defined (HAVE_CLOCK_GETTIME) + struct timespec ts; + ldiv_t d; + +# if 1 + /* + * Ideally, we'd use absolute time (TIMER_ABSTIME), instead of + * computing the time difference... but VLC mtime_t type seems to + * overflow way too quickly for this to work properly, or maybe it's a + * signedness problem (??). + */ + date -= mdate(); + if( date <= 0 ) + return; +# endif + d = ldiv( date, 1000000 ); + ts.tv_sec = d.quot; + ts.tv_nsec = d.rem * 1000; + +# ifdef _POSIX_MONOTONIC_CLOCK + if( clock_nanosleep( CLOCK_MONOTONIC, 0 /*TIMER_ABSTIME*/, &ts, NULL ) ) +# endif + (void)clock_nanosleep( CLOCK_REALTIME, 0, &ts, NULL ); #else struct timeval tv_date; -- GitLab