Commit 402b2ed2 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Use the monotonic clock from POSIX real time extension when available.

That one goes at the same “speed” as the real time clock, but it is
warp-less. Now it should be safe to call ntpdate while performing.
parent 99214227
...@@ -718,8 +718,13 @@ VLC_ADD_LDFLAGS([vlc plugin],[${THREAD_LIB}]) ...@@ -718,8 +718,13 @@ VLC_ADD_LDFLAGS([vlc plugin],[${THREAD_LIB}])
dnl Don't link with rt when using GNU-pth dnl Don't link with rt when using GNU-pth
if test "${THREAD_LIB}" != "-lpth" && test "${THREAD_LIB}" != "-lst"; then if test "${THREAD_LIB}" != "-lpth" && test "${THREAD_LIB}" != "-lst"; then
dnl HP/UX port AC_CHECK_LIB(rt, clock_gettime, [
AC_CHECK_LIB(rt,sem_init, [VLC_ADD_LDFLAGS([vlc],[-lrt])]) 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 have_nanosleep=false
AC_CHECK_FUNCS(nanosleep,have_nanosleep=:,[ AC_CHECK_FUNCS(nanosleep,have_nanosleep=:,[
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* $Id$ * $Id$
* *
* Authors: Vincent Seguin <seguin@via.ecp.fr> * Authors: Vincent Seguin <seguin@via.ecp.fr>
* Rémi Denis-Courmont <rem$videolan,org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
...@@ -22,15 +23,12 @@ ...@@ -22,15 +23,12 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. * 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 * Preamble
*****************************************************************************/ *****************************************************************************/
#include <stdio.h> /* sprintf() */ #include <stdio.h> /* sprintf() */
#include <time.h> /* clock_gettime(), clock_nanosleep() */
#include <stdlib.h> /* ldiv() */
#include <vlc/vlc.h> #include <vlc/vlc.h>
...@@ -106,6 +104,7 @@ char *secstotimestr( char *psz_buffer, int i_seconds ) ...@@ -106,6 +104,7 @@ char *secstotimestr( char *psz_buffer, int i_seconds )
return( psz_buffer ); return( psz_buffer );
} }
/** /**
* Return high precision date * Return high precision date
* *
...@@ -188,15 +187,23 @@ mtime_t mdate( void ) ...@@ -188,15 +187,23 @@ mtime_t mdate( void )
return usec_time; 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 #else
struct timeval tv_date; struct timeval tv_date;
/* gettimeofday() could return an error, and should be tested. However, the /* gettimeofday() cannot fail given &tv_date is a valid address */
* only possible error, according to 'man', is EFAULT, which can not happen (void)gettimeofday( &tv_date, NULL );
* here, since tv is a local variable. */
gettimeofday( &tv_date, NULL );
return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec ); return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
#endif #endif
} }
...@@ -231,6 +238,29 @@ void mwait( mtime_t date ) ...@@ -231,6 +238,29 @@ void mwait( mtime_t date )
} }
msleep( delay ); 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 #else
struct timeval tv_date; struct timeval tv_date;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment