1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___THREAD_THREAD_H 11 #define _LIBCPP___THREAD_THREAD_H 12 13 #include <__condition_variable/condition_variable.h> 14 #include <__config> 15 #include <__exception/terminate.h> 16 #include <__functional/hash.h> 17 #include <__functional/unary_function.h> 18 #include <__memory/unique_ptr.h> 19 #include <__mutex/mutex.h> 20 #include <__system_error/system_error.h> 21 #include <__thread/id.h> 22 #include <__threading_support> 23 #include <__utility/forward.h> 24 #include <tuple> 25 26 #ifndef _LIBCPP_HAS_NO_LOCALIZATION 27 # include <locale> 28 # include <sstream> 29 #endif 30 31 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 32 # pragma GCC system_header 33 #endif 34 35 _LIBCPP_BEGIN_NAMESPACE_STD 36 37 template <class _Tp> 38 class __thread_specific_ptr; 39 class _LIBCPP_EXPORTED_FROM_ABI __thread_struct; 40 class _LIBCPP_HIDDEN __thread_struct_imp; 41 class __assoc_sub_state; 42 43 _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data(); 44 45 class _LIBCPP_EXPORTED_FROM_ABI __thread_struct { 46 __thread_struct_imp* __p_; 47 48 __thread_struct(const __thread_struct&); 49 __thread_struct& operator=(const __thread_struct&); 50 51 public: 52 __thread_struct(); 53 ~__thread_struct(); 54 55 void notify_all_at_thread_exit(condition_variable*, mutex*); 56 void __make_ready_at_thread_exit(__assoc_sub_state*); 57 }; 58 59 template <class _Tp> 60 class __thread_specific_ptr { 61 __libcpp_tls_key __key_; 62 63 // Only __thread_local_data() may construct a __thread_specific_ptr 64 // and only with _Tp == __thread_struct. 65 static_assert((is_same<_Tp, __thread_struct>::value), ""); 66 __thread_specific_ptr(); 67 friend _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data(); 68 69 __thread_specific_ptr(const __thread_specific_ptr&); 70 __thread_specific_ptr& operator=(const __thread_specific_ptr&); 71 72 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); 73 74 public: 75 typedef _Tp* pointer; 76 77 ~__thread_specific_ptr(); 78 79 _LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); } 80 _LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); } 81 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return get(); } 82 void set_pointer(pointer __p); 83 }; 84 85 template <class _Tp> 86 void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) { 87 delete static_cast<pointer>(__p); 88 } 89 90 template <class _Tp> 91 __thread_specific_ptr<_Tp>::__thread_specific_ptr() { 92 int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit); 93 if (__ec) 94 __throw_system_error(__ec, "__thread_specific_ptr construction failed"); 95 } 96 97 template <class _Tp> 98 __thread_specific_ptr<_Tp>::~__thread_specific_ptr() { 99 // __thread_specific_ptr is only created with a static storage duration 100 // so this destructor is only invoked during program termination. Invoking 101 // pthread_key_delete(__key_) may prevent other threads from deleting their 102 // thread local data. For this reason we leak the key. 103 } 104 105 template <class _Tp> 106 void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) { 107 _LIBCPP_ASSERT_INTERNAL(get() == nullptr, "Attempting to overwrite thread local data"); 108 std::__libcpp_tls_set(__key_, __p); 109 } 110 111 template <> 112 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> { 113 _LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT { 114 return hash<__libcpp_thread_id>()(__v.__id_); 115 } 116 }; 117 118 #ifndef _LIBCPP_HAS_NO_LOCALIZATION 119 template <class _CharT, class _Traits> 120 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 121 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) { 122 // [thread.thread.id]/9 123 // Effects: Inserts the text representation for charT of id into out. 124 // 125 // [thread.thread.id]/2 126 // The text representation for the character type charT of an 127 // object of type thread::id is an unspecified sequence of charT 128 // such that, for two objects of type thread::id x and y, if 129 // x == y is true, the thread::id objects have the same text 130 // representation, and if x != y is true, the thread::id objects 131 // have distinct text representations. 132 // 133 // Since various flags in the output stream can affect how the 134 // thread id is represented (e.g. numpunct or showbase), we 135 // use a temporary stream instead and just output the thread 136 // id representation as a string. 137 138 basic_ostringstream<_CharT, _Traits> __sstr; 139 __sstr.imbue(locale::classic()); 140 __sstr << __id.__id_; 141 return __os << __sstr.str(); 142 } 143 #endif // _LIBCPP_HAS_NO_LOCALIZATION 144 145 class _LIBCPP_EXPORTED_FROM_ABI thread { 146 __libcpp_thread_t __t_; 147 148 thread(const thread&); 149 thread& operator=(const thread&); 150 151 public: 152 typedef __thread_id id; 153 typedef __libcpp_thread_t native_handle_type; 154 155 _LIBCPP_HIDE_FROM_ABI thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} 156 #ifndef _LIBCPP_CXX03_LANG 157 template <class _Fp, class... _Args, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value> > 158 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp&& __f, _Args&&... __args); 159 #else // _LIBCPP_CXX03_LANG 160 template <class _Fp> 161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp __f); 162 #endif 163 ~thread(); 164 165 _LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; } 166 167 _LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT { 168 if (!__libcpp_thread_isnull(&__t_)) 169 terminate(); 170 __t_ = __t.__t_; 171 __t.__t_ = _LIBCPP_NULL_THREAD; 172 return *this; 173 } 174 175 _LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); } 176 177 _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); } 178 void join(); 179 void detach(); 180 _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); } 181 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; } 182 183 static unsigned hardware_concurrency() _NOEXCEPT; 184 }; 185 186 #ifndef _LIBCPP_CXX03_LANG 187 188 template <class _TSp, class _Fp, class... _Args, size_t... _Indices> 189 inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) { 190 std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...); 191 } 192 193 template <class _Fp> 194 _LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) { 195 // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...> 196 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 197 __thread_local_data().set_pointer(std::get<0>(*__p.get()).release()); 198 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; 199 std::__thread_execute(*__p.get(), _Index()); 200 return nullptr; 201 } 202 203 template <class _Fp, class... _Args, class > 204 thread::thread(_Fp&& __f, _Args&&... __args) { 205 typedef unique_ptr<__thread_struct> _TSPtr; 206 _TSPtr __tsp(new __thread_struct); 207 typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp; 208 unique_ptr<_Gp> __p(new _Gp(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...)); 209 int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); 210 if (__ec == 0) 211 __p.release(); 212 else 213 __throw_system_error(__ec, "thread constructor failed"); 214 } 215 216 #else // _LIBCPP_CXX03_LANG 217 218 template <class _Fp> 219 struct __thread_invoke_pair { 220 // This type is used to pass memory for thread local storage and a functor 221 // to a newly created thread because std::pair doesn't work with 222 // std::unique_ptr in C++03. 223 _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} 224 unique_ptr<__thread_struct> __tsp_; 225 _Fp __fn_; 226 }; 227 228 template <class _Fp> 229 _LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) { 230 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 231 __thread_local_data().set_pointer(__p->__tsp_.release()); 232 (__p->__fn_)(); 233 return nullptr; 234 } 235 236 template <class _Fp> 237 thread::thread(_Fp __f) { 238 typedef __thread_invoke_pair<_Fp> _InvokePair; 239 typedef unique_ptr<_InvokePair> _PairPtr; 240 _PairPtr __pp(new _InvokePair(__f)); 241 int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); 242 if (__ec == 0) 243 __pp.release(); 244 else 245 __throw_system_error(__ec, "thread constructor failed"); 246 } 247 248 #endif // _LIBCPP_CXX03_LANG 249 250 inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); } 251 252 _LIBCPP_END_NAMESPACE_STD 253 254 #endif // _LIBCPP___THREAD_THREAD_H 255