gcc/libstdc++-v3/include
Jonathan Wakely 93fc477468 libstdc++: Optimise std::future::wait_for and fix futex polling
To poll a std::future to see if it's ready you have to call one of the
timed waiting functions. The most obvious way is wait_for(0s) but this
was previously very inefficient because it would turn the relative
timeout to an absolute one by calling system_clock::now(). When the
relative timeout is zero (or less) we're obviously going to get a time
that has already passed, but the overhead of obtaining the current time
can be dozens of microseconds. The alternative is to call wait_until
with an absolute timeout that is in the past. If you know the clock's
epoch is in the past you can use a default constructed time_point.
Alternatively, using some_clock::time_point::min() gives the earliest
time point supported by the clock, which should be safe to assume is in
the past. However, using a futex wait with an absolute timeout before
the UNIX epoch fails and sets errno=EINVAL. The new code using futex
waits with absolute timeouts was not checking for this case, which could
result in hangs (or killing the process if the libray is built with
assertions enabled).

This patch checks for times before the epoch before attempting to wait
on a futex with an absolute timeout, which fixes the hangs or crashes.
It also makes it very fast to poll using an absolute timeout before the
epoch (because we skip the futex syscall).

It also makes future::wait_for avoid waiting at all when the relative
timeout is zero or less, to avoid the unnecessary overhead of getting
the current time. This makes polling with wait_for(0s) take only a few
cycles instead of dozens of milliseconds.

libstdc++-v3/ChangeLog:

	* include/std/future (future::wait_for): Do not wait for
	durations less than or equal to zero.
	* src/c++11/futex.cc (_M_futex_wait_until)
	(_M_futex_wait_until_steady): Do not wait for timeouts before
	the epoch.
	* testsuite/30_threads/future/members/poll.cc: New test.
2020-11-12 23:47:04 +00:00
..
backward
bits libstdc++: Add remaining C++20 additions to <sstream> [P0408R7] 2020-11-10 19:22:47 +00:00
c
c_compatibility
c_global libstdc++: Add parentheses around assignments used as truth values 2020-09-10 17:09:16 +01:00
c_std
debug libstdc++: Remove <debug/array> 2020-11-09 21:20:01 +01:00
decimal
experimental libstdc++: Remove <debug/array> 2020-11-09 21:20:01 +01:00
ext libstdc++: Simplify __numeric_traits definition 2020-11-12 14:36:39 +00:00
parallel libstdc++: Define new C++17 std::search overload for Parallel Mode [PR 94971] 2020-11-04 13:36:32 +00:00
precompiled libstdc++: Add c++2a <syncstream> 2020-11-02 10:41:32 -08:00
pstl libstdc++: Rebase include/pstl to current upstream 2020-10-21 06:11:28 -07:00
std libstdc++: Optimise std::future::wait_for and fix futex polling 2020-11-12 23:47:04 +00:00
tr1 libstdc++: Prevent deprecation warnings from <tr1/shared_ptr> 2020-10-29 22:47:22 +00:00
tr2
Makefile.am libstdc++: Remove <debug/array> 2020-11-09 21:20:01 +01:00
Makefile.in libstdc++: Remove <debug/array> 2020-11-09 21:20:01 +01:00