diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 24a4a35d1731fb40177904f90d0e83d5d661d1f7..d39a06f962f5df72ae4f2235777f9fa69a7f7216 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,22 @@ 2019-12-02 Mike Crowe <mac@mcrowe.com> + PR libstdc++/78237 Add full steady_clock support to timed_mutex + * acinclude.m4 (GLIBCXX_CHECK_PTHREAD_MUTEX_CLOCKLOCK): Define to + detect presence of pthread_mutex_clocklock function. + * config.h.in: Regenerate. + * configure: Regenerate. + * configure.ac: Call GLIBCXX_CHECK_PTHREAD_MUTEX_CLOCKLOCK. + * include/std/mutex (__timed_mutex_impl): Remove unnecessary __clock_t. + (__timed_mutex_impl::_M_try_lock_for): Use best clock to turn relative + timeout into absolute timeout. + (__timed_mutex_impl::_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. + [_GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK] (timed_mutex::_M_clocklock): + New member function. + (recursive_timed_mutex::_M_clocklock): Likewise. + * testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc: New test. Ensure that timed_mutex::try_lock_until actually times out after the specified time when using both system_clock and diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index 73e07e513bcda4faacefe46092627104c30b5dfd..7e685bb2f898171d56902a5381b5e0768cc47736 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -4227,6 +4227,37 @@ AC_DEFUN([GLIBCXX_CHECK_PTHREAD_COND_CLOCKWAIT], [ 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_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.h>], + [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 <pthread.h>.]) + 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 <pthread.h>, and define _GLIBCXX_USE_SYSCTL_HW_NCPU. dnl diff --git a/libstdc++-v3/config.h.in b/libstdc++-v3/config.h.in index 32f7863a85ef90083c29f466b84b95c54b0339a2..3d515347396b5fb87ae506d2d161c19ca09c733a 100644 --- a/libstdc++-v3/config.h.in +++ b/libstdc++-v3/config.h.in @@ -1002,6 +1002,9 @@ /* Define if pthread_cond_clockwait is available in <pthread.h>. */ #undef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT +/* Define if pthread_mutex_clocklock is available in <pthread.h>. */ +#undef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK + /* Define if POSIX read/write locks are available in <gthr.h>. */ #undef _GLIBCXX_USE_PTHREAD_RWLOCK_T diff --git a/libstdc++-v3/configure b/libstdc++-v3/configure index e1d2413229467f8fb06bb1336859270809ed4696..a439bb86e6eed48b1d4bf46ad7f23ec922b44a8f 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -21880,6 +21880,89 @@ 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 <pthread.h> +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 <pthread.h> +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 154819acefef3c6f62bf90e8bc04a2e0bb5ddbac..c1ecc6705192e0334d8d348d0c08559ae901f08c 100644 --- a/libstdc++-v3/configure.ac +++ b/libstdc++-v3/configure.ac @@ -225,6 +225,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 981b6725f7c93e7e3a931d116fd3a70345029110..26ee084b9793e48078e758610d515d5dc331b74c 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<typename _Rep, typename _Period> bool _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime) { - using chrono::steady_clock; - auto __rt = chrono::duration_cast<steady_clock::duration>(__rtime); - if (ratio_greater<steady_clock::period, _Period>()) +#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<typename _Duration> bool - _M_try_lock_until(const chrono::time_point<__clock_t, + _M_try_lock_until(const chrono::time_point<chrono::system_clock, _Duration>& __atime) { auto __s = chrono::time_point_cast<chrono::seconds>(__atime); @@ -163,12 +166,31 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return static_cast<_Derived*>(this)->_M_timedlock(__ts); } +#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK + template<typename _Duration> + bool + _M_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()) + }; + + return static_cast<_Derived*>(this)->_M_clocklock(CLOCK_MONOTONIC, + __ts); + } +#endif + template<typename _Clock, typename _Duration> 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,12 @@ _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 +317,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