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