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