X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmisc%2Fmtime.c;h=9966e473bec8f5a046fcf1b92ccbb132331bba48;hb=afd188c9e6900bc5fcf4c4284a4fe1a75bb2d5e2;hp=078ef68e808dceefacbe9a8399fa2e4546395860;hpb=df291664cd0e365fa4393f6ab065efbeb120332f;p=vlc diff --git a/src/misc/mtime.c b/src/misc/mtime.c index 078ef68e80..9966e473be 100644 --- a/src/misc/mtime.c +++ b/src/misc/mtime.c @@ -33,7 +33,7 @@ # include "config.h" #endif -#include +#include #include /* clock_gettime(), clock_nanosleep() */ #include @@ -144,6 +144,19 @@ char *secstotimestr( char *psz_buffer, int i_seconds ) 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). @@ -151,18 +164,14 @@ char *secstotimestr( char *psz_buffer, int i_seconds ) 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 * @@ -190,9 +199,9 @@ mtime_t mdate( void ) #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) @@ -215,7 +224,7 @@ mtime_t mdate( void ) 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 ) @@ -225,12 +234,12 @@ mtime_t mdate( void ) (may also be true, for single cores with adaptive CPU frequency and active power management?) */ - HINSTANCE h_Kernel32 = LoadLibraryA("kernel32.dll"); + HINSTANCE h_Kernel32 = LoadLibrary(_T("kernel32.dll")); if(h_Kernel32) { - void WINAPI (*pf_GetSystemInfo)(LPSYSTEM_INFO*); - pf_GetSystemInfo = (void WINAPI (*)(LPSYSTEM_INFO*)) - GetProcAddress(h_Kernel32, "GetSystemInfo"); + void WINAPI (*pf_GetSystemInfo)(LPSYSTEM_INFO); + pf_GetSystemInfo = (void WINAPI (*)(LPSYSTEM_INFO)) + GetProcAddress(h_Kernel32, _T("GetSystemInfo")); if(pf_GetSystemInfo) { SYSTEM_INFO system_info; @@ -262,16 +271,16 @@ mtime_t mdate( void ) * 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; @@ -279,17 +288,17 @@ mtime_t mdate( void ) 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 ); @@ -302,7 +311,7 @@ mtime_t mdate( void ) res = (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec; #endif - return cached_time = res; + return res; } /** @@ -315,15 +324,11 @@ mtime_t mdate( void ) */ 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 }; @@ -352,8 +357,6 @@ void mwait( mtime_t date ) */ 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 }; @@ -390,10 +393,6 @@ void msleep( mtime_t delay ) * 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; } /*