xref: /freebsd-src/contrib/llvm-project/libcxx/include/__thread/thread.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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 
3506c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3606c3fb27SDimitry Andric 
37cb14a3feSDimitry Andric template <class _Tp>
38cb14a3feSDimitry Andric class __thread_specific_ptr;
3906c3fb27SDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI __thread_struct;
4006c3fb27SDimitry Andric class _LIBCPP_HIDDEN __thread_struct_imp;
4106c3fb27SDimitry Andric class __assoc_sub_state;
4206c3fb27SDimitry Andric 
4306c3fb27SDimitry Andric _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();
4406c3fb27SDimitry Andric 
45cb14a3feSDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI __thread_struct {
4606c3fb27SDimitry Andric   __thread_struct_imp* __p_;
4706c3fb27SDimitry Andric 
4806c3fb27SDimitry Andric   __thread_struct(const __thread_struct&);
4906c3fb27SDimitry Andric   __thread_struct& operator=(const __thread_struct&);
50cb14a3feSDimitry Andric 
5106c3fb27SDimitry Andric public:
5206c3fb27SDimitry Andric   __thread_struct();
5306c3fb27SDimitry Andric   ~__thread_struct();
5406c3fb27SDimitry Andric 
5506c3fb27SDimitry Andric   void notify_all_at_thread_exit(condition_variable*, mutex*);
5606c3fb27SDimitry Andric   void __make_ready_at_thread_exit(__assoc_sub_state*);
5706c3fb27SDimitry Andric };
5806c3fb27SDimitry Andric 
5906c3fb27SDimitry Andric template <class _Tp>
60cb14a3feSDimitry Andric class __thread_specific_ptr {
6106c3fb27SDimitry Andric   __libcpp_tls_key __key_;
6206c3fb27SDimitry Andric 
6306c3fb27SDimitry Andric   // Only __thread_local_data() may construct a __thread_specific_ptr
6406c3fb27SDimitry Andric   // and only with _Tp == __thread_struct.
6506c3fb27SDimitry Andric   static_assert((is_same<_Tp, __thread_struct>::value), "");
6606c3fb27SDimitry Andric   __thread_specific_ptr();
6706c3fb27SDimitry Andric   friend _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();
6806c3fb27SDimitry Andric 
6906c3fb27SDimitry Andric   __thread_specific_ptr(const __thread_specific_ptr&);
7006c3fb27SDimitry Andric   __thread_specific_ptr& operator=(const __thread_specific_ptr&);
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 
7706c3fb27SDimitry Andric   ~__thread_specific_ptr();
7806c3fb27SDimitry Andric 
79cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); }
80cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); }
81cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return get(); }
8206c3fb27SDimitry Andric   void set_pointer(pointer __p);
8306c3fb27SDimitry Andric };
8406c3fb27SDimitry Andric 
8506c3fb27SDimitry Andric template <class _Tp>
86cb14a3feSDimitry Andric void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) {
8706c3fb27SDimitry Andric   delete static_cast<pointer>(__p);
8806c3fb27SDimitry Andric }
8906c3fb27SDimitry Andric 
9006c3fb27SDimitry Andric template <class _Tp>
91cb14a3feSDimitry Andric __thread_specific_ptr<_Tp>::__thread_specific_ptr() {
92cb14a3feSDimitry Andric   int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
9306c3fb27SDimitry Andric   if (__ec)
9406c3fb27SDimitry Andric     __throw_system_error(__ec, "__thread_specific_ptr construction failed");
9506c3fb27SDimitry Andric }
9606c3fb27SDimitry Andric 
9706c3fb27SDimitry Andric template <class _Tp>
98cb14a3feSDimitry Andric __thread_specific_ptr<_Tp>::~__thread_specific_ptr() {
9906c3fb27SDimitry Andric   // __thread_specific_ptr is only created with a static storage duration
10006c3fb27SDimitry Andric   // so this destructor is only invoked during program termination. Invoking
10106c3fb27SDimitry Andric   // pthread_key_delete(__key_) may prevent other threads from deleting their
10206c3fb27SDimitry Andric   // thread local data. For this reason we leak the key.
10306c3fb27SDimitry Andric }
10406c3fb27SDimitry Andric 
10506c3fb27SDimitry Andric template <class _Tp>
106cb14a3feSDimitry Andric void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) {
107*1db9f3b2SDimitry Andric   _LIBCPP_ASSERT_INTERNAL(get() == nullptr, "Attempting to overwrite thread local data");
10806c3fb27SDimitry Andric   std::__libcpp_tls_set(__key_, __p);
10906c3fb27SDimitry Andric }
11006c3fb27SDimitry Andric 
11106c3fb27SDimitry Andric template <>
112cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> {
113cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT {
11406c3fb27SDimitry Andric     return hash<__libcpp_thread_id>()(__v.__id_);
11506c3fb27SDimitry Andric   }
11606c3fb27SDimitry Andric };
11706c3fb27SDimitry Andric 
11806c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_LOCALIZATION
11906c3fb27SDimitry Andric template <class _CharT, class _Traits>
1205f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
12106c3fb27SDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) {
12206c3fb27SDimitry Andric   // [thread.thread.id]/9
12306c3fb27SDimitry Andric   //   Effects: Inserts the text representation for charT of id into out.
12406c3fb27SDimitry Andric   //
12506c3fb27SDimitry Andric   // [thread.thread.id]/2
12606c3fb27SDimitry Andric   //   The text representation for the character type charT of an
12706c3fb27SDimitry Andric   //   object of type thread::id is an unspecified sequence of charT
12806c3fb27SDimitry Andric   //   such that, for two objects of type thread::id x and y, if
12906c3fb27SDimitry Andric   //   x == y is true, the thread::id objects have the same text
13006c3fb27SDimitry Andric   //   representation, and if x != y is true, the thread::id objects
13106c3fb27SDimitry Andric   //   have distinct text representations.
13206c3fb27SDimitry Andric   //
13306c3fb27SDimitry Andric   // Since various flags in the output stream can affect how the
13406c3fb27SDimitry Andric   // thread id is represented (e.g. numpunct or showbase), we
13506c3fb27SDimitry Andric   // use a temporary stream instead and just output the thread
13606c3fb27SDimitry Andric   // id representation as a string.
13706c3fb27SDimitry Andric 
13806c3fb27SDimitry Andric   basic_ostringstream<_CharT, _Traits> __sstr;
13906c3fb27SDimitry Andric   __sstr.imbue(locale::classic());
14006c3fb27SDimitry Andric   __sstr << __id.__id_;
14106c3fb27SDimitry Andric   return __os << __sstr.str();
14206c3fb27SDimitry Andric }
14306c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_LOCALIZATION
14406c3fb27SDimitry Andric 
145cb14a3feSDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI thread {
14606c3fb27SDimitry Andric   __libcpp_thread_t __t_;
14706c3fb27SDimitry Andric 
14806c3fb27SDimitry Andric   thread(const thread&);
14906c3fb27SDimitry Andric   thread& operator=(const thread&);
150cb14a3feSDimitry Andric 
15106c3fb27SDimitry Andric public:
15206c3fb27SDimitry Andric   typedef __thread_id id;
15306c3fb27SDimitry Andric   typedef __libcpp_thread_t native_handle_type;
15406c3fb27SDimitry Andric 
155cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
15606c3fb27SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
157cb14a3feSDimitry Andric   template <class _Fp, class... _Args, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value> >
158cb14a3feSDimitry Andric   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp&& __f, _Args&&... __args);
15906c3fb27SDimitry Andric #else // _LIBCPP_CXX03_LANG
16006c3fb27SDimitry Andric   template <class _Fp>
161cb14a3feSDimitry Andric   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp __f);
16206c3fb27SDimitry Andric #endif
16306c3fb27SDimitry Andric   ~thread();
16406c3fb27SDimitry Andric 
165cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; }
16606c3fb27SDimitry Andric 
167cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT {
16806c3fb27SDimitry Andric     if (!__libcpp_thread_isnull(&__t_))
16906c3fb27SDimitry Andric       terminate();
17006c3fb27SDimitry Andric     __t_     = __t.__t_;
17106c3fb27SDimitry Andric     __t.__t_ = _LIBCPP_NULL_THREAD;
17206c3fb27SDimitry Andric     return *this;
17306c3fb27SDimitry Andric   }
17406c3fb27SDimitry Andric 
175cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); }
17606c3fb27SDimitry Andric 
177cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); }
17806c3fb27SDimitry Andric   void join();
17906c3fb27SDimitry Andric   void detach();
180cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); }
181cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; }
18206c3fb27SDimitry Andric 
18306c3fb27SDimitry Andric   static unsigned hardware_concurrency() _NOEXCEPT;
18406c3fb27SDimitry Andric };
18506c3fb27SDimitry Andric 
18606c3fb27SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
18706c3fb27SDimitry Andric 
18806c3fb27SDimitry Andric template <class _TSp, class _Fp, class... _Args, size_t... _Indices>
189cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) {
1905f757f3fSDimitry Andric   std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...);
19106c3fb27SDimitry Andric }
19206c3fb27SDimitry Andric 
19306c3fb27SDimitry Andric template <class _Fp>
194cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) {
19506c3fb27SDimitry Andric   // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
19606c3fb27SDimitry Andric   unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
1975f757f3fSDimitry Andric   __thread_local_data().set_pointer(std::get<0>(*__p.get()).release());
19806c3fb27SDimitry Andric   typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
1995f757f3fSDimitry Andric   std::__thread_execute(*__p.get(), _Index());
20006c3fb27SDimitry Andric   return nullptr;
20106c3fb27SDimitry Andric }
20206c3fb27SDimitry Andric 
203cb14a3feSDimitry Andric template <class _Fp, class... _Args, class >
204cb14a3feSDimitry Andric thread::thread(_Fp&& __f, _Args&&... __args) {
20506c3fb27SDimitry Andric   typedef unique_ptr<__thread_struct> _TSPtr;
20606c3fb27SDimitry Andric   _TSPtr __tsp(new __thread_struct);
20706c3fb27SDimitry Andric   typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp;
208cb14a3feSDimitry Andric   unique_ptr<_Gp> __p(new _Gp(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
2095f757f3fSDimitry Andric   int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
21006c3fb27SDimitry Andric   if (__ec == 0)
21106c3fb27SDimitry Andric     __p.release();
21206c3fb27SDimitry Andric   else
21306c3fb27SDimitry Andric     __throw_system_error(__ec, "thread constructor failed");
21406c3fb27SDimitry Andric }
21506c3fb27SDimitry Andric 
21606c3fb27SDimitry Andric #else // _LIBCPP_CXX03_LANG
21706c3fb27SDimitry Andric 
21806c3fb27SDimitry Andric template <class _Fp>
21906c3fb27SDimitry Andric struct __thread_invoke_pair {
22006c3fb27SDimitry Andric   // This type is used to pass memory for thread local storage and a functor
22106c3fb27SDimitry Andric   // to a newly created thread because std::pair doesn't work with
22206c3fb27SDimitry Andric   // std::unique_ptr in C++03.
22306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
22406c3fb27SDimitry Andric   unique_ptr<__thread_struct> __tsp_;
22506c3fb27SDimitry Andric   _Fp __fn_;
22606c3fb27SDimitry Andric };
22706c3fb27SDimitry Andric 
22806c3fb27SDimitry Andric template <class _Fp>
229cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) {
23006c3fb27SDimitry Andric   unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
23106c3fb27SDimitry Andric   __thread_local_data().set_pointer(__p->__tsp_.release());
23206c3fb27SDimitry Andric   (__p->__fn_)();
23306c3fb27SDimitry Andric   return nullptr;
23406c3fb27SDimitry Andric }
23506c3fb27SDimitry Andric 
23606c3fb27SDimitry Andric template <class _Fp>
237cb14a3feSDimitry Andric thread::thread(_Fp __f) {
23806c3fb27SDimitry Andric   typedef __thread_invoke_pair<_Fp> _InvokePair;
23906c3fb27SDimitry Andric   typedef unique_ptr<_InvokePair> _PairPtr;
24006c3fb27SDimitry Andric   _PairPtr __pp(new _InvokePair(__f));
2415f757f3fSDimitry Andric   int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
24206c3fb27SDimitry Andric   if (__ec == 0)
24306c3fb27SDimitry Andric     __pp.release();
24406c3fb27SDimitry Andric   else
24506c3fb27SDimitry Andric     __throw_system_error(__ec, "thread constructor failed");
24606c3fb27SDimitry Andric }
24706c3fb27SDimitry Andric 
24806c3fb27SDimitry Andric #endif // _LIBCPP_CXX03_LANG
24906c3fb27SDimitry Andric 
250cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); }
25106c3fb27SDimitry Andric 
25206c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
25306c3fb27SDimitry Andric 
25406c3fb27SDimitry Andric #endif // _LIBCPP___THREAD_THREAD_H
255