https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78237

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Author: redi
Date: Mon Dec  2 16:23:01 2019
New Revision: 278901

URL: https://gcc.gnu.org/viewcvs?rev=278901&root=gcc&view=rev
Log:
libstdc++: PR 78237 Add full steady_clock support to timed_mutex

The pthread_mutex_clocklock function is available in glibc since the
2.30 release. If this function is available in the C library it can be
used to fix PR libstdc++/78237 by supporting steady_clock properly with
timed_mutex.

This means that code using timed_mutex::try_lock_for or
timed_mutex::wait_until with steady_clock is no longer subject to timing
out early or potentially waiting for much longer if the system clock is
warped at an inopportune moment.

If pthread_mutex_clocklock is available then steady_clock is deemed to
be the "best" clock available which means that it is used for the
relative try_lock_for calls and absolute try_lock_until calls using
steady_clock and user-defined clocks. Calls explicitly using
system_clock (aka high_resolution_clock) continue to use CLOCK_REALTIME
via __gthread_cond_timedwait.

If pthread_mutex_clocklock is not available then system_clock is deemed
to be the "best" clock available which means that the previous
suboptimal behaviour remains.

2019-12-02  Mike Crowe  <m...@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.

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/acinclude.m4
    trunk/libstdc++-v3/config.h.in
    trunk/libstdc++-v3/configure
    trunk/libstdc++-v3/configure.ac
    trunk/libstdc++-v3/include/std/mutex

Reply via email to