From 922a8fa098404cbd5c6089e78eca4aebd828f847 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Sat, 6 Aug 2022 08:34:56 -0700 Subject: Simplify gettimeofday() fallback logic. There's no known supported system needing 1 argument gettimeofday() support. The test for it was added a long time ago (92c6bf9775b). Remove. Until now we tested whether a gettimeofday() fallback is needed when targetting windows. Which lead to the odd result that HAVE_GETTIMEOFDAY only being defined when targetting MinGW (which has gettimeofday() since at least 2007). As the fallback is specific to msvc, remove the configure code and rename src/port/gettimeofday.c to src/port/win32gettimeofday.c. While at it, also remove the definition of struct timezone, a forward declaration of the struct is sufficient. Reviewed-By: Tom Lane Reviewed-By: Thomas Munro Discussion: https://postgr.es/m/20220806000311.ywx65iuchvj4qn2k@awork3.anarazel.de --- config/c-library.m4 | 24 --------- configure | 47 ----------------- configure.ac | 2 - src/include/pg_config.h.in | 10 ---- src/include/port.h | 5 -- src/include/port/win32_port.h | 15 ++---- src/port/gettimeofday.c | 118 ------------------------------------------ src/port/win32gettimeofday.c | 118 ++++++++++++++++++++++++++++++++++++++++++ src/tools/msvc/Mkvcbuild.pm | 3 +- src/tools/msvc/Solution.pm | 2 - 10 files changed, 125 insertions(+), 219 deletions(-) delete mode 100644 src/port/gettimeofday.c create mode 100644 src/port/win32gettimeofday.c diff --git a/config/c-library.m4 b/config/c-library.m4 index 163ad5742d8..35fdc63afee 100644 --- a/config/c-library.m4 +++ b/config/c-library.m4 @@ -37,30 +37,6 @@ AC_DEFUN([PGAC_STRUCT_TIMEZONE], ])# PGAC_STRUCT_TIMEZONE -# PGAC_FUNC_GETTIMEOFDAY_1ARG -# --------------------------- -# Check if gettimeofday() has only one arguments. (Normal is two.) -# If so, define GETTIMEOFDAY_1ARG. -AC_DEFUN([PGAC_FUNC_GETTIMEOFDAY_1ARG], -[AC_CACHE_CHECK(whether gettimeofday takes only one argument, -pgac_cv_func_gettimeofday_1arg, -[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include ], -[struct timeval *tp; -struct timezone *tzp; -gettimeofday(tp,tzp);])], -[pgac_cv_func_gettimeofday_1arg=no], -[pgac_cv_func_gettimeofday_1arg=yes])]) -if test x"$pgac_cv_func_gettimeofday_1arg" = xyes ; then - AC_DEFINE(GETTIMEOFDAY_1ARG, 1, - [Define to 1 if gettimeofday() takes only 1 argument.]) -fi -AH_VERBATIM(GETTIMEOFDAY_1ARG_, -[@%:@ifdef GETTIMEOFDAY_1ARG -@%:@ define gettimeofday(a,b) gettimeofday(a) -@%:@endif])dnl -])# PGAC_FUNC_GETTIMEOFDAY_1ARG - - # PGAC_FUNC_STRERROR_R_INT # --------------------------- # Check if strerror_r() returns int (POSIX) rather than char * (GNU libc). diff --git a/configure b/configure index aa7f55b33a6..0e73edb9ffa 100755 --- a/configure +++ b/configure @@ -15945,39 +15945,6 @@ if test x"$pgac_cv_var_int_timezone" = xyes ; then $as_echo "#define HAVE_INT_TIMEZONE 1" >>confdefs.h fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether gettimeofday takes only one argument" >&5 -$as_echo_n "checking whether gettimeofday takes only one argument... " >&6; } -if ${pgac_cv_func_gettimeofday_1arg+:} false; then : - $as_echo_n "(cached) " >&6 -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include -int -main () -{ -struct timeval *tp; -struct timezone *tzp; -gettimeofday(tp,tzp); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - pgac_cv_func_gettimeofday_1arg=no -else - pgac_cv_func_gettimeofday_1arg=yes -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_func_gettimeofday_1arg" >&5 -$as_echo "$pgac_cv_func_gettimeofday_1arg" >&6; } -if test x"$pgac_cv_func_gettimeofday_1arg" = xyes ; then - -$as_echo "#define GETTIMEOFDAY_1ARG 1" >>confdefs.h - -fi - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for wcstombs_l declaration" >&5 $as_echo_n "checking for wcstombs_l declaration... " >&6; } if ${pgac_cv_func_wcstombs_l+:} false; then : @@ -16909,20 +16876,6 @@ _ACEOF fi done - ac_fn_c_check_func "$LINENO" "gettimeofday" "ac_cv_func_gettimeofday" -if test "x$ac_cv_func_gettimeofday" = xyes; then : - $as_echo "#define HAVE_GETTIMEOFDAY 1" >>confdefs.h - -else - case " $LIBOBJS " in - *" gettimeofday.$ac_objext "* ) ;; - *) LIBOBJS="$LIBOBJS gettimeofday.$ac_objext" - ;; -esac - -fi - - case " $LIBOBJS " in *" dirmod.$ac_objext "* ) ;; *) LIBOBJS="$LIBOBJS dirmod.$ac_objext" diff --git a/configure.ac b/configure.ac index 6a9463b5ae2..efd3be91cd4 100644 --- a/configure.ac +++ b/configure.ac @@ -1779,7 +1779,6 @@ fi ## PGAC_VAR_INT_TIMEZONE -PGAC_FUNC_GETTIMEOFDAY_1ARG PGAC_FUNC_WCSTOMBS_L # Some versions of libedit contain strlcpy(), setproctitle(), and other @@ -1925,7 +1924,6 @@ fi # Win32 (really MinGW) support if test "$PORTNAME" = "win32"; then AC_CHECK_FUNCS(_configthreadlocale) - AC_REPLACE_FUNCS(gettimeofday) AC_LIBOBJ(dirmod) AC_LIBOBJ(getrusage) AC_LIBOBJ(kill) diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index b584788dc08..2d9a1cdc8ab 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -55,13 +55,6 @@ (--enable-thread-safety) */ #undef ENABLE_THREAD_SAFETY -/* Define to 1 if gettimeofday() takes only 1 argument. */ -#undef GETTIMEOFDAY_1ARG - -#ifdef GETTIMEOFDAY_1ARG -# define gettimeofday(a,b) gettimeofday(a) -#endif - /* Define to 1 if you have the `append_history' function. */ #undef HAVE_APPEND_HISTORY @@ -215,9 +208,6 @@ /* Define to 1 if you have the `getpeerucred' function. */ #undef HAVE_GETPEERUCRED -/* Define to 1 if you have the `gettimeofday' function. */ -#undef HAVE_GETTIMEOFDAY - /* Define to 1 if you have the header file. */ #undef HAVE_GSSAPI_GSSAPI_H diff --git a/src/include/port.h b/src/include/port.h index feb2ae840dd..ad76384fb19 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -327,11 +327,6 @@ extern FILE *pgwin32_popen(const char *command, const char *type); #define popen(a,b) pgwin32_popen(a,b) #define pclose(a) _pclose(a) -/* New versions of MingW have gettimeofday, old mingw and msvc don't */ -#ifndef HAVE_GETTIMEOFDAY -/* Last parameter not used */ -extern int gettimeofday(struct timeval *tp, struct timezone *tzp); -#endif #else /* !WIN32 */ /* diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h index a65996cce51..79451a00f96 100644 --- a/src/include/port/win32_port.h +++ b/src/include/port/win32_port.h @@ -179,16 +179,11 @@ #define SIGUSR1 30 #define SIGUSR2 31 -/* - * New versions of MinGW have gettimeofday() and also declare - * struct timezone to support it. - */ -#ifndef HAVE_GETTIMEOFDAY -struct timezone -{ - int tz_minuteswest; /* Minutes west of GMT. */ - int tz_dsttime; /* Nonzero if DST is ever in effect. */ -}; +/* MinW has gettimeofday(), but MSVC doesn't */ +#ifdef _MSC_VER +struct timezone; +/* Last parameter not used */ +extern int gettimeofday(struct timeval *tp, struct timezone *tzp); #endif /* for setitimer in backend/port/win32/timer.c */ diff --git a/src/port/gettimeofday.c b/src/port/gettimeofday.c deleted file mode 100644 index ee8fe823378..00000000000 --- a/src/port/gettimeofday.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * gettimeofday.c - * Win32 gettimeofday() replacement - * - * src/port/gettimeofday.c - * - * Copyright (c) 2003 SRA, Inc. - * Copyright (c) 2003 SKC, Inc. - * - * Permission to use, copy, modify, and distribute this software and - * its documentation for any purpose, without fee, and without a - * written agreement is hereby granted, provided that the above - * copyright notice and this paragraph and the following two - * paragraphs appear in all copies. - * - * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, - * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING - * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS - * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS - * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, - * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. - */ - -#include "c.h" - -#include - -/* FILETIME of Jan 1 1970 00:00:00, the PostgreSQL epoch */ -static const unsigned __int64 epoch = UINT64CONST(116444736000000000); - -/* - * FILETIME represents the number of 100-nanosecond intervals since - * January 1, 1601 (UTC). - */ -#define FILETIME_UNITS_PER_SEC 10000000L -#define FILETIME_UNITS_PER_USEC 10 - -/* - * Both GetSystemTimeAsFileTime and GetSystemTimePreciseAsFileTime share a - * signature, so we can just store a pointer to whichever we find. This - * is the pointer's type. - */ -typedef VOID(WINAPI * PgGetSystemTimeFn) (LPFILETIME); - -/* One-time initializer function, must match that signature. */ -static void WINAPI init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime); - -/* Storage for the function we pick at runtime */ -static PgGetSystemTimeFn pg_get_system_time = &init_gettimeofday; - -/* - * One time initializer. Determine whether GetSystemTimePreciseAsFileTime - * is available and if so, plan to use it; if not, fall back to - * GetSystemTimeAsFileTime. - */ -static void WINAPI -init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime) -{ - /* - * Because it's guaranteed that kernel32.dll will be linked into our - * address space already, we don't need to LoadLibrary it and worry about - * closing it afterwards, so we're not using Pg's dlopen/dlsym() wrapper. - * - * We'll just look up the address of GetSystemTimePreciseAsFileTime if - * present. - * - * While we could look up the Windows version and skip this on Windows - * versions below Windows 8 / Windows Server 2012 there isn't much point, - * and determining the windows version is its self somewhat Windows - * version and development SDK specific... - */ - pg_get_system_time = (PgGetSystemTimeFn) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), - "GetSystemTimePreciseAsFileTime"); - if (pg_get_system_time == NULL) - { - /* - * The expected error from GetLastError() is ERROR_PROC_NOT_FOUND, if - * the function isn't present. No other error should occur. - * - * We can't report an error here because this might be running in - * frontend code; and even if we're in the backend, it's too early to - * elog(...) if we get some unexpected error. Also, it's not a - * serious problem, so just silently fall back to - * GetSystemTimeAsFileTime irrespective of why the failure occurred. - */ - pg_get_system_time = &GetSystemTimeAsFileTime; - } - - (*pg_get_system_time) (lpSystemTimeAsFileTime); -} - -/* - * timezone information is stored outside the kernel so tzp isn't used anymore. - * - * Note: this function is not for Win32 high precision timing purposes. See - * elapsed_time(). - */ -int -gettimeofday(struct timeval *tp, struct timezone *tzp) -{ - FILETIME file_time; - ULARGE_INTEGER ularge; - - (*pg_get_system_time) (&file_time); - ularge.LowPart = file_time.dwLowDateTime; - ularge.HighPart = file_time.dwHighDateTime; - - tp->tv_sec = (long) ((ularge.QuadPart - epoch) / FILETIME_UNITS_PER_SEC); - tp->tv_usec = (long) (((ularge.QuadPart - epoch) % FILETIME_UNITS_PER_SEC) - / FILETIME_UNITS_PER_USEC); - - return 0; -} diff --git a/src/port/win32gettimeofday.c b/src/port/win32gettimeofday.c new file mode 100644 index 00000000000..04645487583 --- /dev/null +++ b/src/port/win32gettimeofday.c @@ -0,0 +1,118 @@ +/* + * win32gettimeofday.c + * Win32 gettimeofday() replacement + * + * src/port/win32gettimeofday.c + * + * Copyright (c) 2003 SRA, Inc. + * Copyright (c) 2003 SKC, Inc. + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose, without fee, and without a + * written agreement is hereby granted, provided that the above + * copyright notice and this paragraph and the following two + * paragraphs appear in all copies. + * + * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, + * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING + * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS + * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS + * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, + * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#include "c.h" + +#include + +/* FILETIME of Jan 1 1970 00:00:00, the PostgreSQL epoch */ +static const unsigned __int64 epoch = UINT64CONST(116444736000000000); + +/* + * FILETIME represents the number of 100-nanosecond intervals since + * January 1, 1601 (UTC). + */ +#define FILETIME_UNITS_PER_SEC 10000000L +#define FILETIME_UNITS_PER_USEC 10 + +/* + * Both GetSystemTimeAsFileTime and GetSystemTimePreciseAsFileTime share a + * signature, so we can just store a pointer to whichever we find. This + * is the pointer's type. + */ +typedef VOID(WINAPI * PgGetSystemTimeFn) (LPFILETIME); + +/* One-time initializer function, must match that signature. */ +static void WINAPI init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime); + +/* Storage for the function we pick at runtime */ +static PgGetSystemTimeFn pg_get_system_time = &init_gettimeofday; + +/* + * One time initializer. Determine whether GetSystemTimePreciseAsFileTime + * is available and if so, plan to use it; if not, fall back to + * GetSystemTimeAsFileTime. + */ +static void WINAPI +init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime) +{ + /* + * Because it's guaranteed that kernel32.dll will be linked into our + * address space already, we don't need to LoadLibrary it and worry about + * closing it afterwards, so we're not using Pg's dlopen/dlsym() wrapper. + * + * We'll just look up the address of GetSystemTimePreciseAsFileTime if + * present. + * + * While we could look up the Windows version and skip this on Windows + * versions below Windows 8 / Windows Server 2012 there isn't much point, + * and determining the windows version is its self somewhat Windows + * version and development SDK specific... + */ + pg_get_system_time = (PgGetSystemTimeFn) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), + "GetSystemTimePreciseAsFileTime"); + if (pg_get_system_time == NULL) + { + /* + * The expected error from GetLastError() is ERROR_PROC_NOT_FOUND, if + * the function isn't present. No other error should occur. + * + * We can't report an error here because this might be running in + * frontend code; and even if we're in the backend, it's too early to + * elog(...) if we get some unexpected error. Also, it's not a + * serious problem, so just silently fall back to + * GetSystemTimeAsFileTime irrespective of why the failure occurred. + */ + pg_get_system_time = &GetSystemTimeAsFileTime; + } + + (*pg_get_system_time) (lpSystemTimeAsFileTime); +} + +/* + * timezone information is stored outside the kernel so tzp isn't used anymore. + * + * Note: this function is not for Win32 high precision timing purposes. See + * elapsed_time(). + */ +int +gettimeofday(struct timeval *tp, struct timezone *tzp) +{ + FILETIME file_time; + ULARGE_INTEGER ularge; + + (*pg_get_system_time) (&file_time); + ularge.LowPart = file_time.dwLowDateTime; + ularge.HighPart = file_time.dwHighDateTime; + + tp->tv_sec = (long) ((ularge.QuadPart - epoch) / FILETIME_UNITS_PER_SEC); + tp->tv_usec = (long) (((ularge.QuadPart - epoch) % FILETIME_UNITS_PER_SEC) + / FILETIME_UNITS_PER_USEC); + + return 0; +} diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index 8d88e78f051..bacc9207581 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -101,7 +101,7 @@ sub mkvcbuild our @pgportfiles = qw( chklocale.c explicit_bzero.c getpeereid.c getrusage.c inet_aton.c - getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c + getaddrinfo.c inet_net_ntop.c kill.c open.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c dirent.c getopt.c getopt_long.c preadv.c pwritev.c pg_bitutils.c @@ -111,6 +111,7 @@ sub mkvcbuild win32dlopen.c win32env.c win32error.c win32fdatasync.c + win32gettimeofday.c win32link.c win32pread.c win32pwrite.c diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index 9fe53bb036e..4916a86f5cf 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -220,7 +220,6 @@ sub GenerateFiles ENABLE_GSS => $self->{options}->{gss} ? 1 : undef, ENABLE_NLS => $self->{options}->{nls} ? 1 : undef, ENABLE_THREAD_SAFETY => 1, - GETTIMEOFDAY_1ARG => undef, HAVE_APPEND_HISTORY => undef, HAVE_ASN1_STRING_GET0_DATA => undef, HAVE_ATOMICS => 1, @@ -267,7 +266,6 @@ sub GenerateFiles HAVE_GETOPT_LONG => undef, HAVE_GETPEEREID => undef, HAVE_GETPEERUCRED => undef, - HAVE_GETTIMEOFDAY => undef, HAVE_GSSAPI_GSSAPI_H => undef, HAVE_GSSAPI_H => undef, HAVE_HMAC_CTX_FREE => undef, -- cgit v1.2.3