xref: /freebsd-src/contrib/llvm-project/libcxx/include/__thread/thread.h (revision b3edf4467982447620505a28fc82e38a414c07dc)
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>
2206c3fb27SDimitry Andric #include <__threading_support>
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 
35*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
36*b3edf446SDimitry Andric #include <__undef_macros>
37*b3edf446SDimitry 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.
6806c3fb27SDimitry 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   __thread_specific_ptr(const __thread_specific_ptr&);
7306c3fb27SDimitry Andric   __thread_specific_ptr& operator=(const __thread_specific_ptr&);
7406c3fb27SDimitry Andric 
7506c3fb27SDimitry Andric   _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
7606c3fb27SDimitry Andric 
7706c3fb27SDimitry Andric public:
7806c3fb27SDimitry Andric   typedef _Tp* pointer;
7906c3fb27SDimitry Andric 
8006c3fb27SDimitry Andric   ~__thread_specific_ptr();
8106c3fb27SDimitry Andric 
82cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); }
83cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); }
84cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return get(); }
8506c3fb27SDimitry Andric   void set_pointer(pointer __p);
8606c3fb27SDimitry Andric };
8706c3fb27SDimitry Andric 
8806c3fb27SDimitry Andric template <class _Tp>
89cb14a3feSDimitry Andric void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) {
9006c3fb27SDimitry Andric   delete static_cast<pointer>(__p);
9106c3fb27SDimitry Andric }
9206c3fb27SDimitry Andric 
9306c3fb27SDimitry Andric template <class _Tp>
94cb14a3feSDimitry Andric __thread_specific_ptr<_Tp>::__thread_specific_ptr() {
95cb14a3feSDimitry Andric   int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
9606c3fb27SDimitry Andric   if (__ec)
9706c3fb27SDimitry Andric     __throw_system_error(__ec, "__thread_specific_ptr construction failed");
9806c3fb27SDimitry Andric }
9906c3fb27SDimitry Andric 
10006c3fb27SDimitry Andric template <class _Tp>
101cb14a3feSDimitry Andric __thread_specific_ptr<_Tp>::~__thread_specific_ptr() {
10206c3fb27SDimitry Andric   // __thread_specific_ptr is only created with a static storage duration
10306c3fb27SDimitry Andric   // so this destructor is only invoked during program termination. Invoking
10406c3fb27SDimitry Andric   // pthread_key_delete(__key_) may prevent other threads from deleting their
10506c3fb27SDimitry Andric   // thread local data. For this reason we leak the key.
10606c3fb27SDimitry Andric }
10706c3fb27SDimitry Andric 
10806c3fb27SDimitry Andric template <class _Tp>
109cb14a3feSDimitry Andric void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) {
1101db9f3b2SDimitry Andric   _LIBCPP_ASSERT_INTERNAL(get() == nullptr, "Attempting to overwrite thread local data");
11106c3fb27SDimitry Andric   std::__libcpp_tls_set(__key_, __p);
11206c3fb27SDimitry Andric }
11306c3fb27SDimitry Andric 
11406c3fb27SDimitry Andric template <>
115cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> {
116cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT {
11706c3fb27SDimitry Andric     return hash<__libcpp_thread_id>()(__v.__id_);
11806c3fb27SDimitry Andric   }
11906c3fb27SDimitry Andric };
12006c3fb27SDimitry Andric 
12106c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_LOCALIZATION
12206c3fb27SDimitry Andric template <class _CharT, class _Traits>
1235f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
12406c3fb27SDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) {
12506c3fb27SDimitry Andric   // [thread.thread.id]/9
12606c3fb27SDimitry Andric   //   Effects: Inserts the text representation for charT of id into out.
12706c3fb27SDimitry Andric   //
12806c3fb27SDimitry Andric   // [thread.thread.id]/2
12906c3fb27SDimitry Andric   //   The text representation for the character type charT of an
13006c3fb27SDimitry Andric   //   object of type thread::id is an unspecified sequence of charT
13106c3fb27SDimitry Andric   //   such that, for two objects of type thread::id x and y, if
13206c3fb27SDimitry Andric   //   x == y is true, the thread::id objects have the same text
13306c3fb27SDimitry Andric   //   representation, and if x != y is true, the thread::id objects
13406c3fb27SDimitry Andric   //   have distinct text representations.
13506c3fb27SDimitry Andric   //
13606c3fb27SDimitry Andric   // Since various flags in the output stream can affect how the
13706c3fb27SDimitry Andric   // thread id is represented (e.g. numpunct or showbase), we
13806c3fb27SDimitry Andric   // use a temporary stream instead and just output the thread
13906c3fb27SDimitry Andric   // id representation as a string.
14006c3fb27SDimitry Andric 
14106c3fb27SDimitry Andric   basic_ostringstream<_CharT, _Traits> __sstr;
14206c3fb27SDimitry Andric   __sstr.imbue(locale::classic());
14306c3fb27SDimitry Andric   __sstr << __id.__id_;
14406c3fb27SDimitry Andric   return __os << __sstr.str();
14506c3fb27SDimitry Andric }
14606c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_LOCALIZATION
14706c3fb27SDimitry Andric 
148cb14a3feSDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI thread {
14906c3fb27SDimitry Andric   __libcpp_thread_t __t_;
15006c3fb27SDimitry Andric 
15106c3fb27SDimitry Andric   thread(const thread&);
15206c3fb27SDimitry Andric   thread& operator=(const thread&);
153cb14a3feSDimitry Andric 
15406c3fb27SDimitry Andric public:
15506c3fb27SDimitry Andric   typedef __thread_id id;
15606c3fb27SDimitry Andric   typedef __libcpp_thread_t native_handle_type;
15706c3fb27SDimitry Andric 
158cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
15906c3fb27SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
160cb14a3feSDimitry Andric   template <class _Fp, class... _Args, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value> >
161cb14a3feSDimitry Andric   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp&& __f, _Args&&... __args);
16206c3fb27SDimitry Andric #else // _LIBCPP_CXX03_LANG
16306c3fb27SDimitry Andric   template <class _Fp>
164cb14a3feSDimitry Andric   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp __f);
16506c3fb27SDimitry Andric #endif
16606c3fb27SDimitry Andric   ~thread();
16706c3fb27SDimitry Andric 
168cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; }
16906c3fb27SDimitry Andric 
170cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT {
17106c3fb27SDimitry Andric     if (!__libcpp_thread_isnull(&__t_))
17206c3fb27SDimitry Andric       terminate();
17306c3fb27SDimitry Andric     __t_     = __t.__t_;
17406c3fb27SDimitry Andric     __t.__t_ = _LIBCPP_NULL_THREAD;
17506c3fb27SDimitry Andric     return *this;
17606c3fb27SDimitry Andric   }
17706c3fb27SDimitry Andric 
178cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); }
17906c3fb27SDimitry Andric 
180cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); }
18106c3fb27SDimitry Andric   void join();
18206c3fb27SDimitry Andric   void detach();
183cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); }
184cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; }
18506c3fb27SDimitry Andric 
18606c3fb27SDimitry Andric   static unsigned hardware_concurrency() _NOEXCEPT;
18706c3fb27SDimitry Andric };
18806c3fb27SDimitry Andric 
18906c3fb27SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
19006c3fb27SDimitry Andric 
19106c3fb27SDimitry Andric template <class _TSp, class _Fp, class... _Args, size_t... _Indices>
192cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) {
1935f757f3fSDimitry Andric   std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...);
19406c3fb27SDimitry Andric }
19506c3fb27SDimitry Andric 
19606c3fb27SDimitry Andric template <class _Fp>
197cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) {
19806c3fb27SDimitry Andric   // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
19906c3fb27SDimitry Andric   unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
2005f757f3fSDimitry Andric   __thread_local_data().set_pointer(std::get<0>(*__p.get()).release());
20106c3fb27SDimitry Andric   typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
2025f757f3fSDimitry Andric   std::__thread_execute(*__p.get(), _Index());
20306c3fb27SDimitry Andric   return nullptr;
20406c3fb27SDimitry Andric }
20506c3fb27SDimitry Andric 
206cb14a3feSDimitry Andric template <class _Fp, class... _Args, class >
207cb14a3feSDimitry Andric thread::thread(_Fp&& __f, _Args&&... __args) {
20806c3fb27SDimitry Andric   typedef unique_ptr<__thread_struct> _TSPtr;
20906c3fb27SDimitry Andric   _TSPtr __tsp(new __thread_struct);
21006c3fb27SDimitry Andric   typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp;
211cb14a3feSDimitry Andric   unique_ptr<_Gp> __p(new _Gp(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
2125f757f3fSDimitry Andric   int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
21306c3fb27SDimitry Andric   if (__ec == 0)
21406c3fb27SDimitry Andric     __p.release();
21506c3fb27SDimitry Andric   else
21606c3fb27SDimitry Andric     __throw_system_error(__ec, "thread constructor failed");
21706c3fb27SDimitry Andric }
21806c3fb27SDimitry Andric 
21906c3fb27SDimitry Andric #else // _LIBCPP_CXX03_LANG
22006c3fb27SDimitry Andric 
22106c3fb27SDimitry Andric template <class _Fp>
22206c3fb27SDimitry Andric struct __thread_invoke_pair {
22306c3fb27SDimitry Andric   // This type is used to pass memory for thread local storage and a functor
22406c3fb27SDimitry Andric   // to a newly created thread because std::pair doesn't work with
22506c3fb27SDimitry Andric   // std::unique_ptr in C++03.
22606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
22706c3fb27SDimitry Andric   unique_ptr<__thread_struct> __tsp_;
22806c3fb27SDimitry Andric   _Fp __fn_;
22906c3fb27SDimitry Andric };
23006c3fb27SDimitry Andric 
23106c3fb27SDimitry Andric template <class _Fp>
232cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) {
23306c3fb27SDimitry Andric   unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
23406c3fb27SDimitry Andric   __thread_local_data().set_pointer(__p->__tsp_.release());
23506c3fb27SDimitry Andric   (__p->__fn_)();
23606c3fb27SDimitry Andric   return nullptr;
23706c3fb27SDimitry Andric }
23806c3fb27SDimitry Andric 
23906c3fb27SDimitry Andric template <class _Fp>
240cb14a3feSDimitry Andric thread::thread(_Fp __f) {
24106c3fb27SDimitry Andric   typedef __thread_invoke_pair<_Fp> _InvokePair;
24206c3fb27SDimitry Andric   typedef unique_ptr<_InvokePair> _PairPtr;
24306c3fb27SDimitry Andric   _PairPtr __pp(new _InvokePair(__f));
2445f757f3fSDimitry Andric   int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
24506c3fb27SDimitry Andric   if (__ec == 0)
24606c3fb27SDimitry Andric     __pp.release();
24706c3fb27SDimitry Andric   else
24806c3fb27SDimitry Andric     __throw_system_error(__ec, "thread constructor failed");
24906c3fb27SDimitry Andric }
25006c3fb27SDimitry Andric 
25106c3fb27SDimitry Andric #endif // _LIBCPP_CXX03_LANG
25206c3fb27SDimitry Andric 
253cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); }
25406c3fb27SDimitry Andric 
25506c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
25606c3fb27SDimitry Andric 
257*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
258*b3edf446SDimitry Andric 
25906c3fb27SDimitry Andric #endif // _LIBCPP___THREAD_THREAD_H
260