This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [patch,fortran] Fix corner case where consecutive calls to date_and_time can return times out of sequence (PR30015)


Hi Andreas, hi all,


+#if HAVE_GETTIMEOFDAY  [...]
+#if GETTIMEOFDAY_ONE_ARGUMENT
+    if (!gettimeofday (&tp))
+#else [...]
+#if HAVE_STRUCT_TIMEZONE
+    if (!gettimeofday (&tp, &tzp))
+#else
+    if (!gettimeofday (&tp, (void *) 0))
+#endif /* HAVE_STRUCT_TIMEZONE  */

Andreas Schwab wrote:
> The timezone information is never used, why not always passing NULL for
> it?  This is the only use of gettimeofday in libgfortran, it would also
> make it possible to eliminate a runtime configure check.
>   
I don't know what you mean by runtime configure check. One would speed
up the ./configure when building libgfortran, but I wouldn't call this
runtime.

I agree that one can get rid of HAVE_STRUCT_TIMEZONE and simply pass
NULL as second argument.

Does anyone know whether there is any implementation, which does not
have two arguments for gettimeofday? (Or, for completeness, have
problems with a NULL pointer as second argument?)

POSIX 2001 defines only
       int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
and notes that "If tzp is not a null pointer, the behavior is unspecified."

If no such platform exists, I would like to use a simple:

#if HAVE_GETTIMEOFDAY
    if (!gettimeofday (&tp, NULL))
this is actually also used that way in:
  ./libiberty/mkstemps.c
  ./libgomp/config/posix/time.c
  ./libmudflap/mf-runtime.c


Would something like the attached patch work?

(Note: I know only very little about using the auto* tools. Especially,
I do not how to *properly* regenerate configure; e.g. I have autoconf
2.60, it was generated by autoconf 2.59 -- is 2.60 also ok?)

Tobias
Index: libgfortran/configure.ac
===================================================================
--- libgfortran/configure.ac	(Revision 119707)
+++ libgfortran/configure.ac	(Arbeitskopie)
@@ -171,7 +171,7 @@
 # Check for library functions.
 AC_CHECK_FUNCS(getrusage times mkstemp strtof strtold snprintf ftruncate chsize)
 AC_CHECK_FUNCS(chdir strerror getlogin gethostname kill link symlink perror)
-AC_CHECK_FUNCS(sleep time ttyname signal alarm ctime clock access fork execl)
+AC_CHECK_FUNCS(sleep time ttyname signal alarm ctime clock access fork execl gettimeofday)
 AC_CHECK_FUNCS(wait setmode)
 
 # Check for types
@@ -374,11 +374,6 @@
 FPU_HOST_HEADER=config/${fpu_host}.h
 AC_SUBST(FPU_HOST_HEADER)
 
-# The standard autoconf HAVE_STRUCT_TIMEZONE doesn't actually check
-# for struct timezone, as you might think.  We also need to check how
-# to call gettimeofday if we have it.
-LIBGFOR_GETTIMEOFDAY
-
 # Attempt to assert that the target is of common type in case we don't
 # have C99 integer types at all.
 LIBGFOR_TARGET_ILP32
Index: libgfortran/acinclude.m4
===================================================================
--- libgfortran/acinclude.m4	(Revision 119707)
+++ libgfortran/acinclude.m4	(Arbeitskopie)
@@ -1,81 +1,6 @@
 m4_include(../config/acx.m4)
 m4_include(../config/no-executables.m4)
 
