File indexing completed on 2026-05-03 08:13:43
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___THREAD_THREAD_H
0011 #define _LIBCPP___CXX03___THREAD_THREAD_H
0012
0013 #include <__cxx03/__condition_variable/condition_variable.h>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__exception/terminate.h>
0016 #include <__cxx03/__functional/hash.h>
0017 #include <__cxx03/__functional/unary_function.h>
0018 #include <__cxx03/__memory/unique_ptr.h>
0019 #include <__cxx03/__mutex/mutex.h>
0020 #include <__cxx03/__system_error/system_error.h>
0021 #include <__cxx03/__thread/id.h>
0022 #include <__cxx03/__thread/support.h>
0023 #include <__cxx03/__utility/forward.h>
0024 #include <__cxx03/tuple>
0025
0026 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
0027 # include <__cxx03/locale>
0028 # include <__cxx03/sstream>
0029 #endif
0030
0031 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0032 # pragma GCC system_header
0033 #endif
0034
0035 _LIBCPP_PUSH_MACROS
0036 #include <__cxx03/__undef_macros>
0037
0038 _LIBCPP_BEGIN_NAMESPACE_STD
0039
0040 template <class _Tp>
0041 class __thread_specific_ptr;
0042 class _LIBCPP_EXPORTED_FROM_ABI __thread_struct;
0043 class _LIBCPP_HIDDEN __thread_struct_imp;
0044 class __assoc_sub_state;
0045
0046 _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();
0047
0048 class _LIBCPP_EXPORTED_FROM_ABI __thread_struct {
0049 __thread_struct_imp* __p_;
0050
0051 __thread_struct(const __thread_struct&);
0052 __thread_struct& operator=(const __thread_struct&);
0053
0054 public:
0055 __thread_struct();
0056 ~__thread_struct();
0057
0058 void notify_all_at_thread_exit(condition_variable*, mutex*);
0059 void __make_ready_at_thread_exit(__assoc_sub_state*);
0060 };
0061
0062 template <class _Tp>
0063 class __thread_specific_ptr {
0064 __libcpp_tls_key __key_;
0065
0066
0067
0068 static_assert(is_same<_Tp, __thread_struct>::value, "");
0069 __thread_specific_ptr();
0070 friend _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();
0071
0072 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
0073
0074 public:
0075 typedef _Tp* pointer;
0076
0077 __thread_specific_ptr(const __thread_specific_ptr&) = delete;
0078 __thread_specific_ptr& operator=(const __thread_specific_ptr&) = delete;
0079 ~__thread_specific_ptr();
0080
0081 _LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); }
0082 _LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); }
0083 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return get(); }
0084 void set_pointer(pointer __p);
0085 };
0086
0087 template <class _Tp>
0088 void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) {
0089 delete static_cast<pointer>(__p);
0090 }
0091
0092 template <class _Tp>
0093 __thread_specific_ptr<_Tp>::__thread_specific_ptr() {
0094 int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
0095 if (__ec)
0096 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
0097 }
0098
0099 template <class _Tp>
0100 __thread_specific_ptr<_Tp>::~__thread_specific_ptr() {
0101
0102
0103
0104
0105 }
0106
0107 template <class _Tp>
0108 void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) {
0109 _LIBCPP_ASSERT_INTERNAL(get() == nullptr, "Attempting to overwrite thread local data");
0110 std::__libcpp_tls_set(__key_, __p);
0111 }
0112
0113 template <>
0114 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> {
0115 _LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT {
0116 return hash<__libcpp_thread_id>()(__v.__id_);
0117 }
0118 };
0119
0120 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
0121 template <class _CharT, class _Traits>
0122 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0123 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) {
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140 basic_ostringstream<_CharT, _Traits> __sstr;
0141 __sstr.imbue(locale::classic());
0142 __sstr << __id.__id_;
0143 return __os << __sstr.str();
0144 }
0145 #endif
0146
0147 class _LIBCPP_EXPORTED_FROM_ABI thread {
0148 __libcpp_thread_t __t_;
0149
0150 thread(const thread&);
0151 thread& operator=(const thread&);
0152
0153 public:
0154 typedef __thread_id id;
0155 typedef __libcpp_thread_t native_handle_type;
0156
0157 _LIBCPP_HIDE_FROM_ABI thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
0158 #ifndef _LIBCPP_CXX03_LANG
0159 template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> = 0>
0160 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp&& __f, _Args&&... __args);
0161 #else
0162 template <class _Fp>
0163 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp __f);
0164 #endif
0165 ~thread();
0166
0167 _LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; }
0168
0169 _LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT {
0170 if (!__libcpp_thread_isnull(&__t_))
0171 terminate();
0172 __t_ = __t.__t_;
0173 __t.__t_ = _LIBCPP_NULL_THREAD;
0174 return *this;
0175 }
0176
0177 _LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); }
0178
0179 _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); }
0180 void join();
0181 void detach();
0182 _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); }
0183 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; }
0184
0185 static unsigned hardware_concurrency() _NOEXCEPT;
0186 };
0187
0188 #ifndef _LIBCPP_CXX03_LANG
0189
0190 template <class _TSp, class _Fp, class... _Args, size_t... _Indices>
0191 inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) {
0192 std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...);
0193 }
0194
0195 template <class _Fp>
0196 _LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) {
0197
0198 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
0199 __thread_local_data().set_pointer(std::get<0>(*__p.get()).release());
0200 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
0201 std::__thread_execute(*__p.get(), _Index());
0202 return nullptr;
0203 }
0204
0205 template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> >
0206 thread::thread(_Fp&& __f, _Args&&... __args) {
0207 typedef unique_ptr<__thread_struct> _TSPtr;
0208 _TSPtr __tsp(new __thread_struct);
0209 typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp;
0210 unique_ptr<_Gp> __p(new _Gp(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
0211 int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
0212 if (__ec == 0)
0213 __p.release();
0214 else
0215 __throw_system_error(__ec, "thread constructor failed");
0216 }
0217
0218 #else
0219
0220 template <class _Fp>
0221 struct __thread_invoke_pair {
0222
0223
0224
0225 _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
0226 unique_ptr<__thread_struct> __tsp_;
0227 _Fp __fn_;
0228 };
0229
0230 template <class _Fp>
0231 _LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) {
0232 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
0233 __thread_local_data().set_pointer(__p->__tsp_.release());
0234 (__p->__fn_)();
0235 return nullptr;
0236 }
0237
0238 template <class _Fp>
0239 thread::thread(_Fp __f) {
0240 typedef __thread_invoke_pair<_Fp> _InvokePair;
0241 typedef unique_ptr<_InvokePair> _PairPtr;
0242 _PairPtr __pp(new _InvokePair(__f));
0243 int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
0244 if (__ec == 0)
0245 __pp.release();
0246 else
0247 __throw_system_error(__ec, "thread constructor failed");
0248 }
0249
0250 #endif
0251
0252 inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); }
0253
0254 _LIBCPP_END_NAMESPACE_STD
0255
0256 _LIBCPP_POP_MACROS
0257
0258 #endif