From df291664cd0e365fa4393f6ab065efbeb120332f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Kempf Date: Wed, 27 Feb 2008 08:07:23 +0000 Subject: [PATCH] Src: change Windows Timer Precision, ref #264. Patch by atmo / Andre Weber. --- src/misc/mtime.c | 47 +++++++++++++++++++++++++++++++++++---- src/misc/win32_specific.c | 7 ++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/misc/mtime.c b/src/misc/mtime.c index ead654273c..078ef68e80 100644 --- a/src/misc/mtime.c +++ b/src/misc/mtime.c @@ -49,7 +49,13 @@ #if defined( WIN32 ) || defined( UNDER_CE ) # include +# include #endif + +#if defined( UNDER_CE ) +# include +#endif + #if defined(HAVE_SYS_TIME_H) # include #endif @@ -211,6 +217,30 @@ mtime_t mdate( void ) freq = ( QueryPerformanceFrequency( &buf ) && (buf.QuadPart == I64C(1193182) || buf.QuadPart == I64C(3579545) ) ) ? buf.QuadPart : 0; + +#if defined( WIN32 ) + /* on windows 2000, XP and Vista detect if there are two + cores there - that makes QueryPerformanceFrequency in + any case not trustable? + (may also be true, for single cores with adaptive + CPU frequency and active power management?) + */ + HINSTANCE h_Kernel32 = LoadLibraryA("kernel32.dll"); + if(h_Kernel32) + { + void WINAPI (*pf_GetSystemInfo)(LPSYSTEM_INFO*); + pf_GetSystemInfo = (void WINAPI (*)(LPSYSTEM_INFO*)) + GetProcAddress(h_Kernel32, "GetSystemInfo"); + if(pf_GetSystemInfo) + { + SYSTEM_INFO system_info; + pf_GetSystemInfo(&system_info); + if(system_info.dwNumberOfProcessors > 1) + freq = 0; + } + FreeLibrary(h_Kernel32); + } +#endif } if( freq != 0 ) @@ -226,9 +256,9 @@ mtime_t mdate( void ) } else { - /* Fallback on GetTickCount() which has a milisecond resolution - * (actually, best case is about 10 ms resolution) - * GetTickCount() only returns a DWORD thus will wrap after + /* Fallback on timeGetTime() which has a milisecond resolution + * (actually, best case is about 5 ms resolution) + * timeGetTime() only returns a DWORD thus will wrap after * about 49.7 days so we try to detect the wrapping. */ static CRITICAL_SECTION date_lock; @@ -238,14 +268,23 @@ mtime_t mdate( void ) if( i_wrap_counts == -1 ) { /* Initialization */ +#if defined( WIN32 ) + i_previous_time = I64C(1000) * timeGetTime(); +#else i_previous_time = I64C(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()); +#else res = I64C(1000) * (i_wrap_counts * I64C(0x100000000) + GetTickCount()); +#endif if( i_previous_time > res ) { /* Counter wrapped */ @@ -331,7 +370,7 @@ void msleep( mtime_t delay ) snooze( delay ); #elif defined( WIN32 ) || defined( UNDER_CE ) - Sleep( (int) (delay / 1000) ); + Sleep( (DWORD) (delay / 1000) ); #elif defined( HAVE_NANOSLEEP ) struct timespec ts_delay; diff --git a/src/misc/win32_specific.c b/src/misc/win32_specific.c index dea9588d97..2fc5d2dcab 100644 --- a/src/misc/win32_specific.c +++ b/src/misc/win32_specific.c @@ -38,6 +38,7 @@ #if !defined( UNDER_CE ) # include # include +# include #endif #include @@ -94,6 +95,8 @@ void system_Init( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] ) #if !defined( UNDER_CE ) _fmode = _O_BINARY; _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */ + + timeBeginPeriod(5); #endif /* Call mdate() once to make sure it is initialized properly */ @@ -370,5 +373,9 @@ void system_End( libvlc_int_t *p_this ) vlc_global()->psz_vlcpath = NULL; } +#if !defined( UNDER_CE ) + timeEndPeriod(5); +#endif + WSACleanup(); } -- 2.39.2