libstdc++: Add full steady_clock support to shared_timed_mutex
authorMike Crowe <mac@mcrowe.com>
Mon, 2 Dec 2019 16:23:10 +0000 (16:23 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 2 Dec 2019 16:23:10 +0000 (16:23 +0000)
The pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock functions
were added to glibc in v2.30. They have also been added to Android
Bionic. If these functions are available in the C library then they can
be used to implement shared_timed_mutex::try_lock_until,
shared_timed_mutex::try_lock_for,
shared_timed_mutex::try_lock_shared_until and
shared_timed_mutex::try_lock_shared_for so that they are no longer
unaffected by the system clock being warped. (This is the shared_mutex
equivalent of PR libstdc++/78237 for mutex.)

If the new functions are available then steady_clock is deemed to be the
"best" clock available which means that it is used for the relative
try_lock_for calls and absolute try_lock_until calls using steady_clock
and user-defined clocks. It's not possible to have
_GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK defined without
_GLIBCXX_USE_PTHREAD_RWLOCK_T, so the requirement that the clock be the
same as condition_variable is maintained. Calls explicitly using
system_clock (aka high_resolution_clock) continue to use CLOCK_REALTIME
via the old pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock
functions.

If the new functions are not available then system_clock is deemed to be
the "best" clock available which means that the previous suboptimal
behaviour remains.

Additionally, the user-defined clock used with
shared_timed_mutex::try_lock_for and shared_mutex::try_lock_shared_for
may have higher precision than __clock_t. We may need to round the
duration up to ensure that the timeout is long enough. (See
__timed_mutex_impl::_M_try_lock_for)

2019-12-02  Mike Crowe  <mac@mcrowe.com>

Add full steady_clock support to shared_timed_mutex
* acinclude.m4 (GLIBCXX_CHECK_PTHREAD_RWLOCK_CLOCKLOCK): Define
to check for the presence of both pthread_rwlock_clockrdlock and
pthread_rwlock_clockwrlock.
* config.h.in: Regenerate.
* configure.ac: Call GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK.
* configure: Regenerate.
* include/std/shared_mutex (shared_timed_mutex): Define __clock_t as
the best clock to use for relative waits.
(shared_timed_mutex::try_lock_for) Round up wait duration if necessary.
(shared_timed_mutex::try_lock_shared_for): Likewise.
(shared_timed_mutex::try_lock_until): Use existing try_lock_until
implementation for system_clock (which matches __clock_t when
_GLIBCCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK is not defined). Add new
overload for steady_clock that uses pthread_rwlock_clockwrlock if it
is available. Simplify overload for non-standard clock to just call
try_lock_for with a relative timeout.
(shared_timed_mutex::try_lock_shared_until): Likewise.

From-SVN: r278903

libstdc++-v3/ChangeLog
libstdc++-v3/acinclude.m4
libstdc++-v3/config.h.in
libstdc++-v3/configure
libstdc++-v3/configure.ac
libstdc++-v3/include/std/shared_mutex

index a67fb0c23ea1fd6ef603094123751f167c6e825c..93331fb529a054fe65ff845887f050f6bb70ca7a 100644 (file)
@@ -1,5 +1,24 @@
 2019-12-02  Mike Crowe  <mac@mcrowe.com>
 
+       Add full steady_clock support to shared_timed_mutex
+       * acinclude.m4 (GLIBCXX_CHECK_PTHREAD_RWLOCK_CLOCKLOCK): Define
+       to check for the presence of both pthread_rwlock_clockrdlock and
+       pthread_rwlock_clockwrlock.
+       * config.h.in: Regenerate.
+       * configure.ac: Call GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK.
+       * configure: Regenerate.
+       * include/std/shared_mutex (shared_timed_mutex): Define __clock_t as
+       the best clock to use for relative waits.
+       (shared_timed_mutex::try_lock_for) Round up wait duration if necessary.
+       (shared_timed_mutex::try_lock_shared_for): Likewise.
+       (shared_timed_mutex::try_lock_until): Use existing try_lock_until
+       implementation for system_clock (which matches __clock_t when
+       _GLIBCCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK is not defined). Add new
+       overload for steady_clock that uses pthread_rwlock_clockwrlock if it
+       is available. Simplify overload for non-standard clock to just call
+       try_lock_for with a relative timeout.
+       (shared_timed_mutex::try_lock_shared_until): Likewise.
+
        PR libstdc++/91906 Fix timed_mutex::try_lock_until on arbitrary clock
        * include/std/mutex (__timed_mutex_impl::_M_try_lock_until): Loop
        until the absolute timeout time is reached as measured against the
index 7e685bb2f898171d56902a5381b5e0768cc47736..016b0c583d050804af5d7192ef9f81dac57b0522 100644 (file)
@@ -4258,6 +4258,39 @@ AC_DEFUN([GLIBCXX_CHECK_PTHREAD_MUTEX_CLOCKLOCK], [
   AC_LANG_RESTORE
 ])
 
+dnl
+dnl Check whether pthread_mutex_clocklock is available in <pthread.h> for std::timed_mutex to use,
+dnl and define _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK.
+dnl
+AC_DEFUN([GLIBCXX_CHECK_PTHREAD_RWLOCK_CLOCKLOCK], [
+
+  AC_LANG_SAVE
+  AC_LANG_CPLUSPLUS
+  ac_save_CXXFLAGS="$CXXFLAGS"
+  CXXFLAGS="$CXXFLAGS -fno-exceptions"
+  ac_save_LIBS="$LIBS"
+  LIBS="$LIBS -lpthread"
+
+  AC_MSG_CHECKING([for pthread_rwlock_clockrdlock, pthread_wlock_clockwrlock])
+  AC_CACHE_VAL(glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK, [
+    GCC_TRY_COMPILE_OR_LINK(
+      [#include <pthread.h>],
+      [pthread_rwlock_t rwl; struct timespec ts;]
+      [int n = pthread_rwlock_clockrdlock(&rwl, CLOCK_REALTIME, &ts);]
+      [int m = pthread_rwlock_clockwrlock(&rwl, CLOCK_REALTIME, &ts);],
+      [glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK=yes],
+      [glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK=no])
+  ])
+  if test $glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK = yes; then
+    AC_DEFINE(_GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK, 1, [Define if pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock are available in <pthread.h>.])
+  fi
+  AC_MSG_RESULT($glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK)
+
+  CXXFLAGS="$ac_save_CXXFLAGS"
+  LIBS="$ac_save_LIBS"
+  AC_LANG_RESTORE
+])
+
 dnl
 dnl Check whether sysctl is available in <pthread.h>, and define _GLIBCXX_USE_SYSCTL_HW_NCPU.
 dnl
index 3d515347396b5fb87ae506d2d161c19ca09c733a..8940e0c7acd1b9aea7bf1ea40727899c69d322fb 100644 (file)
 /* Define if pthread_mutex_clocklock is available in <pthread.h>. */
 #undef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
 
+/* Define if pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock are
+   available in <pthread.h>. */
+#undef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
+
 /* Define if POSIX read/write locks are available in <gthr.h>. */
 #undef _GLIBCXX_USE_PTHREAD_RWLOCK_T
 
index a439bb86e6eed48b1d4bf46ad7f23ec922b44a8f..b7242157107f8009de213960b168f8b20c2ba0ef 100755 (executable)
@@ -21963,6 +21963,93 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
 
 
 
+# For pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock
+
+
+
+  ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+  ac_save_CXXFLAGS="$CXXFLAGS"
+  CXXFLAGS="$CXXFLAGS -fno-exceptions"
+  ac_save_LIBS="$LIBS"
+  LIBS="$LIBS -lpthread"
+
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for pthread_rwlock_clockrdlock, pthread_wlock_clockwrlock" >&5
+$as_echo_n "checking for pthread_rwlock_clockrdlock, pthread_wlock_clockwrlock... " >&6; }
+  if ${glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+
+    if test x$gcc_no_link = xyes; then
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <pthread.h>
+int
+main ()
+{
+pthread_rwlock_t rwl; struct timespec ts;
+      int n = pthread_rwlock_clockrdlock(&rwl, CLOCK_REALTIME, &ts);
+      int m = pthread_rwlock_clockwrlock(&rwl, CLOCK_REALTIME, &ts);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+  glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK=yes
+else
+  glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+else
+  if test x$gcc_no_link = xyes; then
+  as_fn_error $? "Link tests are not allowed after GCC_NO_EXECUTABLES." "$LINENO" 5
+fi
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <pthread.h>
+int
+main ()
+{
+pthread_rwlock_t rwl; struct timespec ts;
+      int n = pthread_rwlock_clockrdlock(&rwl, CLOCK_REALTIME, &ts);
+      int m = pthread_rwlock_clockwrlock(&rwl, CLOCK_REALTIME, &ts);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_link "$LINENO"; then :
+  glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK=yes
+else
+  glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+fi
+
+fi
+
+  if test $glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK = yes; then
+
+$as_echo "#define _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK 1" >>confdefs.h
+
+  fi
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK" >&5
+$as_echo "$glibcxx_cv_PTHREAD_RWLOCK_CLOCKLOCK" >&6; }
+
+  CXXFLAGS="$ac_save_CXXFLAGS"
+  LIBS="$ac_save_LIBS"
+  ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+
+
 
   ac_fn_c_check_header_mongrel "$LINENO" "locale.h" "ac_cv_header_locale_h" "$ac_includes_default"
 if test "x$ac_cv_header_locale_h" = xyes; then :
index c1ecc6705192e0334d8d348d0c08559ae901f08c..699e55fd829d40b464bf62e920418eec5da6356d 100644 (file)
@@ -228,6 +228,9 @@ GLIBCXX_CHECK_PTHREAD_COND_CLOCKWAIT
 # For pthread_mutex_clocklock
 GLIBCXX_CHECK_PTHREAD_MUTEX_CLOCKLOCK
 
+# For pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock
+GLIBCXX_CHECK_PTHREAD_RWLOCK_CLOCKLOCK
+
 AC_LC_MESSAGES
 
 # For hardware_concurrency
index ffd93fb1e15447763cf23c3edb2690d7a4921f8a..cfe2ec078e1d1527b7a8b603c68a48d86664d0d8 100644 (file)
@@ -450,7 +450,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     using _Base = __shared_timed_mutex_base;
 
     // Must use the same clock as condition_variable for __shared_mutex_cv.
-    typedef chrono::system_clock       __clock_t;
+#ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
+    using __clock_t = chrono::steady_clock;
+#else
+    using __clock_t = chrono::system_clock;
+#endif
 
   public:
     shared_timed_mutex() = default;
@@ -467,9 +471,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     template<typename _Rep, typename _Period>
       bool
-      try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
+      try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
       {
-       return try_lock_until(__clock_t::now() + __rel_time);
+       auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime);
+       if (ratio_greater<__clock_t::period, _Period>())
+         ++__rt;
+       return try_lock_until(__clock_t::now() + __rt);
       }
 
     // Shared ownership
@@ -480,9 +487,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     template<typename _Rep, typename _Period>
       bool
-      try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
+      try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rtime)
       {
-       return try_lock_shared_until(__clock_t::now() + __rel_time);
+       auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime);
+       if (ratio_greater<__clock_t::period, _Period>())
+         ++__rt;
+       return try_lock_shared_until(__clock_t::now() + __rt);
       }
 
 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
