summaryrefslogtreecommitdiff
path: root/libcxx/src/thread.cpp
AgeCommit message (Collapse)Author
2025-11-21[libc++][Windows] Enable thread::hardware_concurrency to support more than ↵Yexuan Xiao
64 processors (#168229) Starting with Windows 11, processes can utilize more than 64 processors by default, but GetSystemInfo can only report a maximum of 64. Starting with Windows Vista, GetActiveProcessorCount accurately retrieves the total number of processors on the current system. I’ve implemented similar improvements to Microsoft STL. The following links contain additional background information: https://github.com/microsoft/STL/issues/5453, https://github.com/microsoft/STL/pull/5459 (note: Reason STL uses the more complex GetLogicalProcessorInformationEx: https://github.com/microsoft/STL/pull/5459#discussion_r2072242241.).
2025-05-28Revert "[libc++] Introduce ABI sensitive areas to avoid requiring ↵James Y Knight
_LIBCPP_HIDE_FROM_ABI everywhere (#131156)" (#141756) This reverts commit c861fe8a71e64f3d2108c58147e7375cd9314521. Unfortunately, this use of hidden visibility attributes causes user-defined specializations of standard-library types to also be marked hidden by default, which is incorrect. See discussion thread on #131156. ...and also reverts the follow-up commits: Revert "[libc++] Add explicit ABI annotations to functions from the block runtime declared in <__functional/function.h> (#140592)" This reverts commit 3e4c9dc299c35155934688184319d391b298fff7. Revert "[libc++] Make ABI annotations explicit for windows-specific code (#140507)" This reverts commit f73287e623a6c2e4a3485832bc3e10860cd26eb5. Revert "[libc++][NFC] Replace a few "namespace std" with the correct macro (#140510)" This reverts commit 1d411f27c769a32cb22ce50b9dc4421e34fd40dd.
2025-05-18[libc++] Introduce ABI sensitive areas to avoid requiring ↵Nikolas Klauser
_LIBCPP_HIDE_FROM_ABI everywhere (#131156) This patch introduces `_LIBCPP_{BEGIN,END}_EXPLICIT_ABI_ANNOTATIONS`, which allow us to have implicit annotations for most functions, and just where it's not "hide_from_abi everything" we add explicit annotations. This allows us to drop the `_LIBCPP_HIDE_FROM_ABI` macro from most functions in libc++.
2025-05-12[libc++] Fix missing #includes (#130536)Matt
Adds missing includes that were detected when I tried to build libc++ as a module. Working towards #127012
2025-02-21[libc++] Qualify calls to nullary functions like __throw_foo (#122465)Louis Dionne
This is technically not necessary in most cases to prevent issues with ADL, but let's be consistent. This allows us to remove the libcpp-qualify-declval clang-tidy check, which is now enforced by the robust-against-adl clang-tidy check.
2023-12-18[libc++] Format the code base (#74334)Louis Dionne
This patch runs clang-format on all of libcxx/include and libcxx/src, in accordance with the RFC discussed at [1]. Follow-up patches will format the benchmarks, the test suite and remaining parts of the code. I'm splitting this one into its own patch so the diff is a bit easier to review. This patch was generated with: find libcxx/include libcxx/src -type f \ | grep -v 'module.modulemap.in' \ | grep -v 'CMakeLists.txt' \ | grep -v 'README.txt' \ | grep -v 'libcxx.imp' \ | grep -v '__config_site.in' \ | xargs clang-format -i A Git merge driver is available in libcxx/utils/clang-format-merge-driver.sh to help resolve merge and rebase issues across these formatting changes. [1]: https://discourse.llvm.org/t/rfc-clang-formatting-all-of-libc-once-and-for-all
2023-11-05[libc++] Handle threads-related .cpp files like we do all other source files ↵Louis Dionne
(#71100) Source files in libc++ are added to the CMake targets only if they are required by the configuration. We do this pretty consistently for all configurations like no-filesystem, no-random-device, etc. but we didn't do it for no-threads. This patch makes this consistent for no-threads, which is helpful in reducing the amount of work required to port libc++ to some platforms without threads. Indeed, with the previous approach, several threads-related source files would end up including headers that might fail to compile properly on some platforms. This issue is sidestepped entirely by making the approach for no-threads consistent with the other configurations.
2023-06-17[libc++][NFC] Granularise <thread> headerHui
- This was to make implementing jthread easier and requested in https://reviews.llvm.org/D151559 Differential Revision: https://reviews.llvm.org/D151792
2023-01-11[libc++] Hold mutex lock while notify_all is called at notify_all_at_thread_exitArthur O'Dwyer
Releasing the mutex before the call to notify_all is an optimization. This optimization cannot be used here. The thread waiting on the condition might destroy the associated resources — mutex + condition variable — and the notifier thread will access an destroyed variable — the condition variable. In fact, notify_all_at_thread_exit is meant exactly to join on detached threads, and the waiting thread doesn't expect for the notifier thread to access any further shared resources, making this scenario very likely to happen. The waiting thread might awake spuriously on the release of the mutex lock. The reorder is necessary to prevent this race. Further details can be found at https://cplusplus.github.io/LWG/issue3343. Differential Revision: https://reviews.llvm.org/D105758
2022-04-25[libc++] Avoid lifetime UB in __thread_local_data()Vitaly Buka
Detected on many lld tests with -fsanitize-memory-use-after-dtor. Also https://lab.llvm.org/buildbot/#/builders/sanitizer-x86_64-linux-fast after D122869 will report a lot of them. Threads may outlive static variables. Even if ~__thread_specific_ptr() does nothing, lifetime of members ends with ~ and accessing the value is UB https://eel.is/c++draft/basic.life#1 ``` ==9214==WARNING: MemorySanitizer: use-of-uninitialized-value #0 0x557e1cec4539 in __libcpp_tls_set ../include/c++/v1/__threading_support:428:12 #1 0x557e1cec4539 in set_pointer ../include/c++/v1/thread:196:5 #2 0x557e1cec4539 in void* std::__msan::__thread_proxy< std::__msan::tuple<...>, llvm::parallel::detail::(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(llvm::ThreadPoolStrategy)::'lambda'()::operator()() const::'lambda'()> >(void*) ../include/c++/v1/thread:285:27 Memory was marked as uninitialized #0 0x557e10a0759d in __sanitizer_dtor_callback compiler-rt/lib/msan/msan_interceptors.cpp:940:5 #1 0x557e1d8c478d in std::__msan::__thread_specific_ptr<std::__msan::__thread_struct>::~__thread_specific_ptr() libcxx/include/thread:188:1 #2 0x557e10a07dc0 in MSanCxaAtExitWrapper(void*) compiler-rt/lib/msan/msan_interceptors.cpp:1151:3 ``` The test needs D123979 or -fsanitize-memory-param-retval enabled by default. Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D122864
2022-02-15[libc++] Replace `#include ""` with `<>` in libcxx/src/. NFCI.Arthur O'Dwyer
Our best guess is that the two syntaxes should have exactly equivalent effects, so, let's be consistent with what we do in libcxx/include/. I've left `#include "include/x.h"` and `#include "../y.h"` alone because I'm less sure that they're interchangeable, and they aren't inconsistent with libcxx/include/ because libcxx/include/ never does that kind of thing. Also, use the `_LIBCPP_PUSH_MACROS/POP_MACROS` dance for `<__undef_macros>`, even though it's technically unnecessary in a standalone .cpp file, just so we have consistently one way to do it. Differential Revision: https://reviews.llvm.org/D119561
2021-04-20[libc++] NFC: Normalize `#endif //` comment indentationLouis Dionne
2021-03-03[libc++/abi] Replace uses of _NOEXCEPT in src/ by noexceptLouis Dionne
We always build the libraries in a Standard mode that supports noexcept, so there's no need to use the _NOEXCEPT macro. Differential Revision: https://reviews.llvm.org/D97700
2020-11-26[libc++] Remove sysctl-based implementation of thread::hardware_concurrency()Louis Dionne
Using sysctl requires including headers that are considered internal on Linux, like <sys/sysctl.h> & friends. Instead, sysconf is defined by POSIX (and we have a fallback for Windows), so all the systems we support should be happy with just sysconf. Differential Revision: https://reviews.llvm.org/D92135
2020-10-05[libc++] Use __has_include instead of complex logic in thread.cppLouis Dionne
We might end up including more headers than strictly necessary this way, but it's much simpler and it makes it easier to port thread.cpp to systems not handled by the existing conditionals.
2020-05-14[libc++] Adjust how we guard the inclusion of unistd.hJohn Brawn
unistd.h isn't guaranteed to exist when the target isn't Windows, in particular if the target is bare-metal (i.e. no operating system). Handle this by using __has_include instead, though in filesystem/operations.cpp we already unconditionally include it so just remove the extra include. Differential Revision: https://reviews.llvm.org/D79784
2019-12-02[libcxx{,abi}] Emit deplibs only when detected by CMakeMichał Górny
This is a followup to 35bc5276ca3. It fixes the dependent libs usage in libcxx and libcxxabi to link pthread and rt libraries only if CMake detects them, rather than based on explicit platform blacklist. Differential Revision: https://reviews.llvm.org/D70888
2019-10-23[libcxx][NFC] Strip trailing whitespace, fix typo.Stephan T. Lavavej
2019-07-22[runtimes] Don't depend on libpthread on AndroidYi Kong
r362048 added support for ELF dependent libraries, but broke Android build since Android does not have libpthread. Remove the dependency on the Android build. Differential Revision: https://reviews.llvm.org/D65098 llvm-svn: 366734
2019-05-30[runtimes] Check if pragma comment(lib, ...) is supported firstPetr Hosek
This fixes the issue introduced by r362048 where we always use pragma comment(lib, ...) for dependent libraries when the compiler is Clang, but older Clang versions don't support this pragma so we need to check first if it's supported before using it. llvm-svn: 362055
2019-05-30[runtimes] Support ELF dependent libraries featurePetr Hosek
As of r360984, LLD supports dependent libraries feature for ELF. libunwind, libc++abi and libc++ have library dependencies: libdl librt and libpthread, which means that when libunwind and libc++ are being statically linked (using -static-libstdc++ flag), user has to manually specify -ldl -lpthread which is onerous. This change includes the lib pragma to specify the library dependencies directly in the source that uses those libraries. This doesn't make any difference when using linkers that don't support dependent libraries. However, when using LLD that has dependent libraries feature, users no longer have to manually specifying library dependencies when using static linking, linker will pick the library automatically. Differential Revision: https://reviews.llvm.org/D62090 llvm-svn: 362048
2019-05-01[WebAssembly] WASI support for libcxxDan Gohman
This adds explicit support for the WASI platform to libcxx. WASI libc uses some components from musl, however it's not fully compatible with musl, so we're planning to stop using _LIBCPP_HAS_MUSL_LIBC and customize for WASI libc specifically. Differential Revision: https://reviews.llvm.org/D61336 Reviewers: sbc100, ldionne llvm-svn: 359703
2019-01-19Update more file headers across all of the LLVM projects in the monorepoChandler Carruth
to reflect the new license. These used slightly different spellings that defeated my regular expressions. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351648
2018-11-13[libcxx] GNU/Hurd uses BSD-based interfaces, but does not (and won't) ↵Louis Dionne
provide <sys/sysctl.h> Reviewed as https://reviews.llvm.org/D54338. Thanks to sthibaul for the patch. llvm-svn: 346763
2017-05-31Fix Libc++ build with MinGW64Eric Fiselier
Summary: This patch corrects the build errors I encountered when building on MinGW64. Reviewers: mati865, rnk, compnerd, smeenai, bcraig Reviewed By: mati865, smeenai Subscribers: martell, chapuni, cfe-commits Differential Revision: https://reviews.llvm.org/D33082 llvm-svn: 304360
2017-05-10Remove usages of _LIBCPP_MSVC which is never definedEric Fiselier
llvm-svn: 302736
2017-03-01[libcxx] Support threads on FuchsiaPetr Hosek
Differential Revision: https://reviews.llvm.org/D30278 llvm-svn: 296573
2017-02-11Fix the build of thread.cpp on CloudABI.Ed Schouten
CloudABI does provide unistd.h, but doesn't define __unix__. We need to include this header file to make hardware_concurrency work. llvm-svn: 294832
2017-02-09Threading support: externalize sleep_for() function.Asiri Rathnayake
Different platforms implement the wait/sleep functions in difrerent ways. It makes sense to externalize this into the threading API. Differential revision: https://reviews.llvm.org/D29630 Reviewers: EricWF, joerg llvm-svn: 294573
2017-02-06Refer to _LIBCPP_MSVC macro where applicableSaleem Abdulrasool
Replace preprocess conditions of defined(_MSC_VER) && !defined(__clang__) with defined(_LIBCPP_MSVC). NFC. Patch by Dave Lee! llvm-svn: 294171
2017-01-16[libcxx] Follow-up to r292107Asiri Rathnayake
I've missed a couple of updates. NFC. llvm-svn: 292109
2017-01-07Ensure Sleep(...) isn't passed the value 0 on WindowsEric Fiselier
llvm-svn: 291342
2017-01-07thread: implement sleep_for on WindowsSaleem Abdulrasool
Windows does not provide an implementation of `nanosleep`. Round up the time duration to the nearest ms and use `Sleep`. Although this may over-sleep, there is no hard real-time guarantee on the wake, so sleeping a bit more is better than under-sleeping as it within the specification. llvm-svn: 291331
2017-01-03clean up use of _WIN32Saleem Abdulrasool
Replace the use of _WIN32 in libc++. Replace most use with a C runtime check _LIBCPP_MSVCRT or the new _LIBCPP_WIN32 to indicate that we are using the Win32 API. Use a new _LIBCPP_WCHAR_IS_UCS2 to indicate that we are on an environment that has a short wchar_t. llvm-svn: 290910
2016-08-25Add an _LIBCPP_NORETURN inline function named __throw_XXX for each exception ↵Marshall Clow
type we define. They either construct and throw the exception, or abort() (if exceptions are disabled). Use these functions everywhere instead of assert()ing when exceptions are disabled. WARNING: This is a behavior change - but only with exceptions disabled. Reviewed as: https://reviews.llvm.org/D23855. llvm-svn: 279744
2016-06-03[libcxx] Fix thread join.pass.cpp segfault after r271475Asiri Rathnayake
Some pthread implementations do not like being called pthead_join() with the pthread_t argument set to 0, and causes a segfault. This patch fixes this issue by validating the pthread_t argument before invoking pthread_join(). NFC. Differential revision: http://reviews.llvm.org/D20929 Change-Id: Ief817c57bd0e1f43cbaa03061e02417d6a180c38 Reviewers: EricWF llvm-svn: 271634
2016-05-06Refactor pthread usage of libcxx.Asiri Rathnayake
This patch extracts out all the pthread dependencies of libcxx into the new header __threading_support. The motivation is to make it easy to re-target libcxx into platforms that do not support pthread. Original patch from Fulvio Esposito (fulvio.esposito@outlook.com) - D11781 Applied with tweaks - D19412 Change-Id: I301111f0075de93dd8129416e06babc195aa936b llvm-svn: 268734
2016-01-29[libcxx] Whitelist inclusion of sysctl.h instead of blacklistingBen Craig
Instead of excluding all known operating systems that are not derived from BSD, I now include all operating systems that claim to be derived from BSD. Hopefully, that will make it so that this check doesn't need to change for every new operating system that comes along. http://reviews.llvm.org/D16634 llvm-svn: 259193
2015-03-10Don't include <sys/sysctl.h> on CloudABI.Ed Schouten
As CloudABI does not provide sysctl(), this header is not present. Make thread.cpp build correctly (and pass all tests) by not including the header. llvm-svn: 231768
2014-12-02libc++: support NaCl when building thread.cppJF Bastien
Summary: NaCl shouldn't include sysctl.h when trying to determine std::thread::hardware_concurrency, it should instead use sysconf(_SC_NPROCESSORS_ONLN) through unistd.h. No test needs to be changed, since hardware_concurrency.pass.cpp already tests that std::thread::hardware_concurrency > 0. Test Plan: make check-libcxx Reviewers: dschuff, danalbert Subscribers: jfb, cfe-commits Differential Revision: http://reviews.llvm.org/D6470 llvm-svn: 223128
2014-09-05Allow libc++ to be built on systems without POSIX threadsJonathan Roelofs
If you're crazy enough to want this sort of thing, then add -D_LIBCPP_HAS_NO_THREADS to your CXXFLAGS and --param=additiona_features=libcpp-has-no-threads to your lit commnad line. http://reviews.llvm.org/D3969 llvm-svn: 217271
2014-06-04Handle partial nanosleeps in this_thread::sleep_forDavid Majnemer
Signals may result in nanosleep returning with only some of the requested sleeping performed. Utilize nanosleep's "time-remaining" out parameter to continue sleeping when this occurs. llvm-svn: 210210
2014-01-04Switch to using C++ style casts.Joerg Sonnenberger
llvm-svn: 198505
2013-10-04G M: Provides the _LIBCPP_WARNING macro, to be used for MSVC only, since ↵Howard Hinnant
that compiler doesn't support #warning. llvm-svn: 191980
2013-08-14Xing Xue: port to IBM XLC++/AIX.Howard Hinnant
llvm-svn: 188396
2013-07-02Windows support in thread::hardware_concurrency.Howard Hinnant
llvm-svn: 185451
2013-06-30Matthew Dempsky: POSIX defines that the _POSIX_C_SOURCE macros are to be set ↵Howard Hinnant
by user code to specify what version of POSIX the system should provide. If you want to check what version of POSIX is actually available, you're supposed to test _POSIX_VERSION. However, since sysconf() has been in POSIX since 1995, it's probably safe to assume it's available on any system with a C++11 compiler, especially if _SC_NPROCESSORS_ONLN is defined too. So no point in a complicated preprocessor rule if just we unconditionally include <unistd.h> (on non-Windows systems). Also, I've added a #warning for to help porters detect when a suitable implementation isn't detected at compile-time. Howard: Matthew, can you patch CREDITS.TXT? Thanks. llvm-svn: 185275
2013-05-17Create a weak pthread_create reference on NetBSD to not force aJoerg Sonnenberger
dependency on libpthread for code that doesn't use threads itself. llvm-svn: 182161
2013-03-29Bruce Mitchener, Jr.: Port to emscripten. Fixes ↵Howard Hinnant
http://llvm.org/bugs/show_bug.cgi?id=15624. llvm-svn: 178354
2013-03-28Fix a few warnings/errors for compiling with -fno-exceptions.Howard Hinnant
llvm-svn: 178267