2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_THREAD
11 #define _LIBCPP_THREAD
24 typedef pthread_t native_handle_type;
27 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
30 thread(const thread&) = delete;
31 thread(thread&& t) noexcept;
33 thread& operator=(const thread&) = delete;
34 thread& operator=(thread&& t) noexcept;
36 void swap(thread& t) noexcept;
38 bool joinable() const noexcept;
41 id get_id() const noexcept;
42 native_handle_type native_handle();
44 static unsigned hardware_concurrency() noexcept;
47 void swap(thread& x, thread& y) noexcept;
55 bool operator==(thread::id x, thread::id y) noexcept;
56 bool operator!=(thread::id x, thread::id y) noexcept;
57 bool operator< (thread::id x, thread::id y) noexcept;
58 bool operator<=(thread::id x, thread::id y) noexcept;
59 bool operator> (thread::id x, thread::id y) noexcept;
60 bool operator>=(thread::id x, thread::id y) noexcept;
62 template<class charT, class traits>
63 basic_ostream<charT, traits>&
64 operator<<(basic_ostream<charT, traits>& out, thread::id id);
69 thread::id get_id() noexcept;
71 void yield() noexcept;
73 template <class Clock, class Duration>
74 void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
76 template <class Rep, class Period>
77 void sleep_for(const chrono::duration<Rep, Period>& rel_time);
87 #include <__functional_base>
88 #include <__mutex_base>
89 #include <__thread/poll_with_backoff.h>
90 #include <__thread/timed_backoff_policy.h>
91 #include <__threading_support>
92 #include <__utility/forward.h>
98 #include <system_error>
100 #include <type_traits>
103 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
104 #pragma GCC system_header
108 #include <__undef_macros>
110 #ifdef _LIBCPP_HAS_NO_THREADS
111 #error <thread> is not supported on this single threaded system
112 #else // !_LIBCPP_HAS_NO_THREADS
114 _LIBCPP_BEGIN_NAMESPACE_STD
116 template <class _Tp> class __thread_specific_ptr;
117 class _LIBCPP_TYPE_VIS __thread_struct;
118 class _LIBCPP_HIDDEN __thread_struct_imp;
119 class __assoc_sub_state;
121 _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
123 class _LIBCPP_TYPE_VIS __thread_struct
125 __thread_struct_imp* __p_;
127 __thread_struct(const __thread_struct&);
128 __thread_struct& operator=(const __thread_struct&);
133 void notify_all_at_thread_exit(condition_variable*, mutex*);
134 void __make_ready_at_thread_exit(__assoc_sub_state*);
138 class __thread_specific_ptr
140 __libcpp_tls_key __key_;
142 // Only __thread_local_data() may construct a __thread_specific_ptr
143 // and only with _Tp == __thread_struct.
144 static_assert((is_same<_Tp, __thread_struct>::value), "");
145 __thread_specific_ptr();
146 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
148 __thread_specific_ptr(const __thread_specific_ptr&);
149 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
151 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
154 typedef _Tp* pointer;
156 ~__thread_specific_ptr();
158 _LIBCPP_INLINE_VISIBILITY
159 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
160 _LIBCPP_INLINE_VISIBILITY
161 pointer operator*() const {return *get();}
162 _LIBCPP_INLINE_VISIBILITY
163 pointer operator->() const {return get();}
164 void set_pointer(pointer __p);
168 void _LIBCPP_TLS_DESTRUCTOR_CC
169 __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
171 delete static_cast<pointer>(__p);
175 __thread_specific_ptr<_Tp>::__thread_specific_ptr()
178 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
180 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
184 __thread_specific_ptr<_Tp>::~__thread_specific_ptr()
186 // __thread_specific_ptr is only created with a static storage duration
187 // so this destructor is only invoked during program termination. Invoking
188 // pthread_key_delete(__key_) may prevent other threads from deleting their
189 // thread local data. For this reason we leak the key.
194 __thread_specific_ptr<_Tp>::set_pointer(pointer __p)
196 _LIBCPP_ASSERT(get() == nullptr,
197 "Attempting to overwrite thread local data");
198 __libcpp_tls_set(__key_, __p);
202 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
203 : public unary_function<__thread_id, size_t>
205 _LIBCPP_INLINE_VISIBILITY
206 size_t operator()(__thread_id __v) const _NOEXCEPT
208 return hash<__libcpp_thread_id>()(__v.__id_);
212 template<class _CharT, class _Traits>
213 _LIBCPP_INLINE_VISIBILITY
214 basic_ostream<_CharT, _Traits>&
215 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
216 {return __os << __id.__id_;}
218 class _LIBCPP_TYPE_VIS thread
220 __libcpp_thread_t __t_;
222 thread(const thread&);
223 thread& operator=(const thread&);
225 typedef __thread_id id;
226 typedef __libcpp_thread_t native_handle_type;
228 _LIBCPP_INLINE_VISIBILITY
229 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
230 #ifndef _LIBCPP_CXX03_LANG
231 template <class _Fp, class ..._Args,
232 class = typename enable_if
234 !is_same<typename __uncvref<_Fp>::type, thread>::value
237 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
238 explicit thread(_Fp&& __f, _Args&&... __args);
239 #else // _LIBCPP_CXX03_LANG
241 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
242 explicit thread(_Fp __f);
246 _LIBCPP_INLINE_VISIBILITY
247 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {
248 __t.__t_ = _LIBCPP_NULL_THREAD;
251 _LIBCPP_INLINE_VISIBILITY
252 thread& operator=(thread&& __t) _NOEXCEPT {
253 if (!__libcpp_thread_isnull(&__t_))
256 __t.__t_ = _LIBCPP_NULL_THREAD;
260 _LIBCPP_INLINE_VISIBILITY
261 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
263 _LIBCPP_INLINE_VISIBILITY
264 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
267 _LIBCPP_INLINE_VISIBILITY
268 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
269 _LIBCPP_INLINE_VISIBILITY
270 native_handle_type native_handle() _NOEXCEPT {return __t_;}
272 static unsigned hardware_concurrency() _NOEXCEPT;
275 #ifndef _LIBCPP_CXX03_LANG
277 template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
278 inline _LIBCPP_INLINE_VISIBILITY
280 __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
282 _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
286 _LIBCPP_INLINE_VISIBILITY
287 void* __thread_proxy(void* __vp)
289 // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
290 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
291 __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release());
292 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
293 _VSTD::__thread_execute(*__p.get(), _Index());
297 template <class _Fp, class ..._Args,
300 thread::thread(_Fp&& __f, _Args&&... __args)
302 typedef unique_ptr<__thread_struct> _TSPtr;
303 _TSPtr __tsp(new __thread_struct);
304 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
306 new _Gp(_VSTD::move(__tsp),
307 _VSTD::forward<_Fp>(__f),
308 _VSTD::forward<_Args>(__args)...));
309 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
313 __throw_system_error(__ec, "thread constructor failed");
316 #else // _LIBCPP_CXX03_LANG
319 struct __thread_invoke_pair {
320 // This type is used to pass memory for thread local storage and a functor
321 // to a newly created thread because std::pair doesn't work with
322 // std::unique_ptr in C++03.
323 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
324 unique_ptr<__thread_struct> __tsp_;
329 void* __thread_proxy_cxx03(void* __vp)
331 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
332 __thread_local_data().set_pointer(__p->__tsp_.release());
338 thread::thread(_Fp __f)
341 typedef __thread_invoke_pair<_Fp> _InvokePair;
342 typedef unique_ptr<_InvokePair> _PairPtr;
343 _PairPtr __pp(new _InvokePair(__f));
344 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
348 __throw_system_error(__ec, "thread constructor failed");
351 #endif // _LIBCPP_CXX03_LANG
353 inline _LIBCPP_INLINE_VISIBILITY
354 void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
356 namespace this_thread
359 _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
361 template <class _Rep, class _Period>
363 sleep_for(const chrono::duration<_Rep, _Period>& __d)
365 if (__d > chrono::duration<_Rep, _Period>::zero())
367 // The standard guarantees a 64bit signed integer resolution for nanoseconds,
368 // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>
369 // and issues with long double folding on PowerPC with GCC.
370 _LIBCPP_CONSTEXPR chrono::duration<long double> _Max =
371 chrono::duration<long double>(9223372036.0L);
372 chrono::nanoseconds __ns;
375 __ns = chrono::duration_cast<chrono::nanoseconds>(__d);
380 __ns = chrono::nanoseconds::max();
381 this_thread::sleep_for(__ns);
385 template <class _Clock, class _Duration>
387 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
390 condition_variable __cv;
391 unique_lock<mutex> __lk(__mut);
392 while (_Clock::now() < __t)
393 __cv.wait_until(__lk, __t);
396 template <class _Duration>
397 inline _LIBCPP_INLINE_VISIBILITY
399 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
401 this_thread::sleep_for(__t - chrono::steady_clock::now());
404 inline _LIBCPP_INLINE_VISIBILITY
405 void yield() _NOEXCEPT {__libcpp_thread_yield();}
407 } // namespace this_thread
409 _LIBCPP_END_NAMESPACE_STD
411 #endif // !_LIBCPP_HAS_NO_THREADS
415 #endif // _LIBCPP_THREAD