Path: blob/main/system/lib/libcxx/src/thread.cpp
6175 views
//===------------------------- thread.cpp----------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <__system_error/throw_system_error.h>9#include <__thread/poll_with_backoff.h>10#include <__thread/timed_backoff_policy.h>11#include <__utility/pair.h>12#include <exception>13#include <future>14#include <limits>15#include <thread>16#include <vector>1718#if __has_include(<unistd.h>)19# include <unistd.h> // for sysconf20#endif2122#if defined(__NetBSD__)23# pragma weak pthread_create // Do not create libpthread dependency24#endif2526#if defined(_LIBCPP_WIN32API)27# include <windows.h>28#endif2930#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)31# pragma comment(lib, "pthread")32#endif3334_LIBCPP_BEGIN_NAMESPACE_STD3536thread::~thread() {37if (!__libcpp_thread_isnull(&__t_))38terminate();39}4041void thread::join() {42int ec = EINVAL;43if (!__libcpp_thread_isnull(&__t_)) {44ec = __libcpp_thread_join(&__t_);45if (ec == 0)46__t_ = _LIBCPP_NULL_THREAD;47}4849if (ec)50std::__throw_system_error(ec, "thread::join failed");51}5253void thread::detach() {54int ec = EINVAL;55if (!__libcpp_thread_isnull(&__t_)) {56ec = __libcpp_thread_detach(&__t_);57if (ec == 0)58__t_ = _LIBCPP_NULL_THREAD;59}6061if (ec)62std::__throw_system_error(ec, "thread::detach failed");63}6465unsigned thread::hardware_concurrency() noexcept {66#if defined(_SC_NPROCESSORS_ONLN)67long result = sysconf(_SC_NPROCESSORS_ONLN);68// sysconf returns -1 if the name is invalid, the option does not exist or69// does not have a definite limit.70// if sysconf returns some other negative number, we have no idea71// what is going on. Default to something safe.72if (result < 0)73return 0;74return static_cast<unsigned>(result);75#elif defined(_LIBCPP_WIN32API)76SYSTEM_INFO info;77GetSystemInfo(&info);78return info.dwNumberOfProcessors;79#else // defined(CTL_HW) && defined(HW_NCPU)80// TODO: grovel through /proc or check cpuid on x86 and similar81// instructions on other architectures.82# if defined(_LIBCPP_WARNING)83_LIBCPP_WARNING("hardware_concurrency not yet implemented")84# else85# warning hardware_concurrency not yet implemented86# endif87return 0; // Means not computable [thread.thread.static]88#endif // defined(CTL_HW) && defined(HW_NCPU)89}9091namespace this_thread {9293void sleep_for(const chrono::nanoseconds& ns) {94if (ns > chrono::nanoseconds::zero()) {95__libcpp_thread_sleep_for(ns);96}97}9899} // namespace this_thread100101__thread_specific_ptr<__thread_struct>& __thread_local_data() {102// Even though __thread_specific_ptr's destructor doesn't actually destroy103// anything (see comments there), we can't call it at all because threads may104// outlive the static variable and calling its destructor means accessing an105// object outside of its lifetime, which is UB.106alignas(__thread_specific_ptr<__thread_struct>) static char __b[sizeof(__thread_specific_ptr<__thread_struct>)];107static __thread_specific_ptr<__thread_struct>* __p = new (__b) __thread_specific_ptr<__thread_struct>();108return *__p;109}110111// __thread_struct_imp112113template <class T>114class _LIBCPP_HIDDEN __hidden_allocator {115public:116typedef T value_type;117118T* allocate(size_t __n) { return static_cast<T*>(::operator new(__n * sizeof(T))); }119void deallocate(T* __p, size_t) { ::operator delete(static_cast<void*>(__p)); }120121size_t max_size() const { return size_t(~0) / sizeof(T); }122};123124class _LIBCPP_HIDDEN __thread_struct_imp {125typedef vector<__assoc_sub_state*, __hidden_allocator<__assoc_sub_state*> > _AsyncStates;126typedef vector<pair<condition_variable*, mutex*>, __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;127128_AsyncStates async_states_;129_Notify notify_;130131__thread_struct_imp(const __thread_struct_imp&);132__thread_struct_imp& operator=(const __thread_struct_imp&);133134public:135__thread_struct_imp() {}136~__thread_struct_imp();137138void notify_all_at_thread_exit(condition_variable* cv, mutex* m);139void __make_ready_at_thread_exit(__assoc_sub_state* __s);140};141142__thread_struct_imp::~__thread_struct_imp() {143for (_Notify::iterator i = notify_.begin(), e = notify_.end(); i != e; ++i) {144i->first->notify_all();145i->second->unlock();146}147for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end(); i != e; ++i) {148(*i)->__make_ready();149(*i)->__release_shared();150}151}152153void __thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m) {154notify_.push_back(pair<condition_variable*, mutex*>(cv, m));155}156157void __thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s) {158async_states_.push_back(__s);159__s->__add_shared();160}161162// __thread_struct163164__thread_struct::__thread_struct() : __p_(new __thread_struct_imp) {}165166__thread_struct::~__thread_struct() { delete __p_; }167168void __thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m) {169__p_->notify_all_at_thread_exit(cv, m);170}171172void __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s) { __p_->__make_ready_at_thread_exit(__s); }173174_LIBCPP_END_NAMESPACE_STD175176177