From 24b6bc3ce36b73bcb0d6d32fe702e2761e337e85 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Wed, 11 Feb 2015 16:21:13 -0800
Subject: [PATCH 1/2] Switch gettimeofday to use GetSystemTimeAsFileTime on
 win32 by default

Since commit 8001fe6, processes or client binaries running on Windows
can initialize the syscall function used to map gettimeofday in libpgport
by calling a dedicated initialization function. However this was done in
a way where any process calling gettimeofday from libpgport needs mandatorily
to call this initialization function, making for example any client binary
calling gettimeofday() crash on Windows should it not be initialized.

This commit switches gettimeofday to use by default GetSystemTimeAsFileTime,
which is backward-compatible with the old behavior to prevent any crashes.
Existing client binaries can simply upgrade to the precise API by calling
the initialization function.

Do at the same time some wordsmithing in gettimeofday.c, which considered
only backends in the comments.

Per patch and report from Asif Naheem.
---
 src/backend/main/main.c |  7 +++++--
 src/include/port.h      |  7 +++++--
 src/port/gettimeofday.c | 12 +++++++-----
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/src/backend/main/main.c b/src/backend/main/main.c
index 582198f..d712f4c 100644
--- a/src/backend/main/main.c
+++ b/src/backend/main/main.c
@@ -263,8 +263,11 @@ startup_hacks(const char *progname)
 		SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
 
 #ifndef HAVE_GETTIMEOFDAY
-		/* Figure out which syscall to use to capture timestamp information */
-		init_win32_gettimeofday();
+		/*
+		 * Attempt upgrade to the highest resolution syscall to capture
+		 * timestamp information.
+		 */
+		pgwin32_upgrade_gettimeofday();
 #endif
 
 	}
diff --git a/src/include/port.h b/src/include/port.h
index 26d7fcd..60501d8 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -328,8 +328,11 @@ extern FILE *pgwin32_popen(const char *command, const char *type);
 #ifndef HAVE_GETTIMEOFDAY
 /* Last parameter not used */
 extern int	gettimeofday(struct timeval * tp, struct timezone * tzp);
-/* On windows we need to call some backend start setup for accurate timing */
-extern void init_win32_gettimeofday(void);
+/*
+ * On windows this function can be called to upgrade to a higher level of
+ * precision for capture of timestamp information.
+ */
+extern void pgwin32_upgrade_gettimeofday(void);
 #endif
 #else							/* !WIN32 */
 
diff --git a/src/port/gettimeofday.c b/src/port/gettimeofday.c
index eabf161..167df15 100644
--- a/src/port/gettimeofday.c
+++ b/src/port/gettimeofday.c
@@ -48,15 +48,17 @@ static const unsigned __int64 epoch = UINT64CONST(116444736000000000);
  */
 typedef VOID (WINAPI *PgGetSystemTimeFn)(LPFILETIME);
 
-/* Storage for the function we pick at runtime */
-static PgGetSystemTimeFn pg_get_system_time = NULL;
+/* Storage for the function picked up at runtime */
+static PgGetSystemTimeFn pg_get_system_time = &GetSystemTimeAsFileTime;
 
 /*
- * During backend startup, determine if GetSystemTimePreciseAsFileTime is
- * available and use it; if not, fall back to GetSystemTimeAsFileTime.
+ * Determine if GetSystemTimePreciseAsFileTime is available and use it to
+ * allow the use of the highest level of resolution for timestamp information
+ * timestamp resolution on system. If it is not available, fall back to
+ * GetSystemTimeAsFileTime.
  */
 void
-init_win32_gettimeofday(void)
+pgwin32_upgrade_gettimeofday(void)
 {
 	/*
 	 * Because it's guaranteed that kernel32.dll will be linked into our
-- 
1.9.2.msysgit.0

