From 6162239973a3a10ae49c3cea0c1008d4f9c69b5b Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 11 Nov 2013 13:33:48 +0000 Subject: [PATCH] re PR libstdc++/54562 (mutex and condition variable timers) PR libstdc++/54562 * include/std/mutex (__timed_mutex_impl::__clock_t): Use high_resolution_clock for absolute timeouts, because pthread_mutex_timedlock uses CLOCK_REALTIME not CLOCK_MONOTONIC. (__timed_mutex_impl::_M_try_lock_for): Use steady_clock for relative timeouts as per [thread.req.timing]. (__timed_mutex_impl::_M_try_lock_until): Convert to __clock_t time point instead of using _M_try_lock_for. From-SVN: r204672 --- libstdc++-v3/ChangeLog | 11 +++++++++++ libstdc++-v3/include/std/mutex | 24 ++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 2244435322d..38c70c583e8 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2013-11-11 Jonathan Wakely + + PR libstdc++/54562 + * include/std/mutex (__timed_mutex_impl::__clock_t): Use + high_resolution_clock for absolute timeouts, because + pthread_mutex_timedlock uses CLOCK_REALTIME not CLOCK_MONOTONIC. + (__timed_mutex_impl::_M_try_lock_for): Use steady_clock for relative + timeouts as per [thread.req.timing]. + (__timed_mutex_impl::_M_try_lock_until): Convert to + __clock_t time point instead of using _M_try_lock_for. + 2013-11-09 Jonathan Wakely PR libstdc++/58982 diff --git a/libstdc++-v3/include/std/mutex b/libstdc++-v3/include/std/mutex index 40b2e31dc7f..da2ca0cfe94 100644 --- a/libstdc++-v3/include/std/mutex +++ b/libstdc++-v3/include/std/mutex @@ -203,21 +203,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION class __timed_mutex_impl { protected: -#ifdef _GLIBCXX_USE_CLOCK_MONOTONIC - typedef chrono::steady_clock __clock_t; -#else typedef chrono::high_resolution_clock __clock_t; -#endif template bool _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime) { - auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime); - if (ratio_greater<__clock_t::period, _Period>()) + using chrono::steady_clock; + auto __rt = chrono::duration_cast(__rtime); + if (ratio_greater()) ++__rt; - - return _M_try_lock_until(__clock_t::now() + __rt); + return _M_try_lock_until(steady_clock::now() + __rt); } template @@ -225,11 +221,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_try_lock_until(const chrono::time_point<__clock_t, _Duration>& __atime) { - chrono::time_point<__clock_t, chrono::seconds> __s = - chrono::time_point_cast(__atime); - - chrono::nanoseconds __ns = - chrono::duration_cast(__atime - __s); + 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()), @@ -243,7 +236,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template bool _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime) - { return _M_try_lock_for(__atime - _Clock::now()); } + { + auto __rtime = __atime - _Clock::now(); + return _M_try_lock_until(__clock_t::now() + __rtime); + } }; /// timed_mutex