-dnl Check:
-dnl * If we have gettimeofday;
-dnl * If we have struct timezone for use in calling it;
-dnl * If calling it with a timezone pointer actually works -- this is deemed
-dnl   obsolete or undefined on some systems which say you should use a null
-dnl   pointer -- and undefine HAVE_TIMEZONE if so;
-dnl * Whether it only takes one arg.
-AC_DEFUN([LIBGFOR_GETTIMEOFDAY], [
-  AC_CHECK_FUNCS(gettimeofday)
-  if test "$ac_cv_func_gettimeofday" = yes; then
-    AC_CACHE_CHECK([for struct timezone], gfor_cv_struct_timezone,
-      [AC_TRY_COMPILE([#include <sys/time.h>],
-      [struct timezone tz;],
-      gfor_cv_struct_timezone=yes, gfor_cv_struct_timezone=no)])
-    if test $gfor_cv_struct_timezone = yes; then
-      dnl It may be that we can't call gettimeofday with a non-null pointer.
-      dnl In that case we'll lie about struct timezone.
-      AC_TRY_RUN([
-#ifdef TIME_WITH_SYS_TIME
-#include <sys/time.h>
-#include <time.h>
-#else
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#else
-#include <time.h>
-#endif
-#endif
-main ()
-{
-  struct timeval time;
-  struct timezone dummy;
-  if (gettimeofday (&time, &dummy))
-    exit (1);
-  else
-    exit (0);
-}],
-        [gfor_have_struct_timezone=yes], [gfor_have_struct_timezone=no],
-        [gfor_have_struct_timezone=yes])
-      if test $gfor_have_struct_timezone = yes; then
-        AC_DEFINE(HAVE_TIMEZONE, 1, [Do we have struct timezone])
-      fi
-    fi
-    AC_REQUIRE([AC_HEADER_TIME])
-    AC_CACHE_CHECK([whether gettimeofday can accept two arguments],
-      emacs_cv_gettimeofday_two_arguments,
-      [AC_TRY_LINK([
-#ifdef TIME_WITH_SYS_TIME
-#include <sys/time.h>
-#include <time.h>
-#else
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#else
-#include <time.h>
-#endif
-#endif
-      ],
-      [
-      struct timeval time;
-#ifdef HAVE_TIMEZONE
-      struct timezone dummy;
-#define DUMMY &dummy
-#else
-#define DUMMY NULL
-#endif
-      gettimeofday (&time, DUMMY);],
-      emacs_cv_gettimeofday_two_arguments=yes,
-      emacs_cv_gettimeofday_two_arguments=no)])
-    if test $emacs_cv_gettimeofday_two_arguments = no; then
-      AC_DEFINE(GETTIMEOFDAY_ONE_ARGUMENT, 1,
-        [Does gettimeofday take a single argument])
-    fi
-  fi])
-
 sinclude(../libtool.m4)
 dnl The lines below arrange for aclocal not to bring an installed
 dnl libtool.m4 into aclocal.m4, while still arranging for automake to
Index: libgfortran/intrinsics/date_and_time.c
===================================================================
--- libgfortran/intrinsics/date_and_time.c	(Revision 119707)
+++ libgfortran/intrinsics/date_and_time.c	(Arbeitskopie)
@@ -126,8 +126,6 @@
 
    TODO :
    - Check year boundaries.
-   - There is no STDC/POSIX way to get VALUES(8).  A GNUish way may
-     be to use ftime.
 */
 #define DATE_LEN 8
 #define TIME_LEN 10   
@@ -154,7 +152,25 @@
   struct tm local_time;
   struct tm UTC_time;
 
+#if HAVE_GETTIMEOFDAY
+  {
+    struct timeval tp;
+
+    if (!gettimeofday (&tp, NULL))
+      {
+         lt = tp.tv_sec;
+         values[7] = tp.tv_usec / 1000;
+      }
+    else
+      {
+         lt = time (NULL);
+         values[7] = 0;
+      }
+  }
+#else
   lt = time (NULL);
+  values[7] = 0;
+#endif /* HAVE_GETTIMEOFDAY */
 
   if (lt != (time_t) -1)
     {
@@ -171,32 +187,7 @@
       values[4] = local_time.tm_hour;
       values[5] = local_time.tm_min;
       values[6] = local_time.tm_sec;
-      values[7] = 0;
 
-#if HAVE_GETTIMEOFDAY
-      {
-	struct timeval tp;
-#  if GETTIMEOFDAY_ONE_ARGUMENT
-	if (!gettimeofday (&tp))
-#  else
-#    if HAVE_STRUCT_TIMEZONE
-	struct timezone tzp;
-
-      /* Some systems such as HP-UX, do have struct timezone, but
-	 gettimeofday takes void* as the 2nd arg.  However, the
-	 effect of passing anything other than a null pointer is
-	 unspecified on HP-UX.  Configure checks if gettimeofday
-	 actually fails with a non-NULL arg and pretends that
-	 struct timezone is missing if it does fail.  */
-	if (!gettimeofday (&tp, &tzp))
-#    else
-	if (!gettimeofday (&tp, (void *) 0))
-#    endif /* HAVE_STRUCT_TIMEZONE  */
-#  endif /* GETTIMEOFDAY_ONE_ARGUMENT  */
-	values[7] = tp.tv_usec / 1000;
-      }
-#endif /* HAVE_GETTIMEOFDAY */
-
 #if HAVE_SNPRINTF
       if (__date)
 	snprintf (date, DATE_LEN + 1, "%04d%02d%02d",

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]