# include "config.h"
#endif
-#include <vlc/vlc.h>
+#include <vlc_common.h>
#include <time.h> /* clock_gettime(), clock_nanosleep() */
#include <assert.h>
return( psz_buffer );
}
+#if defined (HAVE_CLOCK_NANOSLEEP)
+static unsigned prec = 0;
+
+static void mprec_once( void )
+{
+ struct timespec ts;
+ if( clock_getres( CLOCK_MONOTONIC, &ts ))
+ clock_getres( CLOCK_REALTIME, &ts );
+
+ prec = ts.tv_nsec / 1000;
+}
+#endif
+
/**
* Return a value that is no bigger than the clock precision
* (possibly zero).
static inline unsigned mprec( void )
{
#if defined (HAVE_CLOCK_NANOSLEEP)
- struct timespec ts;
- if( clock_getres( CLOCK_MONOTONIC, &ts ))
- clock_getres( CLOCK_REALTIME, &ts );
-
- return ts.tv_nsec / 1000;
-#endif
+ static pthread_once_t once = PTHREAD_ONCE_INIT;
+ pthread_once( &once, mprec_once );
+ return prec;
+#else
return 0;
+#endif
}
-static unsigned prec = 0;
-static volatile mtime_t cached_time = 0;
-
/**
* Return high precision date
*
#elif defined( WIN32 ) || defined( UNDER_CE )
/* We don't need the real date, just the value of a high precision timer */
- static mtime_t freq = I64C(-1);
+ static mtime_t freq = INT64_C(-1);
- if( freq == I64C(-1) )
+ if( freq == INT64_C(-1) )
{
/* Extract from the Tcl source code:
* (http://www.cs.man.ac.uk/fellowsd-bin/TIP/7.html)
LARGE_INTEGER buf;
freq = ( QueryPerformanceFrequency( &buf ) &&
- (buf.QuadPart == I64C(1193182) || buf.QuadPart == I64C(3579545) ) )
+ (buf.QuadPart == INT64_C(1193182) || buf.QuadPart == INT64_C(3579545) ) )
? buf.QuadPart : 0;
#if defined( WIN32 )
* about 49.7 days so we try to detect the wrapping. */
static CRITICAL_SECTION date_lock;
- static mtime_t i_previous_time = I64C(-1);
+ static mtime_t i_previous_time = INT64_C(-1);
static int i_wrap_counts = -1;
if( i_wrap_counts == -1 )
{
/* Initialization */
#if defined( WIN32 )
- i_previous_time = I64C(1000) * timeGetTime();
+ i_previous_time = INT64_C(1000) * timeGetTime();
#else
- i_previous_time = I64C(1000) * GetTickCount();
+ i_previous_time = INT64_C(1000) * GetTickCount();
#endif
InitializeCriticalSection( &date_lock );
i_wrap_counts = 0;
EnterCriticalSection( &date_lock );
#if defined( WIN32 )
- res = I64C(1000) *
- (i_wrap_counts * I64C(0x100000000) + timeGetTime());
+ res = INT64_C(1000) *
+ (i_wrap_counts * INT64_C(0x100000000) + timeGetTime());
#else
- res = I64C(1000) *
- (i_wrap_counts * I64C(0x100000000) + GetTickCount());
+ res = INT64_C(1000) *
+ (i_wrap_counts * INT64_C(0x100000000) + GetTickCount());
#endif
if( i_previous_time > res )
{
/* Counter wrapped */
i_wrap_counts++;
- res += I64C(0x100000000) * 1000;
+ res += INT64_C(0x100000000) * 1000;
}
i_previous_time = res;
LeaveCriticalSection( &date_lock );
res = (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
#endif
- return cached_time = res;
+ return res;
}
/**
*/
void mwait( mtime_t date )
{
- if( prec == 0 )
- prec = mprec();
-
/* If the deadline is already elapsed, or within the clock precision,
- * do not even bother the clock. */
- if( ( date - cached_time ) < (mtime_t)prec ) // OK: mtime_t is signed
- return;
+ * do not even bother the system timer. */
+ date -= mprec();
-#if 0 && defined (HAVE_CLOCK_NANOSLEEP)
+#if defined (HAVE_CLOCK_NANOSLEEP)
lldiv_t d = lldiv( date, 1000000 );
struct timespec ts = { d.quot, d.rem * 1000 };
*/
void msleep( mtime_t delay )
{
- mtime_t earlier = cached_time;
-
#if defined( HAVE_CLOCK_NANOSLEEP )
lldiv_t d = lldiv( delay, 1000000 );
struct timespec ts = { d.quot, d.rem * 1000 };
* or clock_nanosleep() if this is an issue. */
select( 0, NULL, NULL, NULL, &tv_delay );
#endif
-
- earlier += delay;
- if( cached_time < earlier )
- cached_time = earlier;
}
/*