gcc/libstdc++-v3/include/std
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
..
algorithm libstdc++: Implement C++20 constrained algorithms 2020-02-06 20:08:34 -05:00
any libstdc++: Fix constructor constraints for std::any (PR 90415) 2020-04-24 00:54:20 +01:00
array libstdc++: Remove <debug/array> 2020-11-09 21:20:01 +01:00
atomic libstdc++: Break long lines to fit in 80 columns 2020-08-05 22:48:17 +01:00
bit libstdc++: Remove redundant check for zero in std::__popcount 2020-11-09 14:54:29 +00:00
bitset libstdc++: Add comparison operators to types from Utilities clause 2020-04-15 19:47:48 +01:00
charconv libstdc++: Add missing noexcept to std::from_chars declarations 2020-10-27 14:50:38 +00:00
chrono libstdc++: Fix arithmetic bug in year_month_weekday conversion [PR96713] 2020-10-28 12:28:08 -04:00
codecvt Update copyright years. 2020-01-01 12:51:42 +01:00
complex libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
concepts libstdc++: Fix typos in copyright notice 2020-10-15 19:20:21 +01:00
condition_variable libstdc++: Reduce header dependencies in and on <memory> 2020-10-22 18:42:03 +01:00
coroutine libstdc++: Define noop coroutine details private and inline [PR 95917] 2020-10-20 11:37:48 +01:00
deque libstdc++: Update __cpp_lib_erase_if macro (P1115R3) 2020-02-15 10:25:23 +00:00
execution libstdc++: Define __cpp_lib_execution feature test macro 2020-04-22 22:54:35 +01:00
filesystem Update copyright years. 2020-01-01 12:51:42 +01:00
forward_list libstdc++: Update __cpp_lib_erase_if macro (P1115R3) 2020-02-15 10:25:23 +00:00
fstream libstdc++: Override BUFSIZ for Windows targets [PR 94268] 2020-10-28 13:19:21 +00:00
functional libstdc++: Remove <debug/array> 2020-11-09 21:20:01 +01:00
future libstdc++: Optimise std::future::wait_for and fix futex polling 2020-11-12 23:47:04 +00:00
iomanip Update copyright years. 2020-01-01 12:51:42 +01:00
ios Update copyright years. 2020-01-01 12:51:42 +01:00
iosfwd Update copyright years. 2020-01-01 12:51:42 +01:00
iostream Update copyright years. 2020-01-01 12:51:42 +01:00
istream libstdc++: Adjust overflow prevention to operator>> 2020-08-06 18:26:45 +01:00
iterator Update copyright years. 2020-01-01 12:51:42 +01:00
limits libstdc++: Make __int128 meet integer-class requirements [PR 96042] 2020-08-19 16:49:07 +01:00
list libstdc++: Update __cpp_lib_erase_if macro (P1115R3) 2020-02-15 10:25:23 +00:00
locale Update copyright years. 2020-01-01 12:51:42 +01:00
map Update copyright years. 2020-01-01 12:51:42 +01:00
memory libstdc++: Reduce header dependencies in and on <memory> 2020-10-22 18:42:03 +01:00
memory_resource libstdc++: Ensure std::forward_as_tuple is defined 2020-10-23 01:12:00 +01:00
mutex libstdc++: Rewrite std::call_once to use futexes [PR 66146] 2020-11-03 18:44:49 +00:00
numbers Update copyright years. 2020-01-01 12:51:42 +01:00
numeric libstdc++: Minor header cleanup in <numeric> 2020-10-05 22:46:46 +01:00
optional libstdc++: Fix constraints on std::optional comparisons [PR 96269] 2020-11-05 19:09:22 +00:00
ostream libstdc++: Implement std::emit_on_flush etc. 2020-11-11 00:19:40 +00:00
queue Update copyright years. 2020-01-01 12:51:42 +01:00
random Update copyright years. 2020-01-01 12:51:42 +01:00
ranges libstdc++: Don't initialize from *this inside some views [PR97600] 2020-10-30 20:33:19 -04:00
ratio Update copyright years. 2020-01-01 12:51:42 +01:00
regex libstdc++: Reduce uses of std::numeric_limits 2020-10-06 00:05:11 +01:00
scoped_allocator libstdc++: Reduce header dependencies in and on <memory> 2020-10-22 18:42:03 +01:00
set Update copyright years. 2020-01-01 12:51:42 +01:00
shared_mutex libstdc++: Only include <condition_variable> in <shared_mutex> if needed 2020-10-22 18:49:38 +01:00
span libstdc++: Remove <debug/array> 2020-11-09 21:20:01 +01:00
sstream libstdc++: Reorder constructors in <sstream> 2020-11-10 19:22:48 +00:00
stack Update copyright years. 2020-01-01 12:51:42 +01:00
stdexcept Update copyright years. 2020-01-01 12:51:42 +01:00
stop_token libstdc++: Use helper type for checking thread ID 2020-11-11 11:13:31 +00:00
streambuf libstdc++: Add deprecated attributes to old iostream members 2020-08-19 12:13:23 +01:00
string libstdc++: Update __cpp_lib_erase_if macro (P1115R3) 2020-02-15 10:25:23 +00:00
string_view libstdc++: Reduce uses of std::numeric_limits 2020-10-06 00:05:11 +01:00
syncstream libstdc++: Implement std::emit_on_flush etc. 2020-11-11 00:19:40 +00:00
system_error libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
thread libstdc++: Assigning to a joinable std::jthread calls std::terminate 2020-11-11 11:13:31 +00:00
tuple libstdc++: Use reserved name for C++20 attribute 2020-10-19 18:07:21 +01:00
type_traits libstdc++: Define type traits for wchar_t even when libc support missing 2020-11-01 11:39:07 +00:00
typeindex libstdc++: Add spaceship operator to std::type_index 2020-04-15 19:47:47 +01:00
unordered_map Update copyright years. 2020-01-01 12:51:42 +01:00
unordered_set Update copyright years. 2020-01-01 12:51:42 +01:00
utility libstdc++: Replace __int_limits with __numeric_traits_integer 2020-07-07 23:36:29 +01:00
valarray libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
variant libstdc++: Fix some warnings in headers 2020-10-29 22:47:22 +00:00
vector libstdc++: Update __cpp_lib_erase_if macro (P1115R3) 2020-02-15 10:25:23 +00:00
version libstdc++: Add c++2a <syncstream> 2020-11-02 10:41:32 -08:00