From 1fc0617dccc3fa138235f219e2eaa7b405d1162e Mon Sep 17 00:00:00 2001 From: Jay Satiro Date: Fri, 24 Jan 2020 03:34:52 -0500 Subject: tool_util: Improve Windows version of tvnow() - Change tool_util.c tvnow() for Windows to match more closely to timeval.c Curl_now(). - Create a win32 init function for the tool, since some initialization is required for the tvnow() changes. Prior to this change the monotonic time function used by curl in Windows was determined at build-time and not runtime. That was a problem because when curl was built targeted for compatibility with old versions of Windows (eg _WIN32_WINNT < 0x0600) it would use GetTickCount which wraps every 49.7 days that Windows has been running. This change makes curl behave similar to libcurl's tvnow function, which determines at runtime whether the OS is Vista+ and if so calls QueryPerformanceCounter instead. (Note QueryPerformanceCounter is used because it has higher resolution than the more obvious candidate GetTickCount64). The changes to tvnow are basically a copy and paste but the types in some cases are different. Ref: https://github.com/curl/curl/issues/3309 Closes https://github.com/curl/curl/pull/4847 --- src/tool_util.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'src/tool_util.c') diff --git a/src/tool_util.c b/src/tool_util.c index 04d73e4e3..ec99bf3f6 100644 --- a/src/tool_util.c +++ b/src/tool_util.c @@ -27,27 +27,35 @@ #if defined(WIN32) && !defined(MSDOS) +/* set in win32_init() */ +extern LARGE_INTEGER Curl_freq; +extern bool Curl_isVistaOrGreater; + +/* In case of bug fix this function has a counterpart in timeval.c */ struct timeval tvnow(void) { - /* - ** GetTickCount() is available on _all_ Windows versions from W95 up - ** to nowadays. Returns milliseconds elapsed since last system boot, - ** increases monotonically and wraps once 49.7 days have elapsed. - ** - ** GetTickCount64() is available on Windows version from Windows Vista - ** and Windows Server 2008 up to nowadays. The resolution of the - ** function is limited to the resolution of the system timer, which - ** is typically in the range of 10 milliseconds to 16 milliseconds. - */ struct timeval now; -#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) && \ - (!defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR)) - ULONGLONG milliseconds = GetTickCount64(); -#else - DWORD milliseconds = GetTickCount(); + if(Curl_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */ + LARGE_INTEGER count; + QueryPerformanceCounter(&count); + now.tv_sec = (long)(count.QuadPart / Curl_freq.QuadPart); + now.tv_usec = (long)((count.QuadPart % Curl_freq.QuadPart) * 1000000 / + Curl_freq.QuadPart); + } + else { + /* Disable /analyze warning that GetTickCount64 is preferred */ +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable:28159) +#endif + DWORD milliseconds = GetTickCount(); +#if defined(_MSC_VER) +#pragma warning(pop) #endif - now.tv_sec = (long)(milliseconds / 1000); - now.tv_usec = (long)((milliseconds % 1000) * 1000); + + now.tv_sec = (long)(milliseconds / 1000); + now.tv_usec = (long)((milliseconds % 1000) * 1000); + } return now; } -- cgit v1.2.3