Path: blob/main/contrib/llvm-project/libcxx/include/__thread/thread.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___THREAD_THREAD_H10#define _LIBCPP___THREAD_THREAD_H1112#include <__condition_variable/condition_variable.h>13#include <__config>14#include <__exception/terminate.h>15#include <__functional/hash.h>16#include <__functional/unary_function.h>17#include <__memory/unique_ptr.h>18#include <__mutex/mutex.h>19#include <__system_error/system_error.h>20#include <__thread/id.h>21#include <__thread/support.h>22#include <__utility/forward.h>23#include <tuple>2425#ifndef _LIBCPP_HAS_NO_LOCALIZATION26# include <locale>27# include <sstream>28#endif2930#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)31# pragma GCC system_header32#endif3334_LIBCPP_PUSH_MACROS35#include <__undef_macros>3637_LIBCPP_BEGIN_NAMESPACE_STD3839template <class _Tp>40class __thread_specific_ptr;41class _LIBCPP_EXPORTED_FROM_ABI __thread_struct;42class _LIBCPP_HIDDEN __thread_struct_imp;43class __assoc_sub_state;4445_LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();4647class _LIBCPP_EXPORTED_FROM_ABI __thread_struct {48__thread_struct_imp* __p_;4950__thread_struct(const __thread_struct&);51__thread_struct& operator=(const __thread_struct&);5253public:54__thread_struct();55~__thread_struct();5657void notify_all_at_thread_exit(condition_variable*, mutex*);58void __make_ready_at_thread_exit(__assoc_sub_state*);59};6061template <class _Tp>62class __thread_specific_ptr {63__libcpp_tls_key __key_;6465// Only __thread_local_data() may construct a __thread_specific_ptr66// and only with _Tp == __thread_struct.67static_assert(is_same<_Tp, __thread_struct>::value, "");68__thread_specific_ptr();69friend _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data();7071_LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);7273public:74typedef _Tp* pointer;7576__thread_specific_ptr(const __thread_specific_ptr&) = delete;77__thread_specific_ptr& operator=(const __thread_specific_ptr&) = delete;78~__thread_specific_ptr();7980_LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); }81_LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); }82_LIBCPP_HIDE_FROM_ABI pointer operator->() const { return get(); }83void set_pointer(pointer __p);84};8586template <class _Tp>87void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) {88delete static_cast<pointer>(__p);89}9091template <class _Tp>92__thread_specific_ptr<_Tp>::__thread_specific_ptr() {93int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);94if (__ec)95__throw_system_error(__ec, "__thread_specific_ptr construction failed");96}9798template <class _Tp>99__thread_specific_ptr<_Tp>::~__thread_specific_ptr() {100// __thread_specific_ptr is only created with a static storage duration101// so this destructor is only invoked during program termination. Invoking102// pthread_key_delete(__key_) may prevent other threads from deleting their103// thread local data. For this reason we leak the key.104}105106template <class _Tp>107void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) {108_LIBCPP_ASSERT_INTERNAL(get() == nullptr, "Attempting to overwrite thread local data");109std::__libcpp_tls_set(__key_, __p);110}111112template <>113struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> {114_LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT {115return hash<__libcpp_thread_id>()(__v.__id_);116}117};118119#ifndef _LIBCPP_HAS_NO_LOCALIZATION120template <class _CharT, class _Traits>121_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&122operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) {123// [thread.thread.id]/9124// Effects: Inserts the text representation for charT of id into out.125//126// [thread.thread.id]/2127// The text representation for the character type charT of an128// object of type thread::id is an unspecified sequence of charT129// such that, for two objects of type thread::id x and y, if130// x == y is true, the thread::id objects have the same text131// representation, and if x != y is true, the thread::id objects132// have distinct text representations.133//134// Since various flags in the output stream can affect how the135// thread id is represented (e.g. numpunct or showbase), we136// use a temporary stream instead and just output the thread137// id representation as a string.138139basic_ostringstream<_CharT, _Traits> __sstr;140__sstr.imbue(locale::classic());141__sstr << __id.__id_;142return __os << __sstr.str();143}144#endif // _LIBCPP_HAS_NO_LOCALIZATION145146class _LIBCPP_EXPORTED_FROM_ABI thread {147__libcpp_thread_t __t_;148149thread(const thread&);150thread& operator=(const thread&);151152public:153typedef __thread_id id;154typedef __libcpp_thread_t native_handle_type;155156_LIBCPP_HIDE_FROM_ABI thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}157#ifndef _LIBCPP_CXX03_LANG158template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> = 0>159_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp&& __f, _Args&&... __args);160#else // _LIBCPP_CXX03_LANG161template <class _Fp>162_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp __f);163#endif164~thread();165166_LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; }167168_LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT {169if (!__libcpp_thread_isnull(&__t_))170terminate();171__t_ = __t.__t_;172__t.__t_ = _LIBCPP_NULL_THREAD;173return *this;174}175176_LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); }177178_LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); }179void join();180void detach();181_LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); }182_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; }183184static unsigned hardware_concurrency() _NOEXCEPT;185};186187#ifndef _LIBCPP_CXX03_LANG188189template <class _TSp, class _Fp, class... _Args, size_t... _Indices>190inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) {191std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...);192}193194template <class _Fp>195_LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) {196// _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>197unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));198__thread_local_data().set_pointer(std::get<0>(*__p.get()).release());199typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;200std::__thread_execute(*__p.get(), _Index());201return nullptr;202}203204template <class _Fp, class... _Args, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value, int> >205thread::thread(_Fp&& __f, _Args&&... __args) {206typedef unique_ptr<__thread_struct> _TSPtr;207_TSPtr __tsp(new __thread_struct);208typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp;209unique_ptr<_Gp> __p(new _Gp(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...));210int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());211if (__ec == 0)212__p.release();213else214__throw_system_error(__ec, "thread constructor failed");215}216217#else // _LIBCPP_CXX03_LANG218219template <class _Fp>220struct __thread_invoke_pair {221// This type is used to pass memory for thread local storage and a functor222// to a newly created thread because std::pair doesn't work with223// std::unique_ptr in C++03.224_LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}225unique_ptr<__thread_struct> __tsp_;226_Fp __fn_;227};228229template <class _Fp>230_LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) {231unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));232__thread_local_data().set_pointer(__p->__tsp_.release());233(__p->__fn_)();234return nullptr;235}236237template <class _Fp>238thread::thread(_Fp __f) {239typedef __thread_invoke_pair<_Fp> _InvokePair;240typedef unique_ptr<_InvokePair> _PairPtr;241_PairPtr __pp(new _InvokePair(__f));242int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());243if (__ec == 0)244__pp.release();245else246__throw_system_error(__ec, "thread constructor failed");247}248249#endif // _LIBCPP_CXX03_LANG250251inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); }252253_LIBCPP_END_NAMESPACE_STD254255_LIBCPP_POP_MACROS256257#endif // _LIBCPP___THREAD_THREAD_H258259260