libstdc++: Fix timed_mutex::try_lock_until on arbitrary clock (PR 91906)
authorMike Crowe <mac@mcrowe.com>
Mon, 2 Dec 2019 16:23:06 +0000 (16:23 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 2 Dec 2019 16:23:06 +0000 (16:23 +0000)
commita7334019b11798a9e791edef62a690b521e78a5b
tree498509b78868b1b78e45cd8b030fa1f557a57d41
parent3b2fb54353d29ae7c6067ebc78549d1296b1340d
libstdc++: Fix timed_mutex::try_lock_until on arbitrary clock (PR 91906)

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 and
permitted by the standard.

2019-12-02  Mike Crowe  <mac@mcrowe.com>

PR libstdc++/91906 Fix timed_mutex::try_lock_until on arbitrary clock
* include/std/mutex (__timed_mutex_impl::_M_try_lock_until): Loop
until the absolute timeout time is reached as measured against the
appropriate clock.
* testsuite/util/slow_clock.h: New file. Move implementation of
slow_clock test class.
* testsuite/30_threads/condition_variable/members/2.cc: Include
slow_clock from header.
* testsuite/30_threads/shared_timed_mutex/try_lock/3.cc: Convert
existing test to templated function so that it can be called with
both system_clock and steady_clock.
* testsuite/30_threads/timed_mutex/try_lock_until/3.cc: Also run test
using slow_clock to test above fix.
* testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc:
Likewise.
* testsuite/30_threads/recursive_timed_mutex/try_lock_until/4.cc: Add
new test that try_lock_until behaves as try_lock if the timeout has
already expired or exactly matches the current time.

From-SVN: r278902
libstdc++-v3/ChangeLog
libstdc++-v3/include/std/mutex
libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc
libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc
libstdc++-v3/testsuite/30_threads/shared_timed_mutex/try_lock/3.cc
libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/3.cc
libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/4.cc [new file with mode: 0644]
libstdc++-v3/testsuite/util/slow_clock.h [new file with mode: 0644]