106c3fb27SDimitry Andric // -*- C++ -*- 206c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 306c3fb27SDimitry Andric // 406c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 506c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 606c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 706c3fb27SDimitry Andric // 806c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 906c3fb27SDimitry Andric 1006c3fb27SDimitry Andric #ifndef _LIBCPP___THREAD_THREAD_H 1106c3fb27SDimitry Andric #define _LIBCPP___THREAD_THREAD_H 1206c3fb27SDimitry Andric 1306c3fb27SDimitry Andric #include <__condition_variable/condition_variable.h> 1406c3fb27SDimitry Andric #include <__config> 1506c3fb27SDimitry Andric #include <__exception/terminate.h> 1606c3fb27SDimitry Andric #include <__functional/hash.h> 1706c3fb27SDimitry Andric #include <__functional/unary_function.h> 1806c3fb27SDimitry Andric #include <__memory/unique_ptr.h> 1906c3fb27SDimitry Andric #include <__mutex/mutex.h> 2006c3fb27SDimitry Andric #include <__system_error/system_error.h> 2106c3fb27SDimitry Andric #include <__thread/id.h> 22*0fca6ea1SDimitry Andric #include <__thread/support.h> 2306c3fb27SDimitry Andric #include <__utility/forward.h> 2406c3fb27SDimitry Andric #include <tuple> 2506c3fb27SDimitry Andric 2606c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_LOCALIZATION 2706c3fb27SDimitry Andric # include <locale> 2806c3fb27SDimitry Andric # include <sstream> 2906c3fb27SDimitry Andric #endif 3006c3fb27SDimitry Andric 3106c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 3206c3fb27SDimitry Andric # pragma GCC system_header 3306c3fb27SDimitry Andric #endif 3406c3fb27SDimitry Andric 35b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS 36b3edf446SDimitry Andric #include <__undef_macros> 37b3edf446SDimitry Andric 3806c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 3906c3fb27SDimitry Andric 40cb14a3feSDimitry Andric template <class _Tp> 41cb14a3feSDimitry Andric class __thread_specific_ptr; 4206c3fb27SDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI __thread_struct; 4306c3fb27SDimitry Andric class _LIBCPP_HIDDEN __thread_struct_imp; 4406c3fb27SDimitry Andric class __assoc_sub_state; 4506c3fb27SDimitry Andric 4606c3fb27SDimitry Andric _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data(); 4706c3fb27SDimitry Andric 48cb14a3feSDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI __thread_struct { 4906c3fb27SDimitry Andric __thread_struct_imp* __p_; 5006c3fb27SDimitry Andric 5106c3fb27SDimitry Andric __thread_struct(const __thread_struct&); 5206c3fb27SDimitry Andric __thread_struct& operator=(const __thread_struct&); 53cb14a3feSDimitry Andric 5406c3fb27SDimitry Andric public: 5506c3fb27SDimitry Andric __thread_struct(); 5606c3fb27SDimitry Andric ~__thread_struct(); 5706c3fb27SDimitry Andric 5806c3fb27SDimitry Andric void notify_all_at_thread_exit(condition_variable*, mutex*); 5906c3fb27SDimitry Andric void __make_ready_at_thread_exit(__assoc_sub_state*); 6006c3fb27SDimitry Andric }; 6106c3fb27SDimitry Andric 6206c3fb27SDimitry Andric template <class _Tp> 63cb14a3feSDimitry Andric class __thread_specific_ptr { 6406c3fb27SDimitry Andric __libcpp_tls_key __key_; 6506c3fb27SDimitry Andric 6606c3fb27SDimitry Andric // Only __thread_local_data() may construct a __thread_specific_ptr 6706c3fb27SDimitry Andric // and only with _Tp == __thread_struct. 68*0fca6ea1SDimitry Andric static_assert(is_same<_Tp, __thread_struct>::value, ""); 6906c3fb27SDimitry Andric __thread_specific_ptr(); 7006c3fb27SDimitry Andric friend _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data(); 7106c3fb27SDimitry Andric 7206c3fb27SDimitry Andric _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); 7306c3fb27SDimitry Andric 7406c3fb27SDimitry Andric public: 7506c3fb27SDimitry Andric typedef _Tp* pointer; 7606c3fb27SDimitry Andric 77*0fca6ea1SDimitry Andric __thread_specific_ptr(const __thread_specific_ptr&) = delete; 78*0fca6ea1SDimitry Andric __thread_specific_ptr& operator=(const __thread_specific_ptr&) = delete; 7906c3fb27SDimitry Andric ~__thread_specific_ptr(); 8006c3fb27SDimitry Andric 81cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); } 82cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); } 83cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return get(); } 8406c3fb27SDimitry Andric void set_pointer(pointer __p); 8506c3fb27SDimitry Andric }; 8606c3fb27SDimitry Andric 8706c3fb27SDimitry Andric template <class _Tp> 88cb14a3feSDimitry Andric void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) { 8906c3fb27SDimitry Andric delete static_cast<pointer>(__p); 9006c3fb27SDimitry Andric } 9106c3fb27SDimitry Andric 9206c3fb27SDimitry Andric template <class _Tp> 93cb14a3feSDimitry Andric __thread_specific_ptr<_Tp>::__thread_specific_ptr() { 94cb14a3feSDimitry Andric int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit); 9506c3fb27SDimitry Andric if (__ec) 9606c3fb27SDimitry Andric __throw_system_error(__ec, "__thread_specific_ptr construction failed"); 9706c3fb27SDimitry Andric } 9806c3fb27SDimitry Andric 9906c3fb27SDimitry Andric template <class _Tp> 100cb14a3feSDimitry Andric __thread_specific_ptr<_Tp>::~__thread_specific_ptr() { 10106c3fb27SDimitry Andric // __thread_specific_ptr is only created with a static storage duration 10206c3fb27SDimitry Andric // so this destructor is only invoked during program termination. Invoking 10306c3fb27SDimitry Andric // pthread_key_delete(__key_) may prevent other threads from deleting their 10406c3fb27SDimitry Andric // thread local data. For this reason we leak the key. 10506c3fb27SDimitry Andric } 10606c3fb27SDimitry Andric 10706c3fb27SDimitry Andric template <class _Tp> 108cb14a3feSDimitry Andric void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) { 1091db9f3b2SDimitry Andric _LIBCPP_ASSERT_INTERNAL(get() == nullptr, "Attempting to overwrite thread local data"); 11006c3fb27SDimitry Andric std::__libcpp_tls_set(__key_, __p); 11106c3fb27SDimitry Andric } 11206c3fb27SDimitry Andric 11306c3fb27SDimitry Andric template <> 114cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> { 115cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT { 11606c3fb27SDimitry Andric return hash<__libcpp_thread_id>()(__v.__id_); 11706c3fb27SDimitry Andric } 11806c3fb27SDimitry Andric }; 11906c3fb27SDimitry Andric 12006c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_LOCALIZATION 12106c3fb27SDimitry Andric template <class _CharT, class _Traits> 1225f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 12306c3fb27SDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) { 12406c3fb27SDimitry Andric // [thread.thread.id]/9 12506c3fb27SDimitry Andric // Effects: Inserts the text representation for charT of id into out. 12606c3fb27SDimitry Andric // 12706c3fb27SDimitry Andric // [thread.thread.id]/2 12806c3fb27SDimitry Andric // The text representation for the character type charT of an 12906c3fb27SDimitry Andric // object of type thread::id is an unspecified sequence of charT 13006c3fb27SDimitry Andric // such that, for two objects of type thread::id x and y, if 13106c3fb27SDimitry Andric // x == y is true, the thread::id objects have the same text 13206c3fb27SDimitry Andric // representation, and if x != y is true, the thread::id objects 13306c3fb27SDimitry Andric // have distinct text representations. 13406c3fb27SDimitry Andric // 13506c3fb27SDimitry Andric // Since various flags in the output stream can affect how the 13606c3fb27SDimitry Andric // thread id is represented (e.g. numpunct or showbase), we 13706c3fb27SDimitry Andric // use a temporary stream instead and just output the thread 13806c3fb27SDimitry Andric // id representation as a string. 13906c3fb27SDimitry Andric 14006c3fb27SDimitry Andric basic_ostringstream<_CharT, _Traits> __sstr; 14106c3fb27SDimitry Andric __sstr.imbue(locale::classic()); 14206c3fb27SDimitry Andric __sstr << __id.__id_; 14306c3fb27SDimitry Andric return __os << __sstr.str(); 14406c3fb27SDimitry Andric } 14506c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_LOCALIZATION 14606c3fb27SDimitry Andric 147cb14a3feSDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI thread { 14806c3fb27SDimitry Andric __libcpp_thread_t __t_; 14906c3fb27SDimitry Andric 15006c3fb27SDimitry Andric thread(const thread&); 15106c3fb27SDimitry Andric thread& operator=(const thread&); 152cb14a3feSDimitry Andric 15306c3fb27SDimitry Andric public: 15406c3fb27SDimitry Andric typedef __thread_id id; 15506c3fb27SDimitry Andric typedef __libcpp_thread_t native_handle_type; 15606c3fb27SDimitry Andric 157cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} 15806c3fb27SDimitry Andric #ifndef _LIBCPP_CXX03_LANG 159*0fca6ea1SDimitry Andric template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> = 0> 160cb14a3feSDimitry Andric _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp&& __f, _Args&&... __args); 16106c3fb27SDimitry Andric #else // _LIBCPP_CXX03_LANG 16206c3fb27SDimitry Andric template <class _Fp> 163cb14a3feSDimitry Andric _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp __f); 16406c3fb27SDimitry Andric #endif 16506c3fb27SDimitry Andric ~thread(); 16606c3fb27SDimitry Andric 167cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; } 16806c3fb27SDimitry Andric 169cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT { 17006c3fb27SDimitry Andric if (!__libcpp_thread_isnull(&__t_)) 17106c3fb27SDimitry Andric terminate(); 17206c3fb27SDimitry Andric __t_ = __t.__t_; 17306c3fb27SDimitry Andric __t.__t_ = _LIBCPP_NULL_THREAD; 17406c3fb27SDimitry Andric return *this; 17506c3fb27SDimitry Andric } 17606c3fb27SDimitry Andric 177cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); } 17806c3fb27SDimitry Andric 179cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); } 18006c3fb27SDimitry Andric void join(); 18106c3fb27SDimitry Andric void detach(); 182cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); } 183cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; } 18406c3fb27SDimitry Andric 18506c3fb27SDimitry Andric static unsigned hardware_concurrency() _NOEXCEPT; 18606c3fb27SDimitry Andric }; 18706c3fb27SDimitry Andric 18806c3fb27SDimitry Andric #ifndef _LIBCPP_CXX03_LANG 18906c3fb27SDimitry Andric 19006c3fb27SDimitry Andric template <class _TSp, class _Fp, class... _Args, size_t... _Indices> 191cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) { 1925f757f3fSDimitry Andric std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...); 19306c3fb27SDimitry Andric } 19406c3fb27SDimitry Andric 19506c3fb27SDimitry Andric template <class _Fp> 196cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) { 19706c3fb27SDimitry Andric // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...> 19806c3fb27SDimitry Andric unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 1995f757f3fSDimitry Andric __thread_local_data().set_pointer(std::get<0>(*__p.get()).release()); 20006c3fb27SDimitry Andric typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; 2015f757f3fSDimitry Andric std::__thread_execute(*__p.get(), _Index()); 20206c3fb27SDimitry Andric return nullptr; 20306c3fb27SDimitry Andric } 20406c3fb27SDimitry Andric 205*0fca6ea1SDimitry Andric template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> > 206cb14a3feSDimitry Andric thread::thread(_Fp&& __f, _Args&&... __args) { 20706c3fb27SDimitry Andric typedef unique_ptr<__thread_struct> _TSPtr; 20806c3fb27SDimitry Andric _TSPtr __tsp(new __thread_struct); 20906c3fb27SDimitry Andric typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp; 210cb14a3feSDimitry Andric unique_ptr<_Gp> __p(new _Gp(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...)); 2115f757f3fSDimitry Andric int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); 21206c3fb27SDimitry Andric if (__ec == 0) 21306c3fb27SDimitry Andric __p.release(); 21406c3fb27SDimitry Andric else 21506c3fb27SDimitry Andric __throw_system_error(__ec, "thread constructor failed"); 21606c3fb27SDimitry Andric } 21706c3fb27SDimitry Andric 21806c3fb27SDimitry Andric #else // _LIBCPP_CXX03_LANG 21906c3fb27SDimitry Andric 22006c3fb27SDimitry Andric template <class _Fp> 22106c3fb27SDimitry Andric struct __thread_invoke_pair { 22206c3fb27SDimitry Andric // This type is used to pass memory for thread local storage and a functor 22306c3fb27SDimitry Andric // to a newly created thread because std::pair doesn't work with 22406c3fb27SDimitry Andric // std::unique_ptr in C++03. 22506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} 22606c3fb27SDimitry Andric unique_ptr<__thread_struct> __tsp_; 22706c3fb27SDimitry Andric _Fp __fn_; 22806c3fb27SDimitry Andric }; 22906c3fb27SDimitry Andric 23006c3fb27SDimitry Andric template <class _Fp> 231cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) { 23206c3fb27SDimitry Andric unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 23306c3fb27SDimitry Andric __thread_local_data().set_pointer(__p->__tsp_.release()); 23406c3fb27SDimitry Andric (__p->__fn_)(); 23506c3fb27SDimitry Andric return nullptr; 23606c3fb27SDimitry Andric } 23706c3fb27SDimitry Andric 23806c3fb27SDimitry Andric template <class _Fp> 239cb14a3feSDimitry Andric thread::thread(_Fp __f) { 24006c3fb27SDimitry Andric typedef __thread_invoke_pair<_Fp> _InvokePair; 24106c3fb27SDimitry Andric typedef unique_ptr<_InvokePair> _PairPtr; 24206c3fb27SDimitry Andric _PairPtr __pp(new _InvokePair(__f)); 2435f757f3fSDimitry Andric int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); 24406c3fb27SDimitry Andric if (__ec == 0) 24506c3fb27SDimitry Andric __pp.release(); 24606c3fb27SDimitry Andric else 24706c3fb27SDimitry Andric __throw_system_error(__ec, "thread constructor failed"); 24806c3fb27SDimitry Andric } 24906c3fb27SDimitry Andric 25006c3fb27SDimitry Andric #endif // _LIBCPP_CXX03_LANG 25106c3fb27SDimitry Andric 252cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); } 25306c3fb27SDimitry Andric 25406c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD 25506c3fb27SDimitry Andric 256b3edf446SDimitry Andric _LIBCPP_POP_MACROS 257b3edf446SDimitry Andric 25806c3fb27SDimitry Andric #endif // _LIBCPP___THREAD_THREAD_H 259