[patch] Default to --enable-libstdcxx-time=auto

Jakub Jelinek jakub@redhat.com
Thu May 23 10:28:00 GMT 2013


On Wed, May 22, 2013 at 02:35:40PM +0200, Jakub Jelinek wrote:
> non-steady clock instead.  Or, have you also considered just using
> for this routine
> #if _GLIBCXX_HAS_SYS_SYSCALL_H
> #include <sys/syscall.h>
> #endif
> 
> #if defined (SYS_clock_gettime) && defined (CLOCK_MONOTONIC)
> syscall (SYS_clock_gettime, CLOCK_MONOTONIC, &tp);
> #endif
> if clock_gettime isn't available, at least on Linux?
> The implementation seems to ignore ENOSYS from clock_gettime, so ignoring it
> even here wouldn't make it worse.

I mean something like completely untested following patch, then it would
be pretty much enabled for all non-prehistoric Linux builds (there is a risk
of it returning garbage on 2.4.x and earlier kernels, if you compile it on
something that defines __NR_clock_gettime in their headers, but the exact
same risk is if you do the same with --enable-libstdcxx-time=rt
(clock_gettime wrapper in glibc will return -1/ENOSYS in that case, so will
the syscall, but chrono.cc seems to ignore that return value)).
2.6+ kernels (2004-ish and later or so) should support CLOCK_MONOTONIC just
fine.  Of course, there is a possibility of fallback, at least for the
clock_gettime/syscall CLOCK_RUNTIME or gettimeofday, if they fail, fall
through into the time case, and for CLOCK_MONOTONIC perhaps just lie and
return time as well, shouldn't really affect almost anybody.

Still, the ABI question is there, would we want to apply to 4.8.1 (can we
get agreement on that RSN, this is pretty much the only blocker for 4.8.1
rc2 right now) and, would we export that symbol as @@GLIBCXX_3.4.18 (with
all trunk @@GLIBCXX_3.4.18 symbols moved to 3.4.19) and add @GLIBCXX_3.4.17
alias for backwards compatibility with those that configured with
--enable-libstdcxx-time=rt ?

--- libstdc++-v3/src/c++11/chrono.cc.jj	2013-03-16 08:07:57.000000000 +0100
+++ libstdc++-v3/src/c++11/chrono.cc	2013-05-23 12:08:04.165686015 +0200
@@ -32,6 +32,9 @@
      defined(_GLIBCXX_USE_GETTIMEOFDAY)
 #include <sys/time.h>
 #endif
+#ifdef _GLIBCXX_USE_CLOCK_MONOTONIC_SYSCALL
+#include <sys/syscall.h>
+#endif
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -47,7 +50,11 @@ namespace std _GLIBCXX_VISIBILITY(defaul
 #ifdef _GLIBCXX_USE_CLOCK_REALTIME
       timespec tp;
       // -EINVAL, -EFAULT
+#ifdef _GLIBCXX_USE_CLOCK_REALTIME_SYSCALL
+      syscall(SYS_clock_gettime, CLOCK_REALTIME, &tp);
+#else
       clock_gettime(CLOCK_REALTIME, &tp);
+#endif
       return time_point(duration(chrono::seconds(tp.tv_sec)
 				 + chrono::nanoseconds(tp.tv_nsec)));
 #elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
@@ -70,7 +77,11 @@ namespace std _GLIBCXX_VISIBILITY(defaul
     {
       timespec tp;
       // -EINVAL, -EFAULT
+#ifdef _GLIBCXX_USE_CLOCK_REALTIME_SYSCALL
+      syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &tp);
+#else
       clock_gettime(CLOCK_MONOTONIC, &tp);
+#endif
       return time_point(duration(chrono::seconds(tp.tv_sec)
 				 + chrono::nanoseconds(tp.tv_nsec)));
     }
--- libstdc++-v3/acinclude.m4.jj	2013-04-10 08:32:08.000000000 +0200
+++ libstdc++-v3/acinclude.m4	2013-05-23 12:03:29.626014601 +0200
@@ -1274,6 +1274,28 @@ AC_DEFUN([GLIBCXX_ENABLE_LIBSTDCXX_TIME]
     fi
   fi
 
+  if test x"$ac_has_clock_monotonic" != x"yes"; then
+    AC_MSG_CHECKING([for clock_gettime syscall])
+    AC_TRY_COMPILE(
+      [#include <unistd.h>
+       #include <time.h>
+       #include <sys/syscall.h>
+      ],
+      [#if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK)
+	timespec tp;
+       #endif
+       syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &tp);
+       syscall(SYS_clock_gettime, CLOCK_REALTIME, &tp);
+      ], [ac_has_clock_monotonic_syscall=yes], [ac_has_clock_monotonic_syscall=no])
+    AC_MSG_RESULT($ac_has_clock_monotonic_syscall)
+    if test x"$ac_has_clock_monotonic_syscall" = x"yes"; then
+      AC_DEFINE(_GLIBCXX_USE_CLOCK_MONOTONIC_SYSCALL, 1,
+      [ Defined if clock_gettime syscall has monotonic clock support. ])
+      ac_has_clock_monotonic=yes
+      ac_has_clock_realtime=yes
+    fi
+  fi
+
   if test x"$ac_has_clock_monotonic" = x"yes"; then
     AC_DEFINE(_GLIBCXX_USE_CLOCK_MONOTONIC, 1,
       [ Defined if clock_gettime has monotonic clock support. ])


	Jakub



More information about the Gcc-patches mailing list