aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/src
AgeCommit message (Collapse)AuthorFilesLines
2020-12-18libstdc++: Fix build failure due to missing <langinfo.h> [PR98374]Patrick Palka1-1/+5
This should fix a build failure on Windows which lacks <langinfo.h>, from which we use nl_langinfo() to obtain the radix character of the current locale. (We can't use the more portable localeconv() from <clocale> to obtain the radix character of the current locale here because it's not thread-safe, unfortunately.) This change means that on Windows and other such platforms, we'll just always assume the radix character used by printf is '.' when formatting a long double through it. libstdc++-v3/ChangeLog: PR libstdc++/98374 * src/c++17/floating_to_chars.cc: Guard include of <langinfo.h> with __has_include. (__floating_to_chars_precision) [!defined(RADIXCHAR)]: Don't attempt to obtain the radix character of the current locale, just assume it's '.'.
2020-12-18libstdc++: Check FE_TONEAREST is defined before using itPatrick Palka2-6/+6
We need to test that FE_TONEAREST is defined before we may use it along with fegetround/fesetround to adjust the floating-point rounding mode. This fixes a build failure with older versions of newlib. libstdc++-v3/ChangeLog: * src/c++17/floating_from_chars.cc (from_chars_impl) [!defined(FE_TONEAREST)]: Don't adjust the rounding mode. * src/c++17/floating_to_chars.cc (__floating_to_chars_precision): Likewise.
2020-12-17libstdc++: Add floating-point std::to_chars implementationPatrick Palka3-1/+1566
This implements the floating-point std::to_chars overloads for float, double and long double. We use the Ryu library to compute the shortest round-trippable fixed and scientific forms for float, double and long double. We also use Ryu for performing explicit-precision fixed and scientific formatting for float and double. For explicit-precision formatting for long double we fall back to using printf. Hexadecimal formatting for float, double and long double is implemented from scratch. The supported long double binary formats are binary64, binary80 (x86 80-bit extended precision), binary128 and ibm128. Much of the complexity of the implementation is in computing the exact output length before handing it off to Ryu (which doesn't do bounds checking). In some cases it's hard to compute the output length beforehand, so in these cases we instead compute an upper bound on the output length and use a sufficiently-sized intermediate buffer only if necessary. Another source of complexity is in the general-with-precision formatting mode, where we need to do zero-trimming of the string returned by Ryu, and where we also take care to avoid having to format the number through Ryu a second time when the general formatting mode resolves to fixed (which we determine by doing a scientific formatting first and inspecting the scientific exponent). We avoid going through Ryu twice by instead transforming the scientific form to the corresponding fixed form via in-place string manipulation. This implementation is non-conforming in a couple of ways: 1. For the shortest hexadecimal formatting, we currently follow the Microsoft implementation's decision to be consistent with the output of printf's '%a' specifier at the expense of sometimes not printing the shortest representation. For example, the shortest hex form for the number 1.08p+0 is 2.1p-1, but we output the former instead of the latter, as does printf. 2. The Ryu routine generic_binary_to_decimal that we use for performing shortest formatting for large floating point types is implemented using the __int128 type, but some targets with a large long double type lack __int128 (e.g. i686), so we can't perform shortest formatting of long double on such targets through Ryu. As a temporary stopgap this patch makes the long double to_chars overloads just dispatch to the double overloads on these targets, which means we lose precision in the output. (We could potentially fix this by writing a specialized version of Ryu's generic_binary_to_decimal routine that uses uint64_t instead of __int128.) [Though I wonder if there's a better way to work around the lack of __int128 on i686 specifically?] 3. Our shortest formatting for __ibm128 doesn't guarantee the round-trip property if the difference between the high- and low-order exponent is large. This is because we treat __ibm128 as if it has a contiguous 105-bit mantissa by merging the mantissas of the high- and low-order parts (using code extracted from glibc), so we potentially lose precision from the low-order part. This seems to be consistent with how glibc printf formats __ibm128. libstdc++-v3/ChangeLog: * config/abi/pre/gnu.ver: Add new exports. * include/std/charconv (to_chars): Declare the floating-point overloads for float, double and long double. * src/c++17/Makefile.am (sources): Add floating_to_chars.cc. * src/c++17/Makefile.in: Regenerate. * src/c++17/floating_to_chars.cc: New file. (to_chars): Define for float, double and long double. * testsuite/20_util/to_chars/long_double.cc: New test.
2020-12-17libstdc++: Apply modifications to our local copy of RyuPatrick Palka8-236/+45
This performs the following modifications to our local copy of Ryu in order to make it more readily usable for our std::to_chars implementation: * Remove all #includes * Remove copy_special_str routines * Adjust the exponent formatting to match printf * Remove some functions we're not going to use * Add an out-parameter to d2exp_buffered_n for the scientific exponent * Store the sign bit inside struct floating_decimal_[32|64] * Rename [df]2s_buffered_n and change their return type * Make generic_binary_to_decimal take the bit representation in parts libstdc++-v3/ChangeLog: * src/c++17/ryu/common.h, src/c++17/ryu/d2fixed.c, src/c++17/ryu/d2fixed_full_table.h, src/c++17/ryu/d2s.c, src/c++17/ryu/d2s_intrinsics.h, src/c++17/ryu/f2s.c, src/c++17/ryu/f2s_intrinsics.h, src/c++17/ryu/generic_128.c: Apply local modifications.
2020-12-17libstdc++: Import parts of the Ryu libraryPatrick Palka13-0/+8024
This imports the source files from the Ryu library that define d2s_buffered_n, f2s_buffered_n, d2fixed_buffered_n, d2exp_buffered_n and generic_binary_to_decimal, which we're going to use as the base of our std::to_chars implementation. libstdc++-v3/ChangeLog: * src/c++17/ryu/MERGE: New file. * src/c++17/ryu/common.h, src/c++17/ryu/d2fixed.c, src/c++17/ryu/d2fixed_full_table.h, src/c++17/ryu/d2s.c, src/c++17/ryu/d2s_full_table.h, src/c++17/ryu/d2s_intrinsics.h, src/c++17/ryu/digit_table.h, src/c++17/ryu/f2s.c, src/c++17/ryu/f2s_intrinsics.h, src/c++17/ryu/generic_128.c, src/c++17/ryu/generic_128.h, src/c++17/ryu/ryu_generic_128.h: Import these files from the Ryu library.
2020-12-17libstdc++: Fix -Wunused warningJonathan Wakely1-1/+2
As noted in PR 66146 comment 35, there is a new warning in the new std::call_once implementation. libstdc++-v3/ChangeLog: * src/c++11/mutex.cc (std::once_flag::_M_finish): Add maybe_unused attribute to variable used in assertion.
2020-12-16libstdc++: Add C++ runtime support for new 128-bit long double formatJonathan Wakely21-268/+885
This adds support for the new __ieee128 long double format on powerpc64le targets. Most of the complexity comes from wanting a single libstdc++.so library that contains the symbols needed by code compiled with both -mabi=ibmlongdouble and -mabi=ieeelongdouble (and not forgetting -mlong-double-64 as well!) In a few places this just requires an extra overload, for example std::from_chars has to be overloaded for both forms of long double. That can be done in a single translation unit that defines overloads for 'long double' and also '__ieee128', so that user code including <charconv> will be able to link to a definition for either type of long double. Those are the easy cases. The difficult parts are (as for the std::string ABI transition) the I/O and locale facets. In order to be able to write either form of long double to an ostream such as std::cout we need the locale to contain a std::num_put facet that can handle both forms. The same approach is taken as was already done for supporting 64-bit long double and 128-bit long double: adding extra overloads of do_put to the facet class. On targets where the new long double code is enabled, the facets that are registered in the locale at program startup have additional overloads so that they can work with any long double type. Where this fails to work is if user code installs its own facet, which will probably not have the additional overloads and so will only be able to output one or the other type. In practice the number of users expecting to be able to use their own locale facets in code using a mix of -mabi=ibmlongdouble and -mabi=ieeelongdouble is probably close to zero. libstdc++-v3/ChangeLog: * Makefile.in: Regenerate. * config.h.in: Regenerate. * config/abi/pre/gnu.ver: Make patterns less greedy. * config/os/gnu-linux/ldbl-ieee128-extra.ver: New file with patterns for IEEE128 long double symbols. * configure: Regenerate. * configure.ac: Enable alternative 128-bit long double format on powerpc64*-*-linux*. * doc/Makefile.in: Regenerate. * fragment.am: Regenerate. * include/Makefile.am: Set _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT. * include/Makefile.in: Regenerate. * include/bits/c++config: Define inline namespace for new long double symbols. Don't define _GLIBCXX_USE_FLOAT128 when it's the same type as long double. * include/bits/locale_classes.h [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT] (locale::_Impl::_M_init_extra_ldbl128): Declare new member function. * include/bits/locale_facets.h (_GLIBCXX_NUM_FACETS): Simplify by only counting narrow character facets. (_GLIBCXX_NUM_CXX11_FACETS): Likewise. (_GLIBCXX_NUM_LBDL_ALT128_FACETS): New. [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT] (num_get::__do_get): Define vtable placeholder for __ibm128 long double type. [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT && __LONG_DOUBLE_IEEE128__] (num_get::__do_get): Declare vtable placeholder for __ibm128 long double type. [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT && __LONG_DOUBLE_IEEE128__] (num_put::__do_put): Likewise. * include/bits/locale_facets.tcc [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT && __LONG_DOUBLE_IEEE128__] (num_get::__do_get, num_put::__do_put): Define. * include/bits/locale_facets_nonio.h [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT && __LONG_DOUBLE_IEEE128__] (money_get::__do_get): Declare vtable placeholder for __ibm128 long double type. [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT && __LONG_DOUBLE_IEEE128__] (money_put::__do_put): Likewise. * include/bits/locale_facets_nonio.tcc [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT && __LONG_DOUBLE_IEEE128__] (money_get::__do_get, money_put::__do_put): Define. * include/ext/numeric_traits.h [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT] (__numeric_traits<__ibm128>, __numeric_traits<__ieee128>): Define. * libsupc++/Makefile.in: Regenerate. * po/Makefile.in: Regenerate. * python/Makefile.in: Regenerate. * src/Makefile.am: Add compatibility-ldbl-alt128.cc and compatibility-ldbl-alt128-cxx11.cc sources and recipes for objects. * src/Makefile.in: Regenerate. * src/c++11/Makefile.in: Regenerate. * src/c++11/compatibility-ldbl-alt128-cxx11.cc: New file defining symbols using the old 128-bit long double format, for the cxx11 ABI. * src/c++11/compatibility-ldbl-alt128.cc: Likewise, for the gcc4-compatible ABI. * src/c++11/compatibility-ldbl-facets-aliases.h: New header for long double compat aliases. * src/c++11/cow-locale_init.cc: Add comment. * src/c++11/cxx11-locale-inst.cc: Define C and C_is_char unconditionally. * src/c++11/cxx11-wlocale-inst.cc: Add sanity check. Include locale-inst.cc directly, not via cxx11-locale-inst.cc. * src/c++11/locale-inst-monetary.h: New header for monetary category instantiations. * src/c++11/locale-inst-numeric.h: New header for numeric category instantiations. * src/c++11/locale-inst.cc: Include new headers for monetary, numeric, and long double definitions. * src/c++11/wlocale-inst.cc: Remove long double compat aliases that are defined in new header now. * src/c++17/Makefile.am: Use -mabi=ibmlongdouble for floating_from_chars.cc. * src/c++17/Makefile.in: Regenerate. * src/c++17/floating_from_chars.cc (from_chars_impl): Add if-constexpr branch for __ieee128. (from_chars): Overload for __ieee128. * src/c++20/Makefile.in: Regenerate. * src/c++98/Makefile.in: Regenerate. * src/c++98/locale_init.cc (num_facets): Adjust calculation. (locale::_Impl::_Impl(size_t)): Call _M_init_extra_ldbl128. * src/c++98/localename.cc (num_facets): Adjust calculation. (locale::_Impl::_Impl(const char*, size_t)): Call _M_init_extra_ldbl128. * src/filesystem/Makefile.in: Regenerate. * testsuite/Makefile.in: Regenerate. * testsuite/util/testsuite_abi.cc: Add new symbol versions. Allow new symbols to be added to GLIBCXX_IEEE128_3.4.29 and CXXABI_IEEE128_1.3.13 too. * testsuite/26_numerics/complex/abi_tag.cc: Add u9__ieee128 to regex matching expected symbols.
2020-12-09libstdc++: Fix build failure for target with no way to sleepJonathan Wakely1-1/+4
In previous releases the std::this_thread::sleep_for function was only declared if the target supports multiple threads. I changed that recently in r11-2649-g5bbb1f3000c57fd4d95969b30fa0e35be6d54ffb so that sleep_for could be used single-threaded. But that means that targets using --disable-threads are now required to provide some way to sleep. This breaks the build for (at least) AVR when trying to build a hosted library. This patch adds a new autoconf macro that is defined when no way to sleep is available, and uses that to suppress the sleeping functions in std::this_thread. The #error in src/c++11/thread.cc is retained for the case where there is no sleep function available but multiple threads are supported. This is consistent with previous releases, but that #error could probably be removed without any consequences. libstdc++-v3/ChangeLog: * acinclude.m4 (GLIBCXX_ENABLE_LIBSTDCXX_TIME): Define NO_SLEEP if none of nanosleep, sleep and Sleep is available. * config.h.in: Regenerate. * configure: Regenerate. * include/std/thread [_GLIBCXX_NO_SLEEP] (__sleep_for): Do not declare. [_GLIBCXX_NO_SLEEP] (sleep_for, sleep_until): Do not define. * src/c++11/thread.cc [_GLIBCXX_NO_SLEEP] (__sleep_for): Do not define.
2020-11-27libstdc++: Partially revert r11-5314Jonathan Wakely2-2/+2
The changes in r11-5314 are broken, because it means we don't use __gthread_once for the first few initializations, but after the program becomes multi-threaded we will repeat the initialization, using __gthread_once once this time. This leads to memory errors. The use of __is_single_threaded() in locale::id::_M_id() is OK, because the side effects are the same either way. libstdc++-v3/ChangeLog: * src/c++98/locale.cc (locale::facet::_S_get_c_locale()): Revert change to use __is_single_threaded. * src/c++98/locale_init.cc (locale::_S_initialize()): Likewise.
2020-11-25libstdc++: Encapsulate __gthread_cond_t as std::__condvarJonathan Wakely1-29/+4
This introduces a new internal utility, std::__condvar, which is a simplified form of std::condition_variable. It has no dependency on <chrono> or std::unique_lock, which allows it to be used in <bits/atomic_wait.h>. This avoids repeating the #ifdef __GTHREAD_COND_INIT preprocessor conditions and associated logic for initializing a __gthread_cond_t correctly. It also encapsulates most of the __gthread_cond_xxx functions as member functions of __condvar. libstdc++-v3/ChangeLog: * include/bits/atomic_timed_wait.h (__cond_wait_until_impl): Do not define when _GLIBCXX_HAVE_LINUX_FUTEX is defined. Use __condvar and mutex instead of __gthread_cond_t and unique_lock<mutex>. (__cond_wait_until): Likewise. Fix test for return value of __cond_wait_until_impl. (__timed_waiters::_M_do_wait_until): Use __condvar instead of __gthread_cond_t. * include/bits/atomic_wait.h: Remove <bits/unique_lock.h> include. Only include <bits/std_mutex.h> if not using futexes. (__platform_wait_max_value): Remove unused variable. (__waiters::lock_t): Use lock_guard instead of unique_lock. (__waiters::_M_cv): Use __condvar instead of __gthread_cond_t. (__waiters::_M_do_wait(__platform_wait_t)): Likewise. (__waiters::_M_notify()): Likewise. Use notify_one() if not asked to notify all. * include/bits/std_mutex.h (__condvar): New type. * include/std/condition_variable (condition_variable::_M_cond) (condition_variable::wait_until): Use __condvar instead of __gthread_cond_t. * src/c++11/condition_variable.cc (condition_variable): Define default constructor and destructor as defaulted. (condition_variable::wait, condition_variable::notify_one) (condition_variable::notify_all): Forward to corresponding member function of __condvar.
2020-11-24libstdc++: Throw instead of segfaulting in std::thread constructor [PR 67791]Jonathan Wakely1-0/+10
This turns a mysterious segfault into an exception with a more useful message. If the exception isn't caught, the user sees this instead of just a segfault: terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted Aborted (core dumped) libstdc++-v3/ChangeLog: PR libstdc++/67791 * src/c++11/thread.cc (thread::_M_start_thread(_State_ptr, void (*)())): Check that gthreads is available before calling __gthread_create.
2020-11-24libstdc++: Use __libc_single_threaded for locale initializationJonathan Wakely2-3/+3
Most initialization of locales and facets happens before main() during startup, when the program is likely to only have one thread. By using the new __gnu_cxx::__is_single_threaded() function instead of checking __gthread_active_p() we can avoid using pthread_once or atomics for the common case. That said, I'm not sure why we don't just use a local static variable instead, as __cxa_guard_acquire() already optimizes for the single-threaded case: static const bool init = (_S_initialize_once(), true); I'll revisit that for GCC 12. libstdc++-v3/ChangeLog: * src/c++98/locale.cc (locale::facet::_S_get_c_locale()) (locale::id::_M_id() const): Use __is_single_threaded. * src/c++98/locale_init.cc (locale::_S_initialize()): Likewise.
2020-11-19libstdc++: Move std::thread to a new headerJonathan Wakely1-0/+1
This makes it possible to use std::thread without including the whole of <thread>. It also makes this_thread::get_id() and this_thread::yield() available even when there is no gthreads support (e.g. when GCC is built with --disable-threads or --enable-threads=single). In order for the std::thread::id return type of this_thread::get_id() to be defined, std:thread itself is defined unconditionally. However the constructor that creates new threads is not defined for single-threaded builds. The thread::join() and thread::detach() member functions are defined inline for single-threaded builds and just throw an exception (because we know the thread cannot be joinable if the constructor that creates joinable threads doesn't exit). The thread::hardware_concurrency() member function is also defined inline and returns 0 (as suggested by the standard when the value "is not computable or well-defined"). The main benefit for most targets is that other headers such as <future> do not need to include the whole of <thread> just to be able to create a std::thread. That avoids including <stop_token> and std::jthread where not required. This is another partial fix for PR 92546. This also means we can use this_thread::get_id() and this_thread::yield() in <stop_token> instead of using the gthread functions directly. This removes some preprocessor conditionals, simplifying the code. libstdc++-v3/ChangeLog: PR libstdc++/92546 * include/Makefile.am: Add new <bits/std_thread.h> header. * include/Makefile.in: Regenerate. * include/std/future: Include new header instead of <thread>. * include/std/stop_token: Include new header instead of <bits/gthr.h>. (stop_token::_S_yield()): Use this_thread::yield(). (_Stop_state_t::_M_requester): Change type to std::thread::id. (_Stop_state_t::_M_request_stop()): Use this_thread::get_id(). (_Stop_state_t::_M_remove_callback(_Stop_cb*)): Likewise. Use __is_single_threaded() to decide whether to synchronize. * include/std/thread (thread, operator==, this_thread::get_id) (this_thread::yield): Move to new header. (operator<=>, operator!=, operator<, operator<=, operator>) (operator>=, hash<thread::id>, operator<<): Define even when gthreads not available. * src/c++11/thread.cc: Include <memory>. * include/bits/std_thread.h: New file. (thread, operator==, this_thread::get_id, this_thread::yield): Define even when gthreads not available. [!_GLIBCXX_HAS_GTHREADS] (thread::join, thread::detach) (thread::hardware_concurrency): Define inline.
2020-11-19libstdc++: Fix overflow checks to use the correct "time_t" [PR 93456]Jonathan Wakely1-6/+8
I recently added overflow checks to src/c++11/futex.cc for PR 93456, but then changed the type of the timespec for PR 93421. This meant the overflow checks were no longer using the right range, because the variable being written to might be smaller than time_t. This introduces new typedef that corresponds to the tv_sec member of the struct being passed to the syscall, and uses that typedef in the range checks. libstdc++-v3/ChangeLog: PR libstdc++/93421 PR libstdc++/93456 * src/c++11/futex.cc (syscall_time_t): New typedef for the type of the syscall_timespec::tv_sec member. (relative_timespec, _M_futex_wait_until) (_M_futex_wait_until_steady): Use syscall_time_t in overflow checks, not time_t.
2020-11-17libstdc++: Revert changes for SYS_clock_gettime64 [PR 93421]Jonathan Wakely2-19/+5
As discussed in the PR, it's incredibly unlikely that a system that needs to use the SYS_clock_gettime syscall (e.g. glibc 2.16 or older) is going to define the SYS_clock_gettime64 macro. Ancient systems that need to use the syscall aren't going to have time64 support. This reverts the recent changes to try and make clock_gettime syscalls be compatible with systems that have been updated for time64 (those changes were wrong anyway as they misspelled the SYS_clock_gettime64 macro). The changes for futex syscalls are retained, because we still use them on modern systems that might be using time64. To ensure that the clock_gettime syscalls are safe, configure will fail if SYS_clock_gettime is needed, and SYS_clock_gettime64 is also defined (but to a distinct value from SYS_clock_gettime), and the tv_sec member of timespec is larger than long. This means we will be unable to build on a hypothetical system where we need the time32 version of SYS_clock_gettime but where userspace is using a time64 struct timespec. In the unlikely event that this failure is triggered on any real systems, we can fix it later. But we probably won't need to. libstdc++-v3/ChangeLog: PR libstdc++/93421 * acinclude.m4 (GLIBCXX_ENABLE_LIBSTDCXX_TIME): Fail if struct timespec isn't compatible with SYS_clock_gettime. * configure: Regenerate. * src/c++11/chrono.cc: Revert changes for time64 compatibility. Add static_assert instead. * src/c++11/futex.cc (_M_futex_wait_until_steady): Assume SYS_clock_gettime can use struct timespec.
2020-11-16libstdc++: Fix error shown during Solaris buildJonathan Wakely2-4/+4
Currently this is shown when building libstdc++ on Solaris: -lrt: open: No such file or directory The error comes from the make_sunver.pl script which tries to open each of its arguments. The arguments are passed by this make rule: perl ${glibcxx_srcdir}/scripts/make_exports.pl \ libstdc++-symbols.ver \ $(libstdc___la_OBJECTS:%.lo=.libs/%.o) \ `echo $(libstdc___la_LIBADD) | \ sed 's,/\([^/.]*\)\.la,/.libs/\1.a,g'` \ > $@ || (rm -f $@ ; exit 1) The $(libstdc___la_LIBADD) variable includes $(GLIBCXX_LIBS) which contains -lrt on Solaris. This patch adds another sed script to filter -l arguments from the echo command. In order to reliably match ' -l[^ ]* ' the echo arguments are quoted and a space added before and after them. This might be overkill just to remove -lrt from the start of the string, but should be robust in case other -l arguments are added to $(GLIBCXX_LIBS), or in case the $(libstdc___la_LIBADD) libraries are reordered. libstdc++-v3/ChangeLog: * src/Makefile.am (libstdc++-symbols.ver-sun): Remove -lrt from arguments passed to make_sunver.pl script. * src/Makefile.in: Regenerate.
2020-11-13libstdc++: Use custom timespec in system calls [PR 93421]Jonathan Wakely2-8/+32
On 32-bit targets where userspace has switched to 64-bit time_t, we cannot pass struct timespec to SYS_futex or SYS_clock_gettime, because the userspace definition of struct timespec will not match what the kernel expects. We use the existence of the SYS_futex_time64 or SYS_clock_gettime_time64 macros to imply that userspace *might* have switched to the new timespec definition. This is a conservative assumption. It's possible that the new syscall numbers are defined in the libc headers but that timespec hasn't been updated yet (as is the case for glibc currently). But using the alternative struct with two longs is still OK, it's just redundant if userspace timespec still uses a 32-bit time_t. We also check that SYS_futex_time64 != SYS_futex so that we don't try to use a 32-bit tv_sec on modern targets that only support the 64-bit system calls and define the old macro to the same value as the new one. We could possibly check #ifdef __USE_TIME_BITS64 to see whether userspace has actually been updated, but it's not clear if user code is meant to inspect that or if it's only for libc internal use. libstdc++-v3/ChangeLog: PR libstdc++/93421 * src/c++11/chrono.cc [_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL] (syscall_timespec): Define a type suitable for SYS_clock_gettime calls. (system_clock::now(), steady_clock::now()): Use syscall_timespec instead of timespec. * src/c++11/futex.cc (syscall_timespec): Define a type suitable for SYS_futex and SYS_clock_gettime calls. (relative_timespec): Use syscall_timespec instead of timespec. (__atomic_futex_unsigned_base::_M_futex_wait_until): Likewise. (__atomic_futex_unsigned_base::_M_futex_wait_until_steady): Likewise.
2020-11-13libstdc++: Remove redundant overflow check for futex timeout [PR 93456]Jonathan Wakely1-12/+14
The relative_timespec function already checks for the case where the specified timeout is in the past, so the difference can never be negative. That means we dn't need to check if it's more negative than the minimum time_t value. libstdc++-v3/ChangeLog: PR libstdc++/93456 * src/c++11/futex.cc (relative_timespec): Remove redundant check negative values. * testsuite/30_threads/future/members/wait_until_overflow.cc: Moved to... * testsuite/30_threads/future/members/93456.cc: ...here.
2020-11-13libstdc++: Avoid more 32-bit time_t overflows in futex callsJonathan Wakely1-14/+22
This fixes another overflow in code converting a std::chrono::seconds duration to a time_t. This time in the new code using a futex wait with an absolute timeout (so this one doesn't need to be backported to the release branches). A timeout after the epochalypse would overflow the tv_sec field, producing an incorrect value. If that incorrect value happened to be negative, the syscall would return with EINVAL and then the caller would keep retrying, spinning until the timeout was reached. If the value happened to be positive, we would wake up too soon and incorrectly report a timeout libstdc++-v3/ChangeLog: * src/c++11/futex.cc (relative_timespec): Add [[unlikely]] attributes. (__atomic_futex_unsigned_base::_M_futex_wait_until) (__atomic_futex_unsigned_base::_M_futex_wait_until_steady): Check for overflow. * testsuite/30_threads/future/members/wait_until_overflow.cc: New test.
2020-11-13libstdc++: Avoid 32-bit time_t overflows in futex callsJonathan Wakely1-26/+53
The existing code doesn't check whether the chrono::seconds value is out of range of time_t. When using a timeout before the epoch (with a negative value) subtracting the current time (as time_t) and then assigning it to a time_t can overflow to a large positive value. This means that we end up waiting several years even though the specific timeout was in the distant past. We do have a check for negative timeouts, but that happens after the conversion to time_t so happens after the overflow. The conversion to a relative timeout is done in two places, so this factors it into a new function and adds the overflow checks there. libstdc++-v3/ChangeLog: * src/c++11/futex.cc (relative_timespec): New function to create relative time from two absolute times. (__atomic_futex_unsigned_base::_M_futex_wait_until) (__atomic_futex_unsigned_base::_M_futex_wait_until_steady): Use relative_timespec.
2020-11-12libstdc++: Optimise std::future::wait_for and fix futex pollingJonathan Wakely1-0/+9
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-10libstdc++: Add remaining C++20 additions to <sstream> [P0408R7]Jonathan Wakely1-0/+48
This adds the new overloads of basic_stringbuf::str, and the corresponding overloads to basic_istringstream, basic_ostringstream and basic_stringstream. libstdc++-v3/ChangeLog: * config/abi/pre/gnu.ver (GLIBCXX_3.4.21): Tighten patterns. (GLIBCXX_3.4.29): Export new symbols. * include/bits/alloc_traits.h (__allocator_like): New concept. * include/std/sstream (basic_stringbuf::swap): Add exception specification. (basic_stringbuf::str() const): Add ref-qualifier. Use new _M_high_mark function. (basic_stringbuf::str(const SAlloc&) const): Define new function. (basic_stringbuf::str() &&): Likewise. (basic_stringbuf::str(const basic_string<C,T,SAlloc>&)): Likewise. (basic_stringbuf::str(basic_string<C,T,Alloc>&&)): Likewise. (basic_stringbuf::view() const): Use _M_high_mark. (basic_istringstream::str, basic_ostringstream::str) (basic_stringstream::str): Define new overloads. * src/c++20/sstream-inst.cc (basic_stringbuf::str) (basic_istringstream::str, basic_ostringstream::str) (basic_stringstream::str): Explicit instantiation definitions for new overloads. * testsuite/27_io/basic_istringstream/view/char/1.cc: Add more checks. * testsuite/27_io/basic_istringstream/view/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_ostringstream/view/char/1.cc: Likewise. * testsuite/27_io/basic_ostringstream/view/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_stringstream/view/char/1.cc: Likewise. * testsuite/27_io/basic_stringstream/view/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_istringstream/str/char/2.cc: New test. * testsuite/27_io/basic_istringstream/str/wchar_t/2.cc: New test. * testsuite/27_io/basic_ostringstream/str/char/3.cc: New test. * testsuite/27_io/basic_ostringstream/str/wchar_t/3.cc: New test. * testsuite/27_io/basic_stringbuf/str/char/4.cc: New test. * testsuite/27_io/basic_stringbuf/str/wchar_t/4.cc: New test. * testsuite/27_io/basic_stringstream/str/char/5.cc: New test. * testsuite/27_io/basic_stringstream/str/wchar_t/5.cc.cc: New test.
2020-11-05libstdc++: Use non-throwing increment in recursive_directory_iterator [PR 97731]Jonathan Wakely1-7/+11
As described in the PR, the recursive_directory_iterator constructor calls advance(ec), but ec is a pointer so it calls _Dir::advance(bool). The intention was to either call advance() or advance(*ec) depending whether the pointer is null or not. This fixes the bug and renames the parameter to ecptr to make similar mistakes less likely in future. libstdc++-v3/ChangeLog: PR libstdc++/97731 * src/filesystem/dir.cc (recursive_directory_iterator): Call the right overload of _Dir::advance. * testsuite/experimental/filesystem/iterators/97731.cc: New test.
2020-11-05libstdc++: Export basic_stringbuf constructor [PR 97729]Jonathan Wakely1-0/+6
libstdc++-v3/ChangeLog: PR libstdc++/97729 * config/abi/pre/gnu.ver (GLIBCXX_3.4.29): Add exports. * src/c++20/sstream-inst.cc (basic_stringbuf): Instantiate private constructor taking __xfer_bufptrs.
2020-11-05libstdc++: Fix new <sstream> constructorsJonathan Wakely1-4/+2
- Add a missing 'explicit' to a basic_stringbuf constructor. - Set up the get/put area pointers in the constructor from strings using different allocator types. - Remove public basic_stringbuf::__sv_type alias. - Do not construct temporary basic_string objects with a default-constructed allocator. Also, change which basic_string constructor is used, as a minor compile-time optimization. Constructing from a basic_string_view requires more work from the compiler, so just use a pointer and length. libstdc++-v3/ChangeLog: * include/std/sstream (basic_stringbuf(const allocator_type&): Add explicit. (basic_stringbuf(const basic_string<C,T,SA>&, openmode, const A&)): Call _M_stringbuf_init. Construct _M_string from pointer and length to avoid constraint checks for string view. (basic_stringbuf::view()): Make __sv_type alias local to the function. (basic_istringstream(const basic_string<C,T,SA>&, openmode, const A&)): Pass string to _M_streambuf instead of constructing a temporary with the wrong allocator. (basic_ostringstream(const basic_string<C,T,SA>&, openmode, const A&)): Likewise. (basic_stringstream(const basic_string<C,T,SA>&, openmode, const A&)): Likewise. * src/c++20/sstream-inst.cc: Use string_view and wstring_view typedefs in explicit instantiations. * testsuite/27_io/basic_istringstream/cons/char/1.cc: Add more tests for constructors. * testsuite/27_io/basic_ostringstream/cons/char/1.cc: Likewise. * testsuite/27_io/basic_stringbuf/cons/char/1.cc: Likewise. * testsuite/27_io/basic_stringbuf/cons/char/2.cc: Likewise. * testsuite/27_io/basic_stringbuf/cons/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_stringbuf/cons/wchar_t/2.cc: Likewise. * testsuite/27_io/basic_stringstream/cons/char/1.cc: Likewise.
2020-11-03libstdc++: Refactor std::call_once internalsJonathan Wakely1-29/+39
This separates the definition of std::__call_proxy into two funcions, one for TLS and one for non-TLS, to make them easier to read. It also replaces the __get_once_functor_lock_ptr() internal helper with a new set_lock_ptr(unique_lock<mutex>*) function so that __once_proxy doesn't need to call it twice. libstdc++-v3/ChangeLog: * src/c++11/mutex.cc [_GLIBCXX_HAVE_TLS] (__once_proxy): Define separately for TLS targets. [!_GLIBCXX_HAVE_TLS] (__get_once_functor_lock_ptr): Replace with ... (set_lock_ptr): ... this. Set new value and return previous value. [!_GLIBCXX_HAVE_TLS] (__set_once_functor_lock_ptr): Adjust to use set_lock_ptr. [!_GLIBCXX_HAVE_TLS] (__once_proxy): Likewise.
2020-11-03libstdc++: Rewrite std::call_once to use futexes [PR 66146]Jonathan Wakely1-0/+59
The current implementation of std::call_once uses pthread_once, which only meets the C++ requirements when compiled with support for exceptions. For most glibc targets and all non-glibc targets, pthread_once does not work correctly if the init_routine exits via an exception. The pthread_once_t object is left in the "active" state, and any later attempts to run another init_routine will block forever. This change makes std::call_once work correctly for Linux targets, by replacing the use of pthread_once with a futex, based on the code from __cxa_guard_acquire. For both glibc and musl, the Linux implementation of pthread_once is already based on futexes, and pthread_once_t is just a typedef for int, so this change does not alter the layout of std::once_flag. By choosing the values for the int appropriately, the new code is even ABI compatible. Code that calls the old implementation of std::call_once will use pthread_once to manipulate the int, while new code will use the new std::once_flag members to manipulate it, but they should interoperate correctly. In both cases, the int is initially zero, has the lowest bit set when there is an active execution, and equals 2 after a successful returning execution. The difference with the new code is that exceptional exceptions are correctly detected and the int is reset to zero. The __cxa_guard_acquire code (and musl's pthread_once) use an additional state to say there are other threads waiting. This allows the futex wake syscall to be skipped if there is no contention. Glibc doesn't use a waiter bit, so we have to unconditionally issue the wake in order to be compatible with code calling the old std::call_once that uses Glibc's pthread_once. If we know that we're using musl (and musl's pthread_once doesn't change) it would be possible to set a waiting state and check for it in std::once_flag::_M_finish(bool), but this patch doesn't do that. This doesn't fix the bug for non-linux targets. A similar approach could be used for targets where we know the definition of pthread_once_t is a mutex and an integer. We could make once_flag._M_activate() use pthread_mutex_lock on the mutex member within the pthread_once_t, and then only set the integer if the execution finishes, and then unlock the mutex. That would require careful study of each target's pthread_once implementation and that work is left for a later date. This also fixes PR 55394 because pthread_once is no longer needed, and PR 84323 because the fast path is now just an atomic load. As a consequence of the new implementation that doesn't use pthread_once, we can also make std::call_once work for targets with no gthreads support. The code for the single-threaded implementation follows the same methods as on Linux, but with no need for atomics or futexes. libstdc++-v3/ChangeLog: PR libstdc++/55394 PR libstdc++/66146 PR libstdc++/84323 * config/abi/pre/gnu.ver (GLIBCXX_3.4.29): Add new symbols. * include/std/mutex [!_GLIBCXX_HAS_GTHREADS] (once_flag): Define even when gthreads is not supported. (once_flag::_M_once) [_GLIBCXX_HAVE_LINUX_FUTEX]: Change type from __gthread_once_t to int. (once_flag::_M_passive(), once_flag::_M_activate()) (once_flag::_M_finish(bool), once_flag::_Active_execution): Define new members for futex and non-threaded implementation. [_GLIBCXX_HAS_GTHREADS] (once_flag::_Prepare_execution): New RAII helper type. (call_once): Use new members of once_flag. * src/c++11/mutex.cc (std::once_flag::_M_activate): Define. (std::once_flag::_M_finish): Define. * testsuite/30_threads/call_once/39909.cc: Do not require gthreads. * testsuite/30_threads/call_once/49668.cc: Likewise. * testsuite/30_threads/call_once/60497.cc: Likewise. * testsuite/30_threads/call_once/call_once1.cc: Likewise. * testsuite/30_threads/call_once/dr2442.cc: Likewise. * testsuite/30_threads/call_once/once_flag.cc: Add test for constexpr constructor. * testsuite/30_threads/call_once/66146.cc: New test. * testsuite/30_threads/call_once/constexpr.cc: Removed. * testsuite/30_threads/once_flag/cons/constexpr.cc: Removed.
2020-11-03libstdc++: use lt_host_flags for libstdc++.laJonathan Yong2-2/+2
For platforms like Mingw and Cygwin, cygwin refuses to generate the shared library without using -no-undefined. Attached patch makes sure the right flags are used, since libtool is already used to link libstdc++. libstdc++-v3/ChangeLog: * src/Makefile.am (libstdc___la_LINK): Add lt_host_flags. * src/Makefile.in: Regenerate.
2020-10-31libstdc++: Fix gnu-version-namespace buidFrançois Dumont2-4/+50
Co-authored-by: Jonathan Wakely <jwakely@redhat.com> libstdc++-v3/ChangeLog * src/c++17/floating_from_chars.cc (_GLIBCXX_USE_CX11_ABI): Add define. (buffering_string): New. [!_GLIBCXX_USE_CXX11_ABI](reserve_string): New. (from_chars): Adapt. * src/c++20/sstream-inst.cc: Limit instantiations to _GLIBCXX_USE_CXX11_ABI.
2020-10-31libstdc++: Use double for unordered container load factors [PR 96958]Jonathan Wakely1-6/+6
My previous commit for this PR changed the types from long double to double, but didn't change the uses of __builtin_ceill and __builtin_floorl. It also failed to change the non-inline functions in src/c++11/hashtable_c++0x.cc. This should fix it properly now. libstdc++-v3/ChangeLog: PR libstdc++/96958 * include/bits/hashtable_policy.h (_Prime_rehash_policy) (_Power2_rehash_policy): Use ceil and floor instead of ceill and floorl. * src/c++11/hashtable_c++0x.cc (_Prime_rehash_policy): Likewise. Use double instead of long double.
2020-10-28libstdc++: Implement C++20 features for <sstream>Thomas Rodgers5-9/+965
New ctors and ::view() accessor for - * basic_stingbuf * basic_istringstream * basic_ostringstream * basic_stringstreamm New ::get_allocator() accessor for basic_stringbuf. libstdc++-v3/ChangeLog: * acinclude.m4 (glibcxx_SUBDIRS): Add src/c++20. * config/abi/pre/gnu.ver (GLIBCXX_3.4.29): New symbols. * configure: Regenerate. * include/std/sstream: (basic_stringbuf::basic_stringbuf(allocator const&)): New constructor. (basic_stringbuf::basic_stringbuf(openmode, allocator const&)): Likewise. (basic_stringbuf::basic_stringbuf(basic_string&&, openmode)): Likewise. (basic_stringbuf::basic_stringbuf(basic_stringbuf&&, allocator const&)): Likewise. (basic_stringbuf::get_allocator()): New method. (basic_stringbuf::view()): Likewise. (basic_istringstream::basic_istringstream(basic_string&&, openmode)): New constructor. (basic_istringstream::basic_istringstream(openmode, allocator const&)): Likewise (basic_istringstream::view()): New method. (basic_ostringstream::basic_ostringstream(basic_string&&, openmode)): New constructor. (basic_ostringstream::basic_ostringstream(openmode, allocator const&)): Likewise (basic_ostringstream::view()): New method. (basic_stringstream::basic_stringstream(basic_string&&, openmode)): New constructor. (basic_stringstream::basic_stringstream(openmode, allocator const&)): Likewise (basic_stringstream::view()): New method. * src/Makefile.in: Add c++20 directory. * src/Makefile.am: Regenerate. * src/c++20/Makefile.am: Add makefile for new sub-directory. * src/c++20/Makefile.in: Generate. * src/c++20/sstream-inst.cc: New file defining explicit instantiations for basic_stringbuf, basic_istringstream, basic_ostringstream, and basic_stringstream member functions added in C++20. * testsuite/27_io/basic_stringbuf/cons/char/2.cc: New test. * testsuite/27_io/basic_stringbuf/cons/wchar_t/2.cc: Likewise. * testsuite/27_io/basic_stringbuf/view/char/1.cc: Likewise. * testsuite/27_io/basic_stringbuf/view/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_istringstream/cons/char/1.cc: Likewise. * testsuite/27_io/basic_istringstream/cons/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_istringstream/view/char/1.cc: Likewise. * testsuite/27_io/basic_istringstream/view/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_ostringstream/cons/char/1.cc: Likewise. * testsuite/27_io/basic_ostringstream/cons/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_ostringstream/view/char/1.cc: Likewise. * testsuite/27_io/basic_ostringstream/view/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_stringstream/cons/char/1.cc: Likewise. * testsuite/27_io/basic_stringstream/cons/wchar_t/1.cc: Likewise. * testsuite/27_io/basic_stringstream/view/char/1.cc: Likewise. * testsuite/27_io/basic_stringstream/view/wchar_t/1.cc: Likewise.
2020-10-27libstdc++: Fix directory_iterator exception specificationJonathan Wakely1-5/+1
libstdc++-v3/ChangeLog: * src/c++17/fs_dir.cc (fs::directory_iterator::operator*): Add noexcept. Do not throw on precondition violation.
2020-10-06libstdc++: Reduce uses of std::numeric_limitsJonathan Wakely1-1/+1
This avoids unnecessary instantiations of std::numeric_limits or inclusion of <limits> when a more lightweight alternative would work. Some uses can be replaced with __gnu_cxx::__int_traits and some can just use size_t(-1) directly where SIZE_MAX is needed. libstdc++-v3/ChangeLog: * include/bits/regex.h: Use __int_traits<int> instead of std::numeric_limits<int>. * include/bits/uniform_int_dist.h: Use __int_traits<T>::__max instead of std::numeric_limits<T>::max(). * include/bits/hashtable_policy.h: Use size_t(-1) instead of std::numeric_limits<size_t>::max(). * include/std/regex: Include <ext/numeric_traits.h>. * include/std/string_view: Use typedef for __int_traits<int>. * src/c++11/hashtable_c++0x.cc: Use size_t(-1) instead of std::numeric_limits<size_t>::max(). * testsuite/std/ranges/iota/96042.cc: Include <limits>. * testsuite/std/ranges/iota/difference_type.cc: Likewise. * testsuite/std/ranges/subrange/96042.cc: Likewise.
2020-10-05libstdc++: Make allocators throw bad_array_new_length on overflow [LWG 3190]Jonathan Wakely1-0/+4
std::allocator and std::pmr::polymorphic_allocator should throw std::bad_array_new_length from their allocate member functions if the number of bytes required cannot be represented in std::size_t. libstdc++-v3/ChangeLog: * config/abi/pre/gnu.ver: Add new symbol. * include/bits/functexcept.h (__throw_bad_array_new_length): Declare new function. * include/ext/malloc_allocator.h (malloc_allocator::allocate): Throw bad_array_new_length for impossible sizes (LWG 3190). * include/ext/new_allocator.h (new_allocator::allocate): Likewise. * include/std/memory_resource (polymorphic_allocator::allocate) (polymorphic_allocator::allocate_object): Use new function, __throw_bad_array_new_length. * src/c++11/functexcept.cc (__throw_bad_array_new_length): Define. * testsuite/20_util/allocator/lwg3190.cc: New test.
2020-09-22libstdc++: Fix out-of-bounds string_view access in filesystem::path [PR 97167]Jonathan Wakely1-1/+1
libstdc++-v3/ChangeLog: PR libstdc++/97167 * src/c++17/fs_path.cc (path::_Parser::root_path()): Check for empty string before inspecting the first character. * testsuite/27_io/filesystem/path/append/source.cc: Append empty string_view to path.
2020-09-11libstdc++: Support futex waiting on chrono::steady_clock directlyMike Crowe1-0/+82
The user-visible effect of this change is for std::future::wait_until to use CLOCK_MONOTONIC when passed a timeout of std::chrono::steady_clock type. This makes it immune to any changes made to the system clock CLOCK_REALTIME. Add an overload of __atomic_futex_unsigned::_M_load_and_text_until_impl that accepts a std::chrono::steady_clock, and correctly passes this through to __atomic_futex_unsigned_base::_M_futex_wait_until_steady which uses CLOCK_MONOTONIC for the timeout within the futex system call. These functions are mostly just copies of the std::chrono::system_clock versions with small tweaks. Prior to this commit, a std::chrono::steady timeout would be converted via std::chrono::system_clock which risks reducing or increasing the timeout if someone changes CLOCK_REALTIME whilst the wait is happening. (The commit immediately prior to this one increases the window of opportunity for that from a short period during the calculation of a relative timeout, to the entire duration of the wait.) FUTEX_WAIT_BITSET was added in kernel v2.6.25. If futex reports ENOSYS to indicate that this operation is not supported then the code falls back to using clock_gettime(2) to calculate a relative time to wait for. I believe that I've added this functionality in a way that it doesn't break ABI compatibility, but that has made it more verbose and less type safe. I believe that it would be better to maintain the timeout as an instance of the correct clock type all the way down to a single _M_futex_wait_until function with an overload for each clock. The current scheme of separating out the seconds and nanoseconds early risks accidentally calling the wait function for the wrong clock. Unfortunately, doing this would break code that compiled against the old header. libstdc++-v3/ChangeLog: * config/abi/pre/gnu.ver: Update for addition of __atomic_futex_unsigned_base::_M_futex_wait_until_steady. * include/bits/atomic_futex.h (__atomic_futex_unsigned_base): Add comments to clarify that _M_futex_wait_until and _M_load_and_test_until use CLOCK_REALTIME. (__atomic_futex_unsigned_base::_M_futex_wait_until_steady) (__atomic_futex_unsigned_base::_M_load_and_text_until_steady): New member functions that use CLOCK_MONOTONIC. (__atomic_futex_unsigned_base::_M_load_and_test_until_impl) (__atomic_futex_unsigned_base::_M_load_when_equal_until): Add overloads that accept a steady_clock time_point and use the new member functions. * src/c++11/futex.cc: Include headers required for clock_gettime. (futex_clock_monotonic_flag): New constant to tell futex to use CLOCK_MONOTONIC to match existing futex_clock_realtime_flag. (futex_clock_monotonic_unavailable): New global to store the result of trying to use CLOCK_MONOTONIC. (__atomic_futex_unsigned_base::_M_futex_wait_until_steady): Add new variant of _M_futex_wait_until that uses CLOCK_MONOTONIC to support waiting using steady_clock.
2020-09-11libstdc++: Use FUTEX_CLOCK_REALTIME for futex waitMike Crowe1-0/+37
The futex system call supports waiting for an absolute time if FUTEX_WAIT_BITSET is used rather than FUTEX_WAIT. Doing so provides two benefits: 1. The call to gettimeofday is not required in order to calculate a relative timeout. 2. If someone changes the system clock during the wait then the futex timeout will correctly expire earlier or later. Currently that only happens if the clock is changed prior to the call to gettimeofday. According to futex(2), support for FUTEX_CLOCK_REALTIME was added in the v2.6.28 Linux kernel and FUTEX_WAIT_BITSET was added in v2.6.25. To ensure that the code still works correctly with earlier kernel versions, an ENOSYS error from futex[1] results in the futex_clock_realtime_unavailable flag being set. This flag is used to avoid the unnecessary unsupported futex call in the future and to fall back to the previous gettimeofday and relative time implementation. glibc applied an equivalent switch in pthread_cond_timedwait to use FUTEX_CLOCK_REALTIME and FUTEX_WAIT_BITSET rather than FUTEX_WAIT for glibc-2.10 back in 2009. See glibc:cbd8aeb836c8061c23a5e00419e0fb25a34abee7 The futex_clock_realtime_unavailable flag is accessed using std::memory_order_relaxed to stop it becoming a bottleneck. If the first two calls to _M_futex_wait_until happen to happen simultaneously then the only consequence is that both will try to use FUTEX_CLOCK_REALTIME, both risk discovering that it doesn't work and, if so, both set the flag. [1] This is how glibc's nptl-init.c determines whether these flags are supported. libstdc++-v3/ChangeLog: * src/c++11/futex.cc: Add new constants for required futex flags. Add futex_clock_realtime_unavailable flag to store result of trying to use FUTEX_CLOCK_REALTIME. (__atomic_futex_unsigned_base::_M_futex_wait_until): Try to use FUTEX_WAIT_BITSET with FUTEX_CLOCK_REALTIME and only fall back to using gettimeofday and FUTEX_WAIT if that's not supported.
2020-09-10libstdc++: handle small max_blocks_per_chunk in pool resources [PR 94160]Jonathan Wakely1-6/+15
When a pool resource is constructed with max_blocks_per_chunk=1 it ends up creating a pool with blocks_per_chunk=0 which means it never allocates anything. Instead it returns null pointers, which should be impossible. To avoid this problem, round the max_blocks_per_chunk value to a multiple of four, so it's never smaller than four. libstdc++-v3/ChangeLog: PR libstdc++/94160 * src/c++17/memory_resource.cc (munge_options): Round max_blocks_per_chunk to a multiple of four. (__pool_resource::_M_alloc_pools()): Simplify slightly. * testsuite/20_util/unsynchronized_pool_resource/allocate.cc: Check that valid pointers are returned when small values are used for max_blocks_per_chunk.
2020-09-10libstdc++: Reduce monotonic_buffer_resource overallocation [PR 96942]Jonathan Wakely1-65/+91
The primary reason for this change is to reduce the size of buffers allocated by std::pmr::monotonic_buffer_resource. Previously, a new buffer would always add the size of the linked list node (11 bytes) and then round up to the next power of two. This results in a huge increase if the expected size of the next buffer is already a power of two. For example, if the resource is constructed with a desired initial size of 4096 the first buffer it allocates will be std::bit_ceil(4096+11) which is 8192. If the user has carefully selected the initial size to match their expected memory requirements then allocating double that amount wastes a lot of memory. After this patch the allocated size will be rounded up to a 64-byte boundary, instead of to a power of two. This means for an initial size of 4096 only 4160 bytes get allocated. Previously only the base-2 logarithm of the size was stored, which could be stored in a single 8-bit integer. Now that the size isn't always a power of two we need to use more bits to store it. As the size is always a multiple of 64 the low six bits are not needed, and so we can use the same approach that the pool resources already use of storing the base-2 logarithm of the alignment in the low bits that are not used for the size. To avoid code duplication, a new aligned_size<N> helper class is introduced by this patch, which is then used by both the pool resources' big_block type and the monotonic_buffer_resource::_Chunk type. Originally the big_block type used two bit-fields to store the size and alignment in the space of a single size_t member. The aligned_size type uses a single size_t member and uses masks and bitwise operations to manipulate the size and alignment values. This results in better code than the old version, because the bit-fields weren't optimally ordered for little endian architectures, so the alignment was actually stored in the high bits, not the unused low bits, requiring additional shifts to calculate the values. Using bitwise operations directly avoids needing to reorder the bit-fields depending on the endianness. While adapting the _Chunk and big_block types to use aligned_size<N> I also added checks for size overflows (technically, unsigned wraparound). The memory resources now ensure that when they require an allocation that is too large to represent in size_t they will request SIZE_MAX bytes from the upstream resource, rather than requesting a small value that results from wrapround. The testsuite is enhanced to verify this. libstdc++-v3/ChangeLog: PR libstdc++/96942 * include/std/memory_resource (monotonic_buffer_resource::do_allocate): Use __builtin_expect when checking if a new buffer needs to be allocated from the upstream resource, and for checks for edge cases like zero sized buffers and allocations. * src/c++17/memory_resource.cc (aligned_size): New class template. (aligned_ceil): New helper function to round up to a given alignment. (monotonic_buffer_resource::chunk): Replace _M_size and _M_align with an aligned_size member. Remove _M_canary member. Change _M_next to pointer instead of unaligned buffer. (monotonic_buffer_resource::chunk::allocate): Round up to multiple of 64 instead of to power of two. Check for size overflow. Remove redundant check for minimum required alignment. (monotonic_buffer_resource::chunk::release): Adjust for changes to data members. (monotonic_buffer_resource::_M_new_buffer): Use aligned_ceil. (big_block): Replace _M_size and _M_align with aligned_size member. (big_block::big_block): Check for size overflow. (big_block::size, big_block::align): Adjust to use aligned_size. (big_block::alloc_size): Use aligned_ceil. (munge_options): Use aligned_ceil. (__pool_resource::allocate): Use big_block::align for alignment. * testsuite/20_util/monotonic_buffer_resource/allocate.cc: Check upstream resource gets expected values for impossible sizes. * testsuite/20_util/unsynchronized_pool_resource/allocate.cc: Likewise. Adjust checks for expected alignment in existing test.
2020-08-11libstdc++: Make std::this_thread functions work without gthreadsJonathan Wakely1-14/+19
The only function in namespace std::this_thread that actually depends on thread support being present is this_thread::get_id(). The other functions (yield, sleep_for and sleep_until) can be defined for targets without gthreads. A small change is needed in std::this_thread::sleep_for which currently uses the __gthread_time_t typedef. Since it just calls nanosleep directly, it should use timespec directly instead of the typedef. Even std::this_thread::get_id() could be made to work, the only difficulty is that it returns a value of type std::thread::id and std::thread is only defined when gthreads support exists. libstdc++-v3/ChangeLog: * include/std/thread [!_GLIBCXX_HAS_GTHREADS] (this_thread::yield) (this_thread::sleep_until): Define. [!_GLIBCXX_HAS_GTHREADS] (this_thread::sleep_for): Define. Replace use of __gthread_time_t typedef with timespec. * src/c++11/thread.cc [!_GLIBCXX_HAS_GTHREADS] (__sleep_for): Likewise. * testsuite/30_threads/this_thread/2.cc: Moved to... * testsuite/30_threads/this_thread/yield.cc: ...here. * testsuite/30_threads/this_thread/3.cc: Moved to... * testsuite/30_threads/this_thread/sleep_for-mt.cc: ...here. * testsuite/30_threads/this_thread/4.cc: Moved to... * testsuite/30_threads/this_thread/sleep_until-mt.cc: ...here. * testsuite/30_threads/this_thread/58038.cc: Add dg-require-sleep. * testsuite/30_threads/this_thread/60421.cc: Likewise. * testsuite/30_threads/this_thread/sleep_for.cc: New test. * testsuite/30_threads/this_thread/sleep_until.cc: New test.
2020-08-10libstdc++: Fix build for targets without lstat [PR 94681]Jonathan Wakely2-2/+2
libstdc++-v3/ChangeLog: PR libstdc++/94681 * src/c++17/fs_ops.cc (read_symlink): Use posix::lstat instead of calling ::lstat directly. * src/filesystem/ops.cc (read_symlink): Likewise.
2020-08-10libstdc++: Use _wstat64 for Windows [PR 95749]Jonathan Wakely1-3/+3
In order to handle large files on Windows we need to use stat API with 64-bit st_sioze member. libstdc++-v3/ChangeLog: PR libstdc++/95749 * src/filesystem/ops-common.h [_GLIBCXX_FILESYSTEM_IS_WINDOWS] (stat_type): Change to __wstat64. (stat): Use _wstat64.
2020-08-06libstdc++: Do not set eofbit eagerly in operator>>(istream&, char(&)[N])Jonathan Wakely2-2/+3
Similar to the bugs I fixed recently in istream::ignore, we incorrectly set eofbit too often in operator>>(istream&, string&) and operator>>(istream&. char(&)[N]). We should only set eofbit if we reach EOF but would have kept going otherwise. If we've already extracted the maximum number of characters (whether that's because of the buffer size or the istream's width()) then we should not set eofbit. libstdc++-v3/ChangeLog: * include/bits/basic_string.tcc (operator>>(basic_istream&, basic_string&)): Do not set eofbit if extraction stopped after in.width() characters. * src/c++98/istream-string.cc (operator>>(istream&, string&)): Likewise. * include/bits/istream.tcc (__istream_extract): Do not set eofbit if extraction stopped after n-1 characters. * src/c++98/istream.cc (__istream_extract): Likewise. * testsuite/21_strings/basic_string/inserters_extractors/char/13.cc: New test. * testsuite/21_strings/basic_string/inserters_extractors/wchar_t/13.cc: New test. * testsuite/27_io/basic_istream/extractors_character/char/5.cc: New test. * testsuite/27_io/basic_istream/extractors_character/wchar_t/5.cc: New test.
2020-08-06libstdc++: Fix unnecessary allocations in read_symlink [PR 96484]Jonathan Wakely2-0/+12
libstdc++-v3/ChangeLog: PR libstdc++/96484 * src/c++17/fs_ops.cc (fs::read_symlink): Return an error immediately for non-symlinks. * src/filesystem/ops.cc (fs::read_symlink): Likewise.
2020-08-05libstdc++: Replace operator>>(istream&, char*) [LWG 2499]Jonathan Wakely2-7/+14
P0487R1 resolved LWG 2499 for C++20 by removing the operator>> overloads that have high risk of buffer overflows. They were replaced by equivalents that only accept a reference to an array, and so can guarantee not to write past the end of the array. In order to support both the old and new functionality, this patch introduces a new overloaded __istream_extract function which takes a maximum length. The new operator>> overloads use the array size as the maximum length. The old overloads now use __builtin_object_size to determine the available buffer size if available (which requires -O2) or use numeric_limits<streamsize>::max()/sizeof(char_type) otherwise. This is a change in behaviour, as the old overloads previously always used numeric_limits<streamsize>::max(), without considering sizeof(char_type) and without attempting to prevent overflows. Because they now do little more than call __istream_extract, the old operator>> overloads are very small inline functions. This means there is no advantage to explicitly instantiating them in the library (in fact that would prevent the __builtin_object_size checks from ever working). As a result, the explicit instantiation declarations can be removed from the header. The explicit instantiation definitions are still needed, for backwards compatibility with existing code that expects to link to the definitions in the library. While working on this change I noticed that src/c++11/istream-inst.cc has the following explicit instantiation definition: template istream& operator>>(istream&, char*); This had no effect (and so should not have been present in that file), because there was an explicit specialization declared in <istream> and defined in src/++98/istream.cc. However, this change removes the explicit specialization, and now the explicit instantiation definition is necessary to ensure the symbol gets defined in the library. libstdc++-v3/ChangeLog: * config/abi/pre/gnu.ver (GLIBCXX_3.4.29): Export new symbols. * include/bits/istream.tcc (__istream_extract): New function template implementing both of operator>>(istream&, char*) and operator>>(istream&, char(&)[N]). Add explicit instantiation declaration for it. Remove explicit instantiation declarations for old function templates. * include/std/istream (__istream_extract): Declare. (operator>>(basic_istream<C,T>&, C*)): Define inline and simply call __istream_extract. (operator>>(basic_istream<char,T>&, signed char*)): Likewise. (operator>>(basic_istream<char,T>&, unsigned char*)): Likewise. (operator>>(basic_istream<C,T>&, C(7)[N])): Define for LWG 2499. (operator>>(basic_istream<char,T>&, signed char(&)[N])): Likewise. (operator>>(basic_istream<char,T>&, unsigned char(&)[N])): Likewise. * include/std/streambuf (basic_streambuf): Declare char overload of __istream_extract as a friend. * src/c++11/istream-inst.cc: Add explicit instantiation definition for wchar_t overload of __istream_extract. Remove explicit instantiation definitions of old operator>> overloads for versioned-namespace build. * src/c++98/istream.cc (operator>>(istream&, char*)): Replace with __istream_extract(istream&, char*, streamsize). * testsuite/27_io/basic_istream/extractors_character/char/3.cc: Do not use variable-length array. * testsuite/27_io/basic_istream/extractors_character/char/4.cc: Do not run test for C++20. * testsuite/27_io/basic_istream/extractors_character/char/9555-ic.cc: Do not test writing to pointers for C++20. * testsuite/27_io/basic_istream/extractors_character/char/9826.cc: Use array instead of pointer. * testsuite/27_io/basic_istream/extractors_character/wchar_t/3.cc: Do not use variable-length array. * testsuite/27_io/basic_istream/extractors_character/wchar_t/4.cc: Do not run test for C++20. * testsuite/27_io/basic_istream/extractors_character/wchar_t/9555-ic.cc: Do not test writing to pointers for C++20. * testsuite/27_io/basic_istream/extractors_character/char/lwg2499.cc: New test. * testsuite/27_io/basic_istream/extractors_character/char/lwg2499_neg.cc: New test. * testsuite/27_io/basic_istream/extractors_character/char/overflow.cc: New test. * testsuite/27_io/basic_istream/extractors_character/wchar_t/lwg2499.cc: New test. * testsuite/27_io/basic_istream/extractors_character/wchar_t/lwg2499_neg.cc: New test.
2020-07-31libstdc++: Fix use of newlocale in std:::from_charsJonathan Wakely1-1/+1
libstdc++-v3/ChangeLog: * src/c++17/floating_from_chars.cc (from_chars_impl): Use LC_ALL_MASK not LC_ALL.
2020-07-30libstdc++: Check _GLIBCXX_USE_C99_STDLIB for strtof and strtoldJonathan Wakely1-2/+6
On broken systems we only have strtod, not strtof and strtold. Just use strtod for all types, even though that will produce incorrect results in some cases. Similarly, if _GLIBCXX_USE_C99_MATH is not defined then std::isinf won't be declared. Just refer to it unqualified, which should find the C library's isinf macro if that hasn't been #undef'd by <cmath>. libstdc++-v3/ChangeLog: * src/c++17/floating_from_chars.cc (from_chars_impl): Use isinf unqualified. [!_GLIBCXX_USE_C99_STDLIB]: Use strtod for float and long double.
2020-07-27libstdc++: Make std::from_chars always round to nearestJonathan Wakely1-0/+12
Also fix the tests that fail on targets without uselocale. libstdc++-v3/ChangeLog: * src/c++17/floating_from_chars.cc (from_chars_impl): Ensure that FE_NEAREST is used. * testsuite/20_util/from_chars/4.cc: Do not use if constexpr in a { target c++14 } test. [!_GLIBCXX_HAVE_USELOCALE]: Disable all tests. * testsuite/20_util/from_chars/5.cc [!_GLIBCXX_HAVE_USELOCALE]: Likewise. * testsuite/20_util/from_chars/6.cc: New test.
2020-07-20libstdc++: Add std::from_chars for floating-point typesJonathan Wakely3-2/+426
This adds the missing std::from_chars overloads for floating-point types, as required for C++17 conformance. The implementation is a hack and not intended to be used in the long term. Rather than parsing the string directly, this determines the initial portion of the string that matches the pattern determined by the chars_format parameter, then creates a NTBS to be parsed by strtod (or strtold or strtof). Because creating a NTBS requires allocating memory, but std::from_chars is noexcept, we need to be careful to minimise allocation. Even after being careful, allocation failure is still possible, and so a non-conforming std::no_more_memory error code might be returned. Because strtod et al depend on the current locale, but std::from_chars does not, we change the current thread's locale to "C" using newlocale and uselocale before calling strtod, and restore it afterwards. Because strtod doesn't have the equivalent of a std::chars_format parameter, it has to examine the input to determine the format in use, even though the std::from_chars code has already parsed it once (or twice for large input strings!) By replacing the use of strtod we could avoid allocation, avoid changing locale, and use optimised code paths specific to each std::chars_format case. We would also get more portable behaviour, rather than depending on the presence of uselocale, and on any bugs or quirks of the target libc's strtod. Replacing strtod is a project for a later date. libstdc++-v3/ChangeLog: * acinclude.m4 (libtool_VERSION): Bump version. * config.h.in: Regenerate. * config/abi/pre/gnu.ver: Add GLIBCXX_3.4.29 version and new exports. * config/os/gnu-linux/ldbl-extra.ver: Add _GLIBCXX_LDBL_3.4.29 version and new export. * configure: Regenerate. * configure.ac: Check for <xlocale.h> and uselocale. * crossconfig.m4: Add macro or checks for uselocale. * include/std/charconv (from_chars): Declare overloads for float, double, and long double. * src/c++17/Makefile.am: Add new file. * src/c++17/Makefile.in: Regenerate. * src/c++17/floating_from_chars.cc: New file. (from_chars): Define for float, double, and long double. * testsuite/20_util/from_chars/1_c++20_neg.cc: Prune extra diagnostics caused by new overloads. * testsuite/20_util/from_chars/1_neg.cc: Likewise. * testsuite/20_util/from_chars/2.cc: Check leading '+'. * testsuite/20_util/from_chars/4.cc: New test. * testsuite/20_util/from_chars/5.cc: New test. * testsuite/util/testsuite_abi.cc: Add new symbol versions.
2020-07-13libstdc++: Fix istream::ignore exit conditions (PR 94749, PR 96161)Jonathan Wakely2-22/+66
My previous fix for PR 94749 did fix the reported case, so that the next character is not discarded if it happens to equal the delimiter when __n characters have already been read. But it introduced a new bug, which is that the delimiter character would *not* be discarded if the number of characters discarded is numeric_limits<streamsize>::max() or more before reaching the delimiter. The new bug happens because I changed the code to check _M_gcount < __n. But when __n == numeric_limits<streamsize>::max() that is false, and so we don't discard the delimiter. It's not sufficient to check for the delimiter when the __large_ignore condition is true, because there's an edge case where the delimiter is reached when _M_gcount == __n and so we break out of the loop without setting __large_ignore. PR 96161 is a similar bug to the original PR 94749 report, where eofbit is set after discarding __n characters if there happen to be no more characters in the stream. This patch fixes both cases (and the regression) by checking different conditions for the __n == max case and the __n < max case. For the former case, we know that we must have either reached the delimiter or EOF, and the value of _M_gcount doesn't matter (except to avoid integer overflow). For the latter case we need to check _M_gcount first and only set eofbit or discard the delimiter if it didn't reach __n. For the latter case overflow can't happen because _M_gcount <= __n < max. libstdc++-v3/ChangeLog: PR libstdc++/94749 PR libstdc++/96161 * include/bits/istream.tcc (basic_istream::ignore(streamsize)) [n == max]: Check overflow conditions on _M_gcount. Rely on the fact that either EOF or the delimiter was reached. [n < max]: Check _M_gcount < n before checking for EOF or delimiter. (basic_istream::ignore(streamsize, char_type): Likewise. * src/c++98/compatibility.cc (istream::ignore(streamsize)) (wistream::ignore(streamsize)): Likewise. * src/c++98/istream.cc (istream::ignore(streamsize, char_type)) (wistream::ignore(streamsize, char_type)): Likewise. * testsuite/27_io/basic_istream/ignore/char/94749.cc: Check that delimiter is discarded if the number of characters ignored doesn't fit in streamsize. * testsuite/27_io/basic_istream/ignore/wchar_t/94749.cc: Likewise. * testsuite/27_io/basic_istream/ignore/char/96161.cc: New test. * testsuite/27_io/basic_istream/ignore/wchar_t/96161.cc: New test.