263 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___THREAD_THREAD_H11#define _LIBCPP___THREAD_THREAD_H12 13#include <__assert>14#include <__condition_variable/condition_variable.h>15#include <__config>16#include <__exception/terminate.h>17#include <__functional/hash.h>18#include <__functional/unary_function.h>19#include <__locale>20#include <__memory/addressof.h>21#include <__memory/unique_ptr.h>22#include <__mutex/mutex.h>23#include <__system_error/throw_system_error.h>24#include <__thread/id.h>25#include <__thread/support.h>26#include <__type_traits/decay.h>27#include <__type_traits/enable_if.h>28#include <__type_traits/is_same.h>29#include <__type_traits/remove_cvref.h>30#include <__utility/forward.h>31#include <tuple>32 33#if _LIBCPP_HAS_LOCALIZATION34# include <sstream>35#endif36 37#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)38# pragma GCC system_header39#endif40 41_LIBCPP_PUSH_MACROS42#include <__undef_macros>43 44_LIBCPP_BEGIN_NAMESPACE_STD45 46#if _LIBCPP_HAS_THREADS47 48template <class _Tp>49class __thread_specific_ptr;50class _LIBCPP_EXPORTED_FROM_ABI __thread_struct;51class _LIBCPP_HIDDEN __thread_struct_imp;52class __assoc_sub_state;53 54_LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();55 56class _LIBCPP_EXPORTED_FROM_ABI __thread_struct {57 __thread_struct_imp* __p_;58 59 __thread_struct(const __thread_struct&);60 __thread_struct& operator=(const __thread_struct&);61 62public:63 __thread_struct();64 ~__thread_struct();65 66 void notify_all_at_thread_exit(condition_variable*, mutex*);67 void __make_ready_at_thread_exit(__assoc_sub_state*);68};69 70template <class _Tp>71class __thread_specific_ptr {72 __libcpp_tls_key __key_;73 74 // Only __thread_local_data() may construct a __thread_specific_ptr75 // and only with _Tp == __thread_struct.76 static_assert(is_same<_Tp, __thread_struct>::value, "");77 __thread_specific_ptr();78 friend _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();79 80 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);81 82public:83 typedef _Tp* pointer;84 85 __thread_specific_ptr(const __thread_specific_ptr&) = delete;86 __thread_specific_ptr& operator=(const __thread_specific_ptr&) = delete;87 ~__thread_specific_ptr();88 89 _LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); }90 _LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); }91 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return get(); }92 void set_pointer(pointer __p);93};94 95template <class _Tp>96void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) {97 delete static_cast<pointer>(__p);98}99 100template <class _Tp>101__thread_specific_ptr<_Tp>::__thread_specific_ptr() {102 int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);103 if (__ec)104 std::__throw_system_error(__ec, "__thread_specific_ptr construction failed");105}106 107template <class _Tp>108__thread_specific_ptr<_Tp>::~__thread_specific_ptr() {109 // __thread_specific_ptr is only created with a static storage duration110 // so this destructor is only invoked during program termination. Invoking111 // pthread_key_delete(__key_) may prevent other threads from deleting their112 // thread local data. For this reason we leak the key.113}114 115template <class _Tp>116void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) {117 _LIBCPP_ASSERT_INTERNAL(get() == nullptr, "Attempting to overwrite thread local data");118 std::__libcpp_tls_set(__key_, __p);119}120 121template <>122struct hash<__thread_id> : public __unary_function<__thread_id, size_t> {123 _LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT {124 return hash<__libcpp_thread_id>()(__v.__id_);125 }126};127 128# if _LIBCPP_HAS_LOCALIZATION129template <class _CharT, class _Traits>130_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&131operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) {132 // [thread.thread.id]/9133 // Effects: Inserts the text representation for charT of id into out.134 //135 // [thread.thread.id]/2136 // The text representation for the character type charT of an137 // object of type thread::id is an unspecified sequence of charT138 // such that, for two objects of type thread::id x and y, if139 // x == y is true, the thread::id objects have the same text140 // representation, and if x != y is true, the thread::id objects141 // have distinct text representations.142 //143 // Since various flags in the output stream can affect how the144 // thread id is represented (e.g. numpunct or showbase), we145 // use a temporary stream instead and just output the thread146 // id representation as a string.147 148 basic_ostringstream<_CharT, _Traits> __sstr;149 __sstr.imbue(locale::classic());150 __sstr << __id.__id_;151 return __os << __sstr.str();152}153# endif // _LIBCPP_HAS_LOCALIZATION154 155# ifndef _LIBCPP_CXX03_LANG156 157template <class _TSp, class _Fp, class... _Args, size_t... _Indices>158inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __index_sequence<_Indices...>) {159 std::__invoke(std::move(std::get<_Indices + 1>(__t))...);160}161 162template <class _Fp>163_LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) {164 // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>165 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));166 __thread_local_data().set_pointer(std::get<0>(*__p.get()).release());167 std::__thread_execute(*__p.get(), __make_index_sequence<tuple_size<_Fp>::value - 1>());168 return nullptr;169}170 171# else // _LIBCPP_CXX03_LANG172 173template <class _Fp>174struct __thread_invoke_pair {175 // This type is used to pass memory for thread local storage and a functor176 // to a newly created thread because std::pair doesn't work with177 // std::unique_ptr in C++03.178 _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}179 unique_ptr<__thread_struct> __tsp_;180 _Fp __fn_;181};182 183template <class _Fp>184_LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) {185 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));186 __thread_local_data().set_pointer(__p->__tsp_.release());187 (__p->__fn_)();188 return nullptr;189}190 191# endif // _LIBCPP_CXX03_LANG192 193class _LIBCPP_EXPORTED_FROM_ABI thread {194 __libcpp_thread_t __t_;195 196 thread(const thread&);197 thread& operator=(const thread&);198 199public:200 typedef __thread_id id;201 typedef __libcpp_thread_t native_handle_type;202 203 _LIBCPP_HIDE_FROM_ABI thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}204 205# ifndef _LIBCPP_CXX03_LANG206 template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> = 0>207 _LIBCPP_HIDE_FROM_ABI explicit thread(_Fp&& __f, _Args&&... __args) {208 typedef unique_ptr<__thread_struct> _TSPtr;209 _TSPtr __tsp(new __thread_struct);210 typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp;211 unique_ptr<_Gp> __p(new _Gp(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...));212 int __ec = std::__libcpp_thread_create(&__t_, std::addressof(__thread_proxy<_Gp>), __p.get());213 if (__ec == 0)214 __p.release();215 else216 __throw_system_error(__ec, "thread constructor failed");217 }218# else // _LIBCPP_CXX03_LANG219 template <class _Fp>220 _LIBCPP_HIDE_FROM_ABI explicit thread(_Fp __f) {221 typedef __thread_invoke_pair<_Fp> _InvokePair;222 typedef unique_ptr<_InvokePair> _PairPtr;223 _PairPtr __pp(new _InvokePair(__f));224 int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());225 if (__ec == 0)226 __pp.release();227 else228 __throw_system_error(__ec, "thread constructor failed");229 }230# endif231 ~thread();232 233 _LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; }234 235 _LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT {236 if (!__libcpp_thread_isnull(&__t_))237 terminate();238 __t_ = __t.__t_;239 __t.__t_ = _LIBCPP_NULL_THREAD;240 return *this;241 }242 243 _LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); }244 245 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); }246 void join();247 void detach();248 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); }249 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; }250 251 [[__nodiscard__]] static unsigned hardware_concurrency() _NOEXCEPT;252};253 254inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); }255 256#endif // _LIBCPP_HAS_THREADS257 258_LIBCPP_END_NAMESPACE_STD259 260_LIBCPP_POP_MACROS261 262#endif // _LIBCPP___THREAD_THREAD_H263