@@ -491,7 +501,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     template<typename _Duration>
       bool
-      try_lock_until(const chrono::time_point<__clock_t, _Duration>& __atime)
+      try_lock_until(const chrono::time_point<chrono::system_clock,
+                    _Duration>& __atime)
       {
        auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
        auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
@@ -512,23 +523,47 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return true;
       }
 
+#ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
+    template<typename _Duration>
+      bool
+      try_lock_until(const chrono::time_point<chrono::steady_clock,
+                  _Duration>& __atime)
+      {
+       auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
+       auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
+
+       __gthread_time_t __ts =
+         {
+           static_cast<std::time_t>(__s.time_since_epoch().count()),
+           static_cast<long>(__ns.count())
+         };
+
+       int __ret = pthread_rwlock_clockwrlock(&_M_rwlock, CLOCK_MONOTONIC,
+                                              &__ts);
+       // On self-deadlock, we just fail to acquire the lock.  Technically,
+       // the program violated the precondition.
+       if (__ret == ETIMEDOUT || __ret == EDEADLK)
+         return false;
+       // Errors not handled: EINVAL
+       __glibcxx_assert(__ret == 0);
+       return true;
+      }
+#endif
+
     template<typename _Clock, typename _Duration>
       bool
-      try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
+      try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
       {
-       // DR 887 - Sync unknown clock to known clock.
-       const typename _Clock::time_point __c_entry = _Clock::now();
-       const __clock_t::time_point __s_entry = __clock_t::now();
-       const auto __delta = __abs_time - __c_entry;
-       const auto __s_atime = __s_entry + __delta;
-       return try_lock_until(__s_atime);
+       typename _Clock::time_point __now = _Clock::now();
+       auto __rtime = __atime - __now;
+       return try_lock_for(__rtime);
       }
 
     // Shared ownership
 
     template<typename _Duration>
       bool
