From: Mike Crowe Date: Mon, 2 Dec 2019 16:23:14 +0000 (+0000) Subject: libstdc++: Fix try_lock_until and try_lock_shared_until on arbitrary clock X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=b789efeae8c0620b83f25e4a0757c4871e02ab5f;p=gcc.git libstdc++: Fix try_lock_until and try_lock_shared_until on arbitrary clock This is the equivalent to PR libstdc++/91906, but for shared_mutex. A non-standard clock may tick more slowly than std::chrono::steady_clock. This means that we risk returning false early when the specified timeout may not have expired. This can be avoided by looping until the timeout time as reported by the non-standard clock has been reached. Unfortunately, we have no way to tell whether the non-standard clock ticks more quickly that std::chrono::steady_clock. If it does then we risk returning later than would be expected, but that is unavoidable without waking up periodically to check, which would be rather too expensive. François Dumont pointed out[1] a flaw in an earlier version of this patch that revealed a hole in the test coverage, so I've added a new test that try_lock_until acts as try_lock if the timeout has already expired. [1] https://gcc.gnu.org/ml/libstdc++/2019-10/msg00021.html 2019-12-02 Mike Crowe Fix try_lock_until and try_lock_shared_until on arbitrary clock * include/std/shared_mutex (shared_timed_mutex::try_lock_until) (shared_timed_mutex::try_lock_shared_until): Loop until the absolute timeout time is reached as measured against the appropriate clock. * testsuite/30_threads/shared_timed_mutex/try_lock_until/1.cc: New file. Test try_lock_until and try_lock_shared_until timeouts against various clocks. * testsuite/30_threads/shared_timed_mutex/try_lock_until/1.cc: New file. Test try_lock_until and try_lock_shared_until timeouts against various clocks. From-SVN: r278904 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 93331fb529a..b39855d3f58 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,16 @@ 2019-12-02 Mike Crowe + Fix try_lock_until and try_lock_shared_until on arbitrary clock + * include/std/shared_mutex (shared_timed_mutex::try_lock_until) + (shared_timed_mutex::try_lock_shared_until): Loop until the absolute + timeout time is reached as measured against the appropriate clock. + * testsuite/30_threads/shared_timed_mutex/try_lock_until/1.cc: New + file. Test try_lock_until and try_lock_shared_until timeouts against + various clocks. + * testsuite/30_threads/shared_timed_mutex/try_lock_until/1.cc: New + file. Test try_lock_until and try_lock_shared_until timeouts against + various clocks. + 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 diff --git a/libstdc++-v3/include/std/shared_mutex b/libstdc++-v3/include/std/shared_mutex index cfe2ec078e1..f6cf7e75967 100644 --- a/libstdc++-v3/include/std/shared_mutex +++ b/libstdc++-v3/include/std/shared_mutex @@ -554,9 +554,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime) { + // The user-supplied clock may not tick at the same rate as + // steady_clock, so we must loop in order to guarantee that + // the timeout has expired before returning false. typename _Clock::time_point __now = _Clock::now(); - auto __rtime = __atime - __now; - return try_lock_for(__rtime); + do { + auto __rtime = __atime - __now; + if (try_lock_for(__rtime)) + return true; + __now = _Clock::now(); + } while (__atime > __now); + return false; } // Shared ownership @@ -631,9 +639,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __atime) { + // The user-supplied clock may not tick at the same rate as + // steady_clock, so we must loop in order to guarantee that + // the timeout has expired before returning false. typename _Clock::time_point __now = _Clock::now(); - auto __rtime = __atime - __now; - return try_lock_shared_for(__rtime); + do { + auto __rtime = __atime - __now; + if (try_lock_shared_for(__rtime)) + return true; + __now = _Clock::now(); + } while (__atime > __now); + return false; } #else // ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)