[PATCH] gethrxtime: don’t use nanouptime, microuptime

Paul Eggert <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Message-ID <[email protected]>
* lib/gethrxtime.c (gethrxtime): On platforms with
CLOCK_MONOTONIC, try it first.  If it fails, just fall back on
CLOCK_REALTIME as that’s what current_timespec would do anyway.
On platforms lacking CLOCK_MONOTONIC, just use current_timespec.
* m4/gethrxtime.m4 (gl_GETHRXTIME): Don’t check for microuptime or
nanouptime.  They aren’t exposed to user space by BSD kernels now,
and it’s not clear that they ever were.
---
 ChangeLog        | 11 +++++++++++
 lib/gethrxtime.c | 40 +++++++++-------------------------------
 m4/gethrxtime.m4 |  6 ++----
 3 files changed, 22 insertions(+), 35 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6fd9e8ce38..6ee89378ea 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2026-06-20  Paul Eggert  <[email protected]>
+
+	gethrxtime: don’t use nanouptime, microuptime
+	* lib/gethrxtime.c (gethrxtime): On platforms with
+	CLOCK_MONOTONIC, try it first.  If it fails, just fall back on
+	CLOCK_REALTIME as that’s what current_timespec would do anyway.
+	On platforms lacking CLOCK_MONOTONIC, just use current_timespec.
+	* m4/gethrxtime.m4 (gl_GETHRXTIME): Don’t check for microuptime or
+	nanouptime.  They aren’t exposed to user space by BSD kernels now,
+	and it’s not clear that they ever were.
+
 2026-06-19  Paul Eggert  <[email protected]>
 
 	fstatat: don’t mess with CFLAGS
diff --git a/lib/gethrxtime.c b/lib/gethrxtime.c
index e1580b3d72..72678b6f65 100644
--- a/lib/gethrxtime.c
+++ b/lib/gethrxtime.c
@@ -34,39 +34,17 @@
 xtime_t
 gethrxtime (void)
 {
-# if HAVE_NANOUPTIME
-  {
-    struct timespec ts;
-    nanouptime (&ts);
-    return xtime_make (ts.tv_sec, ts.tv_nsec);
-  }
+  struct timespec ts;
+# if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
+  /* If available, use a monotonically increasing clock.
+     Otherwise fall back on CLOCK_REALTIME as that is
+     what current_timespec would do anyway.  */
+  if (clock_gettime (CLOCK_MONOTONIC, &ts) < 0)
+    clock_gettime (CLOCK_REALTIME, &ts);
 # else
-
-#  if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
-  {
-    struct timespec ts;
-    if (clock_gettime (CLOCK_MONOTONIC, &ts) == 0)
-      return xtime_make (ts.tv_sec, ts.tv_nsec);
-  }
-#  endif
-
-#  if HAVE_MICROUPTIME
-  {
-    struct timeval tv;
-    microuptime (&tv);
-    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.  */
-  {
-    struct timespec ts;
-    gettime (&ts);
-    return xtime_make (ts.tv_sec, ts.tv_nsec);
-  }
-#  endif
+  ts = current_timespec ();
 # endif
+  return xtime_make (ts.tv_sec, ts.tv_nsec);
 }
 
 #endif
diff --git a/m4/gethrxtime.m4 b/m4/gethrxtime.m4
index e141cfaef5..918437f8df 100644
--- a/m4/gethrxtime.m4
+++ b/m4/gethrxtime.m4
@@ -20,10 +20,8 @@ AC_DEFUN([gl_GETHRXTIME],
     dnl Find libraries needed to link lib/gethrxtime.c.
     AC_REQUIRE([gl_CLOCK_TIME])
     AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
-    AC_CHECK_FUNCS_ONCE([microuptime nanouptime])
-    if test $ac_cv_func_nanouptime != yes \
-       && { test $ac_cv_have_decl_gethrtime = no \
-            || test $gl_cv_arithmetic_hrtime_t = no; }; then
+    if { test $ac_cv_have_decl_gethrtime = no \
+         || test $gl_cv_arithmetic_hrtime_t = no; }; then
       AC_CACHE_CHECK([whether CLOCK_MONOTONIC or CLOCK_REALTIME is defined],
         [gl_cv_have_clock_gettime_macro],
         [AC_EGREP_CPP([have_clock_gettime_macro],
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.