[libcxx-commits] [PATCH] D65339: Implement std::condition_variable via pthread_cond_clockwait() where available

Tom via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 26 10:25:21 PDT 2019


tomcherry created this revision.
Herald added subscribers: libcxx-commits, jfb, ldionne, christof, krytarowski, srhines.

std::condition_variable is currently implemented via
pthread_cond_timedwait() on systems that use pthread.  This is
problematic, since that function waits by default on CLOCK_REALTIME
and libc++ does not provide any mechanism to change from this
default.

Due to this, regardless of if condition_variable::wait_until() is
called with a chrono::system_clock or chrono::steady_clock parameter,
condition_variable::wait_until() will wait using CLOCK_REALTIME.  This
is not accurate to the C++ standard as calling
condition_variable::wait_until() with a chrono::steady_clock parameter
should use CLOCK_MONOTONIC.

This is particularly problematic because CLOCK_REALTIME is a bad
choice as it is subject to discontinuous time adjustments, that may
cause condition_variable::wait_until() to immediately timeout or wait
indefinitely.

This change fixes this issue with a new POSIX function,
pthread_cond_clockwait() proposed on
http://austingroupbugs.net/view.php?id=1216.  The new function is
similar to pthread_cond_timedwait() with the addition of a clock
parameter that allows it to wait using either CLOCK_REALTIME or
CLOCK_MONOTONIC, thus allowing condition_variable::wait_until() to
wait using CLOCK_REALTIME for chrono::system_clock and CLOCK_MONOTONIC
for chrono::steady_clock.

pthread_cond_clockwait() is implemented in glibc (2.30 and later) and
Android's bionic (Android API version 30 and later).

This change additionally makes wait_for() and wait_until() with clocks
other than chrono::system_clock use CLOCK_MONOTONIC.

Bug: 35756266
Test: boot, condition variables unaffected by time changes
Test: libc++ tests

Change-Id: I0246d85df106daa7a3e59ed2bb21180c86d6722d


Repository:
  rCXX libc++

https://reviews.llvm.org/D65339

Files:
  include/__config
  include/__mutex_base
  test/std/thread/thread.condition/thread.condition.condvar/wait_until.pass.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65339.211962.patch
Type: text/x-patch
Size: 10280 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190726/6eda40c1/attachment-0001.bin>


More information about the libcxx-commits mailing list