From: Gildas Bazin Date: Tue, 9 Dec 2003 19:18:48 +0000 (+0000) Subject: * src/misc/mtime.c: fixed an overflow in mdate() on win32. X-Git-Tag: 0.7.0~174 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;ds=sidebyside;h=9e1d63b5dc6ac63a451eaa7708d65ab0973bd6e2;p=vlc * src/misc/mtime.c: fixed an overflow in mdate() on win32. This overflow was only affecting the hardware configurations where we can't use the performance counter and thus have to use GetTickCount(). We now also check for the wrapping of the 32 bits value returned by GetTickCount() so mdate() doesn't wrap after 49.7 days. * src/misc/win32_specific.c: call mdate() once in system_Init() so as to avoid thread safety issues while initialising the static variables used in mdate(). --- diff --git a/src/misc/mtime.c b/src/misc/mtime.c index 324feea070..498dc2a308 100644 --- a/src/misc/mtime.c +++ b/src/misc/mtime.c @@ -3,7 +3,7 @@ * Functions are prototyped in mtime.h. ***************************************************************************** * Copyright (C) 1998-2001, 2003 VideoLAN - * $Id: mtime.c,v 1.39 2003/12/03 21:50:50 sigmunau Exp $ + * $Id: mtime.c,v 1.40 2003/12/09 19:18:48 gbazin Exp $ * * Authors: Vincent Seguin * @@ -154,9 +154,39 @@ mtime_t mdate( void ) QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time ); return ( usec_time * 1000000 ) / freq; } - - /* Milisecond resolution (actually, best case is about 10 ms resolution) */ - return 1000 * GetTickCount(); + 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 + * 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 int i_wrap_counts = -1; + + if( i_wrap_counts == -1 ) + { + /* Initialization */ + i_previous_time = usec_time; + InitializeCriticalSection( &date_lock ); + i_wrap_counts = 0; + } + + EnterCriticalSection( &date_lock ); + usec_time = I64C(1000) * + (i_wrap_counts * I64C(0x100000000) + GetTickCount()); + if( i_previous_time > usec_time ) + { + /* Counter wrapped */ + i_wrap_counts++; + usec_time += I64C(0x100000000000); + } + i_previous_time = usec_time; + LeaveCriticalSection( &date_lock ); + + return usec_time; + } #else struct timeval tv_date; diff --git a/src/misc/win32_specific.c b/src/misc/win32_specific.c index 4782f2059c..7afacde49a 100644 --- a/src/misc/win32_specific.c +++ b/src/misc/win32_specific.c @@ -2,7 +2,7 @@ * win32_specific.c: Win32 specific features ***************************************************************************** * Copyright (C) 2001 VideoLAN - * $Id: win32_specific.c,v 1.27 2003/12/02 12:57:36 gbazin Exp $ + * $Id: win32_specific.c,v 1.28 2003/12/09 19:18:48 gbazin Exp $ * * Authors: Samuel Hocevar * Gildas Bazin @@ -72,6 +72,9 @@ void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] ) _fmode = _O_BINARY; _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */ + /* Call mdate() once to make sure it is initialized properly */ + mdate(); + /* WinSock Library Init. */ if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) ) {