-      try_lock_shared_until(const chrono::time_point<__clock_t,
+      try_lock_shared_until(const chrono::time_point<chrono::system_clock,
                            _Duration>& __atime)
       {
        auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
@@ -564,17 +599,41 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return true;
       }
 
+#ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
+    template<typename _Duration>
+      bool
+      try_lock_shared_until(const chrono::time_point<chrono::steady_clock,
+                           _Duration>& __atime)
+      {
+       auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
+       auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
+
+       __gthread_time_t __ts =
+         {
+           static_cast<std::time_t>(__s.time_since_epoch().count()),
+           static_cast<long>(__ns.count())
+         };
+
+       int __ret = pthread_rwlock_clockrdlock(&_M_rwlock, CLOCK_MONOTONIC,
+                                              &__ts);
+       // On self-deadlock, we just fail to acquire the lock.  Technically,
+       // the program violated the precondition.
+       if (__ret == ETIMEDOUT || __ret == EDEADLK)
+         return false;
+       // Errors not handled: EINVAL
+       __glibcxx_assert(__ret == 0);
+       return true;
+      }
+#endif
+
     template<typename _Clock, typename _Duration>
       bool
       try_lock_shared_until(const chrono::time_point<_Clock,
-                                                    _Duration>& __abs_time)
+                                                    _Duration>& __atime)
       {
-       // DR 887 - Sync unknown clock to known clock.
-       const typename _Clock::time_point __c_entry = _Clock::now();
-       const __clock_t::time_point __s_entry = __clock_t::now();
-       const auto __delta = __abs_time - __c_entry;
-       const auto __s_atime = __s_entry + __delta;
-       return try_lock_shared_until(__s_atime);
+       typename _Clock::time_point __now = _Clock::now();
+       auto __rtime = __atime - __now;
+       return try_lock_shared_for(__rtime);
       }
 
 #else // ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)