Skip to content
Snippets Groups Projects
  • Mike Crowe's avatar
    ab40695a
    libstdc++: Add full steady_clock support to shared_timed_mutex · ab40695a
    Mike Crowe authored
    The pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock functions
    were added to glibc in v2.30. They have also been added to Android
    Bionic. If these functions are available in the C library then they can
    be used to implement shared_timed_mutex::try_lock_until,
    shared_timed_mutex::try_lock_for,
    shared_timed_mutex::try_lock_shared_until and
    shared_timed_mutex::try_lock_shared_for so that they are no longer
    unaffected by the system clock being warped. (This is the shared_mutex
    equivalent of PR libstdc++/78237 for mutex.)
    
    If the new functions are 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. It's not possible to have
    _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK defined without
    _GLIBCXX_USE_PTHREAD_RWLOCK_T, so the requirement that the clock be the
    same as condition_variable is maintained. Calls explicitly using
    system_clock (aka high_resolution_clock) continue to use CLOCK_REALTIME
    via the old pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock
    functions.
    
    If the new functions are not available then system_clock is deemed to be
    the "best" clock available which means that the previous suboptimal
    behaviour remains.
    
    Additionally, the user-defined clock used with
    shared_timed_mutex::try_lock_for and shared_mutex::try_lock_shared_for
    may have higher precision than __clock_t. We may need to round the
    duration up to ensure that the timeout is long enough. (See
    __timed_mutex_impl::_M_try_lock_for)
    
    2019-12-02  Mike Crowe  <mac@mcrowe.com>
    
    	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
    	pthread_rwlock_clockwrlock.
    	* config.h.in: Regenerate.
    	* configure.ac: Call GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK.
    	* configure: Regenerate.
    	* include/std/shared_mutex (shared_timed_mutex): Define __clock_t as
    	the best clock to use for relative waits.
    	(shared_timed_mutex::try_lock_for) Round up wait duration if necessary.
    	(shared_timed_mutex::try_lock_shared_for): Likewise.
    	(shared_timed_mutex::try_lock_until): Use existing try_lock_until
    	implementation for system_clock (which matches __clock_t when
    	_GLIBCCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK is not defined). Add new
    	overload for steady_clock that uses pthread_rwlock_clockwrlock if it
    	is available. Simplify overload for non-standard clock to just call
    	try_lock_for with a relative timeout.
    	(shared_timed_mutex::try_lock_shared_until): Likewise.
    
    From-SVN: r278903
    ab40695a
    History
    libstdc++: Add full steady_clock support to shared_timed_mutex
    Mike Crowe authored
    The pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock functions
    were added to glibc in v2.30. They have also been added to Android
    Bionic. If these functions are available in the C library then they can
    be used to implement shared_timed_mutex::try_lock_until,
    shared_timed_mutex::try_lock_for,
    shared_timed_mutex::try_lock_shared_until and
    shared_timed_mutex::try_lock_shared_for so that they are no longer
    unaffected by the system clock being warped. (This is the shared_mutex
    equivalent of PR libstdc++/78237 for mutex.)
    
    If the new functions are 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. It's not possible to have
    _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK defined without
    _GLIBCXX_USE_PTHREAD_RWLOCK_T, so the requirement that the clock be the
    same as condition_variable is maintained. Calls explicitly using
    system_clock (aka high_resolution_clock) continue to use CLOCK_REALTIME
    via the old pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock
    functions.
    
    If the new functions are not available then system_clock is deemed to be
    the "best" clock available which means that the previous suboptimal
    behaviour remains.
    
    Additionally, the user-defined clock used with
    shared_timed_mutex::try_lock_for and shared_mutex::try_lock_shared_for
    may have higher precision than __clock_t. We may need to round the
    duration up to ensure that the timeout is long enough. (See
    __timed_mutex_impl::_M_try_lock_for)
    
    2019-12-02  Mike Crowe  <mac@mcrowe.com>
    
    	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
    	pthread_rwlock_clockwrlock.
    	* config.h.in: Regenerate.
    	* configure.ac: Call GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK.
    	* configure: Regenerate.
    	* include/std/shared_mutex (shared_timed_mutex): Define __clock_t as
    	the best clock to use for relative waits.
    	(shared_timed_mutex::try_lock_for) Round up wait duration if necessary.
    	(shared_timed_mutex::try_lock_shared_for): Likewise.
    	(shared_timed_mutex::try_lock_until): Use existing try_lock_until
    	implementation for system_clock (which matches __clock_t when
    	_GLIBCCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK is not defined). Add new
    	overload for steady_clock that uses pthread_rwlock_clockwrlock if it
    	is available. Simplify overload for non-standard clock to just call
    	try_lock_for with a relative timeout.
    	(shared_timed_mutex::try_lock_shared_until): Likewise.
    
    From-SVN: r278903