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