diff mbox series

[v4,1/3] eal/windows: add timespec_get shim for MinGW

Message ID 20210415221055.1575-2-dmitry.kozliuk@gmail.com (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers show
Series net/pcap: build on Windows | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Dmitry Kozlyuk April 15, 2021, 10:10 p.m. UTC
MinGW-w64 does not currently implement C11 timespec_get.
Add an internal shim until this is fixed on MinGW side.

Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Acked-by: Jie Zhou <jizh@linux.microsoft.com>
Acked-by: Nick Connolly <nick.connolly@mayadata.io>
---
 lib/librte_eal/windows/include/rte_os_shim.h | 32 ++++++++++++++++++++
 1 file changed, 32 insertions(+)
diff mbox series

Patch

diff --git a/lib/librte_eal/windows/include/rte_os_shim.h b/lib/librte_eal/windows/include/rte_os_shim.h
index f40fb62d1d..e50895fd83 100644
--- a/lib/librte_eal/windows/include/rte_os_shim.h
+++ b/lib/librte_eal/windows/include/rte_os_shim.h
@@ -3,7 +3,10 @@ 
 #ifndef _RTE_OS_SHIM_
 #define _RTE_OS_SHIM_
 
+#include <time.h>
+
 #include <rte_os.h>
+#include <rte_windows.h>
 
 /**
  * @file
@@ -33,4 +36,33 @@ 
 #define IPPROTO_SCTP	132
 #endif
 
+#ifdef RTE_TOOLCHAIN_GCC
+
+#define TIME_UTC 1
+
+static inline int
+rte_timespec_get(struct timespec *now, int base)
+{
+	/* 100ns ticks from 1601-01-01 to 1970-01-01 */
+	static const uint64_t EPOCH = 116444736000000000ULL;
+	static const uint64_t TICKS_PER_SEC = 10000000;
+	static const uint64_t NS_PER_TICK = 100;
+
+	FILETIME ft;
+	uint64_t ticks;
+
+	if (base != TIME_UTC)
+		return 0;
+
+	GetSystemTimePreciseAsFileTime(&ft);
+	ticks = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
+	ticks -= EPOCH;
+	now->tv_sec = ticks / TICKS_PER_SEC;
+	now->tv_nsec = (ticks - now->tv_sec * TICKS_PER_SEC) * NS_PER_TICK;
+	return base;
+}
+
+#define timespec_get(ts, base) rte_timespec_get(ts, base)
+
+#endif /* RTE_TOOLCHAIN_GCC */
 #endif /* _RTE_OS_SHIM_ */