2019-12-02 Mike Crowe <mac@mcrowe.com>
+ 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
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
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)