From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 60035 invoked by alias); 28 Sep 2019 08:45:44 -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 55355 invoked by uid 89); 28 Sep 2019 08:45:28 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.9 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 spammy=H*r:smtp X-HELO: avasout06.plus.net Received: from avasout06.plus.net (HELO avasout06.plus.net) (212.159.14.18) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 28 Sep 2019 08:45:14 +0000 Received: from deneb ([80.229.24.9]) by smtp with ESMTP id E8LfikKyxDGRHE8LhiiJK5; Sat, 28 Sep 2019 09:45:05 +0100 X-Clacks-Overhead: "GNU Terry Pratchett" X-CM-Score: 0.00 Received: from mac by deneb with local (Exim 4.92) (envelope-from ) id 1iE8Le-0006RJ-5O; Sat, 28 Sep 2019 09:45:02 +0100 From: Mike Crowe To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Mike Crowe Subject: [PATCH 05/11] PR libstdc++/78237 Add full steady_clock support to timed_mutex Date: Sat, 28 Sep 2019 08:45:00 -0000 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2019-09/txt/msg00111.txt.bz2 The pthread_mutex_clocklock function is available in glibc since the 2.30 release. If this function is available in the C library it can be used to fix PR libstdc++/78237 by supporting steady_clock properly with timed_mutex. This means that code using timed_mutex::try_lock_for or timed_mutex::wait_until with steady_clock is no longer subject to timing out early or potentially waiting for much longer if the system clock is warped at an inopportune moment. If pthread_mutex_clocklock is 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. Calls explicitly using system_clock (aka high_resolution_clock) continue to use CLOCK_REALTIME via __gthread_cond_timedwait. If pthread_mutex_clocklock is not available then system_clock is deemed to be the "best" clock available which means that the previous suboptimal behaviour remains. * acinclude.m4: Define GLIBCXX_CHECK_PTHREAD_MUTEX_CLOCKLOCK to detect presence of pthread_mutex_clocklock function. * config.h.in: Add _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK define. * configure.ac: Call GLIBCXX_CHECK_PTHREAD_MUTEX_CLOCKLOCK. * configure: Add generated part to detect pthread_mutex_clocklock function. * include/std/mutex (__timed_mutex_impl): Remove unnecessary __clock_t. (_M_try_lock_for) Use best clock to turn relative timeout into absolute timeout. (_M_try_lock_until) Keep existing implementation for system_clock. Add new implementation for steady_clock that calls _M_clocklock. Modify overload for user-defined clock to use a relative wait so that it automatically uses the best clock. * include/std/mutex (timed_mutex): If pthread_mutex_clocklock is available, add _M_clocklock method that calls it. * include/std/mutex (recursive_timed_mutex): Likewise. --- libstdc++-v3/acinclude.m4 | 31 +++++++++++++- libstdc++-v3/config.h.in | 3 +- libstdc++-v3/configure | 82 +++++++++++++++++++++++++++++++++++- libstdc++-v3/configure.ac | 3 +- libstdc++-v3/include/std/mutex | 49 +++++++++++++++++---- 5 files changed, 160 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index ad2cb01..9ecfa96 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -4228,6 +4228,37 @@ AC_DEFUN([GLIBCXX_CHECK_PTHREAD_COND_CLOCKWAIT], [ ]) 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_MUTEX_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_mutex_clocklock]) + AC_CACHE_VAL(glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK, [ + GCC_TRY_COMPILE_OR_LINK( + [#include ], + [pthread_mutex_t mutex; struct timespec ts; int n = pthread_mutex_clocklock(&mutex, CLOCK_REALTIME, &ts);], + [glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK=yes], + [glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK=no]) + ]) + if test $glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK = yes; then + AC_DEFINE(_GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK, 1, [Define if pthread_mutex_clocklock is available in .]) + fi + AC_MSG_RESULT($glibcxx_cv_PTHREAD_MUTEX_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 3d13402..9670176 100644 --- a/libstdc++-v3/config.h.in +++ b/libstdc++-v3/config.h.in @@ -994,6 +994,9 @@ /* Define if pthread_cond_clockwait is available in . */ #undef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT +/* Define if pthread_mutex_clocklock is available in . */ +#undef _GLIBCXX_USE_PTHREAD_MUTEX_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 e646c41..41df34c 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -21677,6 +21677,88 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu +# For pthread_mutex_clocklock + + + + 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_mutex_clocklock" >&5 +$as_echo_n "checking for pthread_mutex_clocklock... " >&6; } + if ${glibcxx_cv_PTHREAD_MUTEX_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_mutex_t mutex; struct timespec ts; int n = pthread_mutex_clocklock(&mutex, CLOCK_REALTIME, &ts); + ; + return 0; +} +_ACEOF +if ac_fn_cxx_try_compile "$LINENO"; then : + glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK=yes +else + glibcxx_cv_PTHREAD_MUTEX_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_mutex_t mutex; struct timespec ts; int n = pthread_mutex_clocklock(&mutex, CLOCK_REALTIME, &ts); + ; + return 0; +} +_ACEOF +if ac_fn_cxx_try_link "$LINENO"; then : + glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK=yes +else + glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK=no +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +fi + +fi + + if test $glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK = yes; then + +$as_echo "#define _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK 1" >>confdefs.h + + fi + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $glibcxx_cv_PTHREAD_MUTEX_CLOCKLOCK" >&5 +$as_echo "$glibcxx_cv_PTHREAD_MUTEX_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 ad4ae0c..35d346f 100644 --- a/libstdc++-v3/configure.ac +++ b/libstdc++-v3/configure.ac @@ -223,6 +223,9 @@ GLIBCXX_CHECK_TMPNAM # For pthread_cond_clockwait GLIBCXX_CHECK_PTHREAD_COND_CLOCKWAIT +# For pthread_mutex_clocklock +GLIBCXX_CHECK_PTHREAD_MUTEX_CLOCKLOCK + AC_LC_MESSAGES # For hardware_concurrency diff --git a/libstdc++-v3/include/std/mutex b/libstdc++-v3/include/std/mutex index 981b672..e06d286 100644 --- a/libstdc++-v3/include/std/mutex +++ b/libstdc++-v3/include/std/mutex @@ -134,22 +134,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION class __timed_mutex_impl { protected: - typedef chrono::high_resolution_clock __clock_t; - template bool _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime) { - using chrono::steady_clock; - auto __rt = chrono::duration_cast(__rtime); - if (ratio_greater()) +#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK + using __clock = chrono::steady_clock; +#else + using __clock = chrono::system_clock; +#endif + + auto __rt = chrono::duration_cast<__clock::duration>(__rtime); + if (ratio_greater<__clock::period, _Period>()) ++__rt; - return _M_try_lock_until(steady_clock::now() + __rt); + return _M_try_lock_until(__clock::now() + __rt); } template bool - _M_try_lock_until(const chrono::time_point<__clock_t, + _M_try_lock_until(const chrono::time_point& __atime) { auto __s = chrono::time_point_cast(__atime); @@ -163,12 +166,31 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return static_cast<_Derived*>(this)->_M_timedlock(__ts); } +#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK + template + bool + _M_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()) + }; + + return static_cast<_Derived*>(this)->_M_clocklock(CLOCK_MONOTONIC, + __ts); + } +#endif + template bool _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime) { auto __rtime = __atime - _Clock::now(); - return _M_try_lock_until(__clock_t::now() + __rtime); + return _M_try_lock_for(__rtime); } }; @@ -229,6 +251,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool _M_timedlock(const __gthread_time_t& __ts) { return !__gthread_mutex_timedlock(&_M_mutex, &__ts); } +#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK + bool + _M_clocklock(clockid_t clockid, const __gthread_time_t& __ts) + { return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); } +#endif }; /// recursive_timed_mutex @@ -289,6 +316,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool _M_timedlock(const __gthread_time_t& __ts) { return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); } + +#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK + bool + _M_clocklock(clockid_t clockid, const __gthread_time_t& __ts) + { return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); } +#endif }; #else // !_GTHREAD_USE_MUTEX_TIMEDLOCK -- git-series 0.9.1