Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:04

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___THREAD_THREAD_H
0011 #define _LIBCPP___THREAD_THREAD_H
0012 
0013 #include <__assert>
0014 #include <__condition_variable/condition_variable.h>
0015 #include <__config>
0016 #include <__exception/terminate.h>
0017 #include <__functional/hash.h>
0018 #include <__functional/unary_function.h>
0019 #include <__memory/unique_ptr.h>
0020 #include <__mutex/mutex.h>
0021 #include <__system_error/throw_system_error.h>
0022 #include <__thread/id.h>
0023 #include <__thread/support.h>
0024 #include <__type_traits/decay.h>
0025 #include <__type_traits/enable_if.h>
0026 #include <__type_traits/is_same.h>
0027 #include <__type_traits/remove_cvref.h>
0028 #include <__utility/forward.h>
0029 #include <tuple>
0030 
0031 #if _LIBCPP_HAS_LOCALIZATION
0032 #  include <locale>
0033 #  include <sstream>
0034 #endif
0035 
0036 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0037 #  pragma GCC system_header
0038 #endif
0039 
0040 _LIBCPP_PUSH_MACROS
0041 #include <__undef_macros>
0042 
0043 _LIBCPP_BEGIN_NAMESPACE_STD
0044 
0045 #if _LIBCPP_HAS_THREADS
0046 
0047 template <class _Tp>
0048 class __thread_specific_ptr;
0049 class _LIBCPP_EXPORTED_FROM_ABI __thread_struct;
0050 class _LIBCPP_HIDDEN __thread_struct_imp;
0051 class __assoc_sub_state;
0052 
0053 _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();
0054 
0055 class _LIBCPP_EXPORTED_FROM_ABI __thread_struct {
0056   __thread_struct_imp* __p_;
0057 
0058   __thread_struct(const __thread_struct&);
0059   __thread_struct& operator=(const __thread_struct&);
0060 
0061 public:
0062   __thread_struct();
0063   ~__thread_struct();
0064 
0065   void notify_all_at_thread_exit(condition_variable*, mutex*);
0066   void __make_ready_at_thread_exit(__assoc_sub_state*);
0067 };
0068 
0069 template <class _Tp>
0070 class __thread_specific_ptr {
0071   __libcpp_tls_key __key_;
0072 
0073   // Only __thread_local_data() may construct a __thread_specific_ptr
0074   // and only with _Tp == __thread_struct.
0075   static_assert(is_same<_Tp, __thread_struct>::value, "");
0076   __thread_specific_ptr();
0077   friend _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();
0078 
0079   _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
0080 
0081 public:
0082   typedef _Tp* pointer;
0083 
0084   __thread_specific_ptr(const __thread_specific_ptr&)            = delete;
0085   __thread_specific_ptr& operator=(const __thread_specific_ptr&) = delete;
0086   ~__thread_specific_ptr();
0087 
0088   _LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); }
0089   _LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); }
0090   _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return get(); }
0091   void set_pointer(pointer __p);
0092 };
0093 
0094 template <class _Tp>
0095 void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) {
0096   delete static_cast<pointer>(__p);
0097 }
0098 
0099 template <class _Tp>
0100 __thread_specific_ptr<_Tp>::__thread_specific_ptr() {
0101   int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
0102   if (__ec)
0103     __throw_system_error(__ec, "__thread_specific_ptr construction failed");
0104 }
0105 
0106 template <class _Tp>
0107 __thread_specific_ptr<_Tp>::~__thread_specific_ptr() {
0108   // __thread_specific_ptr is only created with a static storage duration
0109   // so this destructor is only invoked during program termination. Invoking
0110   // pthread_key_delete(__key_) may prevent other threads from deleting their
0111   // thread local data. For this reason we leak the key.
0112 }
0113 
0114 template <class _Tp>
0115 void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) {
0116   _LIBCPP_ASSERT_INTERNAL(get() == nullptr, "Attempting to overwrite thread local data");
0117   std::__libcpp_tls_set(__key_, __p);
0118 }
0119 
0120 template <>
0121 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> {
0122   _LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT {
0123     return hash<__libcpp_thread_id>()(__v.__id_);
0124   }
0125 };
0126 
0127 #  if _LIBCPP_HAS_LOCALIZATION
0128 template <class _CharT, class _Traits>
0129 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0130 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) {
0131   // [thread.thread.id]/9
0132   //   Effects: Inserts the text representation for charT of id into out.
0133   //
0134   // [thread.thread.id]/2
0135   //   The text representation for the character type charT of an
0136   //   object of type thread::id is an unspecified sequence of charT
0137   //   such that, for two objects of type thread::id x and y, if
0138   //   x == y is true, the thread::id objects have the same text
0139   //   representation, and if x != y is true, the thread::id objects
0140   //   have distinct text representations.
0141   //
0142   // Since various flags in the output stream can affect how the
0143   // thread id is represented (e.g. numpunct or showbase), we
0144   // use a temporary stream instead and just output the thread
0145   // id representation as a string.
0146 
0147   basic_ostringstream<_CharT, _Traits> __sstr;
0148   __sstr.imbue(locale::classic());
0149   __sstr << __id.__id_;
0150   return __os << __sstr.str();
0151 }
0152 #  endif // _LIBCPP_HAS_LOCALIZATION
0153 
0154 class _LIBCPP_EXPORTED_FROM_ABI thread {
0155   __libcpp_thread_t __t_;
0156 
0157   thread(const thread&);
0158   thread& operator=(const thread&);
0159 
0160 public:
0161   typedef __thread_id id;
0162   typedef __libcpp_thread_t native_handle_type;
0163 
0164   _LIBCPP_HIDE_FROM_ABI thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
0165 #  ifndef _LIBCPP_CXX03_LANG
0166   template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> = 0>
0167   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp&& __f, _Args&&... __args);
0168 #  else // _LIBCPP_CXX03_LANG
0169   template <class _Fp>
0170   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp __f);
0171 #  endif
0172   ~thread();
0173 
0174   _LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; }
0175 
0176   _LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT {
0177     if (!__libcpp_thread_isnull(&__t_))
0178       terminate();
0179     __t_     = __t.__t_;
0180     __t.__t_ = _LIBCPP_NULL_THREAD;
0181     return *this;
0182   }
0183 
0184   _LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); }
0185 
0186   _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); }
0187   void join();
0188   void detach();
0189   _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); }
0190   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; }
0191 
0192   static unsigned hardware_concurrency() _NOEXCEPT;
0193 };
0194 
0195 #  ifndef _LIBCPP_CXX03_LANG
0196 
0197 template <class _TSp, class _Fp, class... _Args, size_t... _Indices>
0198 inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) {
0199   std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...);
0200 }
0201 
0202 template <class _Fp>
0203 _LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) {
0204   // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
0205   unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
0206   __thread_local_data().set_pointer(std::get<0>(*__p.get()).release());
0207   typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
0208   std::__thread_execute(*__p.get(), _Index());
0209   return nullptr;
0210 }
0211 
0212 template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> >
0213 thread::thread(_Fp&& __f, _Args&&... __args) {
0214   typedef unique_ptr<__thread_struct> _TSPtr;
0215   _TSPtr __tsp(new __thread_struct);
0216   typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp;
0217   unique_ptr<_Gp> __p(new _Gp(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
0218   int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
0219   if (__ec == 0)
0220     __p.release();
0221   else
0222     __throw_system_error(__ec, "thread constructor failed");
0223 }
0224 
0225 #  else // _LIBCPP_CXX03_LANG
0226 
0227 template <class _Fp>
0228 struct __thread_invoke_pair {
0229   // This type is used to pass memory for thread local storage and a functor
0230   // to a newly created thread because std::pair doesn't work with
0231   // std::unique_ptr in C++03.
0232   _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
0233   unique_ptr<__thread_struct> __tsp_;
0234   _Fp __fn_;
0235 };
0236 
0237 template <class _Fp>
0238 _LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) {
0239   unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
0240   __thread_local_data().set_pointer(__p->__tsp_.release());
0241   (__p->__fn_)();
0242   return nullptr;
0243 }
0244 
0245 template <class _Fp>
0246 thread::thread(_Fp __f) {
0247   typedef __thread_invoke_pair<_Fp> _InvokePair;
0248   typedef unique_ptr<_InvokePair> _PairPtr;
0249   _PairPtr __pp(new _InvokePair(__f));
0250   int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
0251   if (__ec == 0)
0252     __pp.release();
0253   else
0254     __throw_system_error(__ec, "thread constructor failed");
0255 }
0256 
0257 #  endif // _LIBCPP_CXX03_LANG
0258 
0259 inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); }
0260 
0261 #endif // _LIBCPP_HAS_THREADS
0262 
0263 _LIBCPP_END_NAMESPACE_STD
0264 
0265 _LIBCPP_POP_MACROS
0266 
0267 #endif // _LIBCPP___THREAD_THREAD_H