From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 59814 invoked by alias); 15 Oct 2019 17:57:59 -0000 Mailing-List: contact libstdc++-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libstdc++-owner@gcc.gnu.org Received: (qmail 59754 invoked by uid 89); 15 Oct 2019 17:57:59 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-23.5 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.1 spammy=Must, CC, Simplify X-HELO: avasout01.plus.net Received: from avasout01.plus.net (HELO avasout01.plus.net) (84.93.230.227) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 15 Oct 2019 17:57:55 +0000 Received: from deneb ([80.229.24.9]) by smtp with ESMTP id KR4wiu0mIbMsOKR4xiVwbT; Tue, 15 Oct 2019 18:57:55 +0100 X-Clacks-Overhead: "GNU Terry Pratchett" X-CM-Score: 0.00 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mcrowe.com; s=20191005; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=c2qarUiZJnYE5CEY0ipXQ22Psu6tu4CT3ZPx9m59M8s=; b=KtKmhu/lrXHwPYlziEJ4YDZtcU m3cY/HgGRpzzGxMiIZ7slOWyFH9nN++vR47me48WaQEQG57KWSbQxnrnX9yHVpzkPgEnHebMj/ewH hsO9Fr/AwKh9PHPBd+EthxZLodcPRveP7CNNPWMWJwNRW7L1RFD8DjpTY1opmVnK263B0lNtlqXPD QW7Zp82lwdzoOPTpY0BonjzNHKaVrDAT1KjLZ0NWtdBfO9KmMM2/k6O+aI6oPAPQpMqZbQTbn2fKA 9EsQu9mFk3dsH19N3DXqd+VIGFIbbXxjpAZ+PbhaRKHWM/mpdPuaFyuQpaugjZSfR6GwyqOX78J5Q O815MlVw==; Received: from mac by deneb with local (Exim 4.92) (envelope-from ) id 1iKR4m-0003rr-F1; Tue, 15 Oct 2019 18:57:40 +0100 From: Mike Crowe To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Mike Crowe Subject: [PATCH v2 09/11] libstdc++ shared_mutex: Add full steady_clock support to shared_timed_mutex Date: Tue, 15 Oct 2019 17:58:00 -0000 Message-Id: <218b31eaa6de223a34c752d768313ba19e41ec48.1571162241.git-series.mac@mcrowe.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2019-10/txt/msg00063.txt.bz2 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. * libstdc++-v3/acinclude.m4: Define GLIBCXX_CHECK_PTHREAD_RWLOCK_CLOCKLOCK to check for the presence of both pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock. * config.h.in: Add _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK define. * configure.ac: Call GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK. * configure: Add generated part to detect pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock functions. * include/std/shared_mutex (shared_timed_mutex): Define __clock_t as the best clock to use for relative waits. (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. (try_lock_shared_until): Likewise. --- libstdc++-v3/acinclude.m4 | 33 +++++++++++- libstdc++-v3/config.h.in | 4 +- libstdc++-v3/configure | 88 ++++++++++++++++++++++++++++- libstdc++-v3/configure.ac | 3 +- libstdc++-v3/include/std/shared_mutex | 87 ++++++++++++++++++++++------ 5 files changed, 198 insertions(+), 17 deletions(-) diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index 9ecfa96..d860dbe 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -4259,6 +4259,39 @@ AC_DEFUN([GLIBCXX_CHECK_PTHREAD_MUTEX_CLOCKLOCK], [ ]) dnl +dnl Check whether pthread_mutex_clocklock is available in 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_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 .]) + 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 , and define _GLIBCXX_USE_SYSCTL_HW_NCPU. dnl AC_DEFUN([GLIBCXX_CHECK_SYSCTL_HW_NCPU], [ diff --git a/libstdc++-v3/config.h.in b/libstdc++-v3/config.h.in index 3d51534..8940e0c 100644 --- a/libstdc++-v3/config.h.in +++ b/libstdc++-v3/config.h.in @@ -1005,6 +1005,10 @@ /* Define if pthread_mutex_clocklock is available in . */ #undef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK +/* Define if pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock are + available in . */ +#undef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK + /* Define if POSIX read/write locks are available in . */ #undef _GLIBCXX_USE_PTHREAD_RWLOCK_T diff --git a/libstdc++-v3/configure b/libstdc++-v3/configure index bf0f50a..edb565f 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -21963,6 +21963,94 @@ 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 +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 +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 : diff --git a/libstdc++-v3/configure.ac b/libstdc++-v3/configure.ac index c1ecc67..699e55f 100644 --- a/libstdc++-v3/configure.ac +++ b/libstdc++-v3/configure.ac @@ -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 diff --git a/libstdc++-v3/include/std/shared_mutex b/libstdc++-v3/include/std/shared_mutex index ffd93fb..b637bdc 100644 --- a/libstdc++-v3/include/std/shared_mutex +++ b/libstdc++-v3/include/std/shared_mutex @@ -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; @@ -491,7 +495,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template bool - try_lock_until(const chrono::time_point<__clock_t, _Duration>& __atime) + try_lock_until(const chrono::time_point& __atime) { auto __s = chrono::time_point_cast(__atime); auto __ns = chrono::duration_cast(__atime - __s); @@ -512,23 +517,47 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return true; } +#ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK + template + bool + try_lock_until(const chrono::time_point& __atime) + { + auto __s = chrono::time_point_cast(__atime); + auto __ns = chrono::duration_cast(__atime - __s); + + __gthread_time_t __ts = + { + static_cast(__s.time_since_epoch().count()), + static_cast(__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 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 bool - try_lock_shared_until(const chrono::time_point<__clock_t, + try_lock_shared_until(const chrono::time_point& __atime) { auto __s = chrono::time_point_cast(__atime); @@ -564,17 +593,41 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return true; } +#ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK + template + bool + try_lock_shared_until(const chrono::time_point& __atime) + { + auto __s = chrono::time_point_cast(__atime); + auto __ns = chrono::duration_cast(__atime - __s); + + __gthread_time_t __ts = + { + static_cast(__s.time_since_epoch().count()), + static_cast(__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 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) -- git-series 0.9.1