Implement ms time with GetSystemTimeAsFile time.

There's a potential race condition with calling time(NULL) after
GetSystemTime().

See
https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/march/implementing-a-high-resolution-time-provider-for-windows

Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
This commit is contained in:
Jerry Yu 2023-01-30 13:27:54 +08:00
parent 38257491aa
commit 947fd3d6ea

View file

@ -196,10 +196,13 @@ mbedtls_ms_time_t mbedtls_ms_time(void)
#include <windows.h>
mbedtls_ms_time_t mbedtls_ms_time(void)
{
SYSTEMTIME st;
FILETIME ct;
mbedtls_ms_time_t current_ms;
GetSystemTime(&st);
return time(NULL)*1000LL + st.wMilliseconds;
GetSystemTimeAsFileTime(&ct);
current_ms = ((mbedtls_ms_time_t) ct.dwLowDateTime +
((mbedtls_ms_time_t) (ct.dwHighDateTime) << 32LL))/10;
return current_ms;
}
#else
#error "No mbedtls_ms_time available"