bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gethrxtime: fall back on gettime?


From: Paul Eggert
Subject: Re: gethrxtime: fall back on gettime?
Date: Thu, 10 Nov 2005 12:25:15 -0800
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

Jim Meyering <address@hidden> writes:

> What do you think of making gethrxtime fall back on gettime?

Yes, that makes sense to me.  I installed the patch below.  This
also fixes the comments to match the code.

> While we're on the subject, how about removing gettime's use
> of time?  If there is a system lacking all of the preceding
> functions, then we should find/use a function it does provide
> that has some sub-second precision rather than using `time (NULL)',
> which has none.  Then we could say that the function is guaranteed
> to provide at least nominal sub-second precision.

Hmm, my kneejerk reaction is that there are no guarantees with clocks.
Even if we successfully invoke clock_gettime or gettimeofday, there's
still no guarantee that the clock has subsecond precision, as it could
be a gettimeofday emulator running atop a clock with 1-second
resolution.

Also, if we insist on not calling time(), that means we'd have to
delve into Microsoft's _ftime in order to port to mingw, right?  I'd
rather avoid _ftime if I could.

Index: ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/ChangeLog,v
retrieving revision 1.444
diff -p -u -r1.444 ChangeLog
--- ChangeLog   4 Nov 2005 18:25:19 -0000       1.444
+++ ChangeLog   10 Nov 2005 20:18:40 -0000
@@ -1,3 +1,7 @@
+2005-11-10  Paul Eggert  <address@hidden>
+
+       * modules/gethrxtime (Depends-on): Add gettime.
+
 2005-11-04  Bruno Haible  <address@hidden>
 
        * gnulib-tool: Implement --update mode.
Index: modules/gethrxtime
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/gethrxtime,v
retrieving revision 1.2
diff -p -u -r1.2 gethrxtime
--- modules/gethrxtime  23 Mar 2005 11:51:30 -0000      1.2
+++ modules/gethrxtime  10 Nov 2005 20:18:40 -0000
@@ -10,6 +10,7 @@ m4/clock_time.m4
 m4/longlong.m4
 
 Depends-on:
+gettime
 extensions
 
 configure.ac:
Index: lib/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/ChangeLog,v
retrieving revision 1.1048
diff -p -u -r1.1048 ChangeLog
--- lib/ChangeLog       10 Nov 2005 02:19:29 -0000      1.1048
+++ lib/ChangeLog       10 Nov 2005 20:18:40 -0000
@@ -1,3 +1,12 @@
+2005-11-10  Paul Eggert  <address@hidden>
+
+       * gethrxtime.c: Include "timespec.h" rather than the sys/time / time
+       business.
+       (gethrxtime) [! (HAVE_NANOUPTIME
+       || (defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME)
+       || HAVE_MICROUPTIME)]: Fall back on gettime rather than rolling
+       our own approximation.
+
 2005-11-10  Simon Josefsson  <address@hidden>
 
        * readline.c: Remove EOL.
Index: lib/gethrxtime.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gethrxtime.c,v
retrieving revision 1.3
diff -p -u -r1.3 gethrxtime.c
--- lib/gethrxtime.c    15 May 2005 04:45:43 -0000      1.3
+++ lib/gethrxtime.c    10 Nov 2005 20:18:40 -0000
@@ -24,20 +24,12 @@
 
 #include "gethrxtime.h"
 
-#include <sys/types.h>
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
+#include "timespec.h"
 
-/* Get the time of a high-resolution clock, preferably one that is not
-   subject to resetting or drifting.  */
+/* Get the current time, as a count of the number of nanoseconds since
+   an arbitrary epoch (e.g., the system boot time).  Prefer a
+   high-resolution clock that is not subject to resetting or
+   drifting.  */
 
 xtime_t
 gethrxtime (void)
@@ -65,16 +57,14 @@ gethrxtime (void)
     return xtime_make (tv.tv_sec, 1000 * tv.tv_usec);
   }
 
+# else
   /* No monotonically increasing clocks are available; fall back on a
      clock that might jump backwards, since it's the best we can do.  */
-# elif HAVE_GETTIMEOFDAY && XTIME_PRECISION != 1
   {
-    struct timeval tv;
-    gettimeofday (&tv, NULL);
-    return xtime_make (tv.tv_sec, 1000 * tv.tv_usec);
+    struct timespec ts;
+    gettime (&ts);
+    return xtime_make (ts.tv_sec, ts.tv_nsec);
   }
-# else
-  return xtime_make (time (NULL), 0);
 # endif
 #endif
 }
Index: lib/gethrxtime.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gethrxtime.h,v
retrieving revision 1.3
diff -p -u -r1.3 gethrxtime.h
--- lib/gethrxtime.h    15 May 2005 04:45:43 -0000      1.3
+++ lib/gethrxtime.h    10 Nov 2005 20:18:40 -0000
@@ -24,8 +24,9 @@
 # include "xtime.h"
 
 /* Get the current time, as a count of the number of nanoseconds since
-   an arbitrary epoch (e.g., the system boot time).  This clock can't
-   be set, is always increasing, and is nearly linear.  */
+   an arbitrary epoch (e.g., the system boot time).  Prefer a
+   high-resolution clock that is not subject to resetting or
+   drifting.  */
 
 # if HAVE_ARITHMETIC_HRTIME_T && HAVE_DECL_GETHRTIME
 #  include <time.h>
Index: m4/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/ChangeLog,v
retrieving revision 1.757
diff -p -u -r1.757 ChangeLog
--- m4/ChangeLog        30 Oct 2005 23:06:58 -0000      1.757
+++ m4/ChangeLog        10 Nov 2005 20:18:40 -0000
@@ -1,3 +1,8 @@
+2005-11-10  Paul Eggert  <address@hidden>
+
+       * gethrxtime.m4 (gl_PREREQ_GETHRXTIME): Don't require AC_HEADER_TIME
+       or gettimeofday; no longer needed.
+
 2005-10-30  Paul Eggert  <address@hidden>
 
        * chdir-long.m4 (gl_FUNC_CHDIR_LONG): Revamp wording and local
Index: m4/gethrxtime.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/gethrxtime.m4,v
retrieving revision 1.1
diff -p -u -r1.1 gethrxtime.m4
--- m4/gethrxtime.m4    26 Feb 2005 08:18:27 -0000      1.1
+++ m4/gethrxtime.m4    10 Nov 2005 20:18:40 -0000
@@ -1,4 +1,4 @@
-# gethrxtime.m4 serial 2
+# gethrxtime.m4 serial 3
 dnl Copyright (C) 2005 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -50,10 +50,9 @@ AC_DEFUN([gl_XTIME],
 # Prerequisites of lib/gethrxtime.c.
 AC_DEFUN([gl_PREREQ_GETHRXTIME],
 [
-  AC_REQUIRE([AC_HEADER_TIME])
   AC_REQUIRE([gl_CLOCK_TIME])
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
-  AC_CHECK_FUNCS_ONCE(gettimeofday microuptime nanouptime)
+  AC_CHECK_FUNCS_ONCE(microuptime nanouptime)
 
   if test $ac_cv_func_nanouptime != yes; then
     LIB_GETHRXTIME=




reply via email to

[Prev in Thread] Current Thread [Next in Thread]