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-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_THREAD 11#define _LIBCPP_THREAD 12 13/* 14 15 thread synopsis 16 17namespace std 18{ 19 20class thread 21{ 22public: 23 class id; 24 typedef pthread_t native_handle_type; 25 26 thread() noexcept; 27 template <class F, class ...Args> explicit thread(F&& f, Args&&... args); 28 ~thread(); 29 30 thread(const thread&) = delete; 31 thread(thread&& t) noexcept; 32 33 thread& operator=(const thread&) = delete; 34 thread& operator=(thread&& t) noexcept; 35 36 void swap(thread& t) noexcept; 37 38 bool joinable() const noexcept; 39 void join(); 40 void detach(); 41 id get_id() const noexcept; 42 native_handle_type native_handle(); 43 44 static unsigned hardware_concurrency() noexcept; 45}; 46 47void swap(thread& x, thread& y) noexcept; 48 49class thread::id 50{ 51public: 52 id() noexcept; 53}; 54 55bool operator==(thread::id x, thread::id y) noexcept; 56bool operator!=(thread::id x, thread::id y) noexcept; 57bool operator< (thread::id x, thread::id y) noexcept; 58bool operator<=(thread::id x, thread::id y) noexcept; 59bool operator> (thread::id x, thread::id y) noexcept; 60bool operator>=(thread::id x, thread::id y) noexcept; 61 62template<class charT, class traits> 63basic_ostream<charT, traits>& 64operator<<(basic_ostream<charT, traits>& out, thread::id id); 65 66namespace this_thread 67{ 68 69thread::id get_id() noexcept; 70 71void yield() noexcept; 72 73template <class Clock, class Duration> 74void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); 75 76template <class Rep, class Period> 77void sleep_for(const chrono::duration<Rep, Period>& rel_time); 78 79} // this_thread 80 81} // std 82 83*/ 84 85#include <__config> 86#include <__debug> 87#include <__functional_base> 88#include <__mutex_base> 89#include <__thread/poll_with_backoff.h> 90#include <__threading_support> 91#include <__utility/decay_copy.h> 92#include <__utility/forward.h> 93#include <chrono> 94#include <cstddef> 95#include <functional> 96#include <iosfwd> 97#include <memory> 98#include <system_error> 99#include <tuple> 100#include <type_traits> 101 102#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 103#pragma GCC system_header 104#endif 105 106_LIBCPP_PUSH_MACROS 107#include <__undef_macros> 108 109#ifdef _LIBCPP_HAS_NO_THREADS 110#error <thread> is not supported on this single threaded system 111#else // !_LIBCPP_HAS_NO_THREADS 112 113_LIBCPP_BEGIN_NAMESPACE_STD 114 115template <class _Tp> class __thread_specific_ptr; 116class _LIBCPP_TYPE_VIS __thread_struct; 117class _LIBCPP_HIDDEN __thread_struct_imp; 118class __assoc_sub_state; 119 120_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); 121 122class _LIBCPP_TYPE_VIS __thread_struct 123{ 124 __thread_struct_imp* __p_; 125 126 __thread_struct(const __thread_struct&); 127 __thread_struct& operator=(const __thread_struct&); 128public: 129 __thread_struct(); 130 ~__thread_struct(); 131 132 void notify_all_at_thread_exit(condition_variable*, mutex*); 133 void __make_ready_at_thread_exit(__assoc_sub_state*); 134}; 135 136template <class _Tp> 137class __thread_specific_ptr 138{ 139 __libcpp_tls_key __key_; 140 141 // Only __thread_local_data() may construct a __thread_specific_ptr 142 // and only with _Tp == __thread_struct. 143 static_assert((is_same<_Tp, __thread_struct>::value), ""); 144 __thread_specific_ptr(); 145 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); 146 147 __thread_specific_ptr(const __thread_specific_ptr&); 148 __thread_specific_ptr& operator=(const __thread_specific_ptr&); 149 150 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); 151 152public: 153 typedef _Tp* pointer; 154 155 ~__thread_specific_ptr(); 156 157 _LIBCPP_INLINE_VISIBILITY 158 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));} 159 _LIBCPP_INLINE_VISIBILITY 160 pointer operator*() const {return *get();} 161 _LIBCPP_INLINE_VISIBILITY 162 pointer operator->() const {return get();} 163 void set_pointer(pointer __p); 164}; 165 166template <class _Tp> 167void _LIBCPP_TLS_DESTRUCTOR_CC 168__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) 169{ 170 delete static_cast<pointer>(__p); 171} 172 173template <class _Tp> 174__thread_specific_ptr<_Tp>::__thread_specific_ptr() 175{ 176 int __ec = 177 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit); 178 if (__ec) 179 __throw_system_error(__ec, "__thread_specific_ptr construction failed"); 180} 181 182template <class _Tp> 183__thread_specific_ptr<_Tp>::~__thread_specific_ptr() 184{ 185 // __thread_specific_ptr is only created with a static storage duration 186 // so this destructor is only invoked during program termination. Invoking 187 // pthread_key_delete(__key_) may prevent other threads from deleting their 188 // thread local data. For this reason we leak the key. 189} 190 191template <class _Tp> 192void 193__thread_specific_ptr<_Tp>::set_pointer(pointer __p) 194{ 195 _LIBCPP_ASSERT(get() == nullptr, 196 "Attempting to overwrite thread local data"); 197 __libcpp_tls_set(__key_, __p); 198} 199 200template<> 201struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> 202 : public unary_function<__thread_id, size_t> 203{ 204 _LIBCPP_INLINE_VISIBILITY 205 size_t operator()(__thread_id __v) const _NOEXCEPT 206 { 207 return hash<__libcpp_thread_id>()(__v.__id_); 208 } 209}; 210 211template<class _CharT, class _Traits> 212_LIBCPP_INLINE_VISIBILITY 213basic_ostream<_CharT, _Traits>& 214operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) 215{return __os << __id.__id_;} 216 217class _LIBCPP_TYPE_VIS thread 218{ 219 __libcpp_thread_t __t_; 220 221 thread(const thread&); 222 thread& operator=(const thread&); 223public: 224 typedef __thread_id id; 225 typedef __libcpp_thread_t native_handle_type; 226 227 _LIBCPP_INLINE_VISIBILITY 228 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} 229#ifndef _LIBCPP_CXX03_LANG 230 template <class _Fp, class ..._Args, 231 class = typename enable_if 232 < 233 !is_same<typename __uncvref<_Fp>::type, thread>::value 234 >::type 235 > 236 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS 237 explicit thread(_Fp&& __f, _Args&&... __args); 238#else // _LIBCPP_CXX03_LANG 239 template <class _Fp> 240 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS 241 explicit thread(_Fp __f); 242#endif 243 ~thread(); 244 245 _LIBCPP_INLINE_VISIBILITY 246 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { 247 __t.__t_ = _LIBCPP_NULL_THREAD; 248 } 249 250 _LIBCPP_INLINE_VISIBILITY 251 thread& operator=(thread&& __t) _NOEXCEPT { 252 if (!__libcpp_thread_isnull(&__t_)) 253 terminate(); 254 __t_ = __t.__t_; 255 __t.__t_ = _LIBCPP_NULL_THREAD; 256 return *this; 257 } 258 259 _LIBCPP_INLINE_VISIBILITY 260 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} 261 262 _LIBCPP_INLINE_VISIBILITY 263 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);} 264 void join(); 265 void detach(); 266 _LIBCPP_INLINE_VISIBILITY 267 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);} 268 _LIBCPP_INLINE_VISIBILITY 269 native_handle_type native_handle() _NOEXCEPT {return __t_;} 270 271 static unsigned hardware_concurrency() _NOEXCEPT; 272}; 273 274#ifndef _LIBCPP_CXX03_LANG 275 276template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices> 277inline _LIBCPP_INLINE_VISIBILITY 278void 279__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) 280{ 281 _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); 282} 283 284template <class _Fp> 285_LIBCPP_INLINE_VISIBILITY 286void* __thread_proxy(void* __vp) 287{ 288 // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...> 289 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 290 __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release()); 291 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; 292 _VSTD::__thread_execute(*__p.get(), _Index()); 293 return nullptr; 294} 295 296template <class _Fp, class ..._Args, 297 class 298 > 299thread::thread(_Fp&& __f, _Args&&... __args) 300{ 301 typedef unique_ptr<__thread_struct> _TSPtr; 302 _TSPtr __tsp(new __thread_struct); 303 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp; 304 unique_ptr<_Gp> __p( 305 new _Gp(_VSTD::move(__tsp), 306 _VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)), 307 _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...)); 308 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); 309 if (__ec == 0) 310 __p.release(); 311 else 312 __throw_system_error(__ec, "thread constructor failed"); 313} 314 315#else // _LIBCPP_CXX03_LANG 316 317template <class _Fp> 318struct __thread_invoke_pair { 319 // This type is used to pass memory for thread local storage and a functor 320 // to a newly created thread because std::pair doesn't work with 321 // std::unique_ptr in C++03. 322 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} 323 unique_ptr<__thread_struct> __tsp_; 324 _Fp __fn_; 325}; 326 327template <class _Fp> 328void* __thread_proxy_cxx03(void* __vp) 329{ 330 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 331 __thread_local_data().set_pointer(__p->__tsp_.release()); 332 (__p->__fn_)(); 333 return nullptr; 334} 335 336template <class _Fp> 337thread::thread(_Fp __f) 338{ 339 340 typedef __thread_invoke_pair<_Fp> _InvokePair; 341 typedef unique_ptr<_InvokePair> _PairPtr; 342 _PairPtr __pp(new _InvokePair(__f)); 343 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); 344 if (__ec == 0) 345 __pp.release(); 346 else 347 __throw_system_error(__ec, "thread constructor failed"); 348} 349 350#endif // _LIBCPP_CXX03_LANG 351 352inline _LIBCPP_INLINE_VISIBILITY 353void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} 354 355namespace this_thread 356{ 357 358_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns); 359 360template <class _Rep, class _Period> 361void 362sleep_for(const chrono::duration<_Rep, _Period>& __d) 363{ 364 if (__d > chrono::duration<_Rep, _Period>::zero()) 365 { 366 // The standard guarantees a 64bit signed integer resolution for nanoseconds, 367 // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits> 368 // and issues with long double folding on PowerPC with GCC. 369 _LIBCPP_CONSTEXPR chrono::duration<long double> _Max = 370 chrono::duration<long double>(9223372036.0L); 371 chrono::nanoseconds __ns; 372 if (__d < _Max) 373 { 374 __ns = chrono::duration_cast<chrono::nanoseconds>(__d); 375 if (__ns < __d) 376 ++__ns; 377 } 378 else 379 __ns = chrono::nanoseconds::max(); 380 this_thread::sleep_for(__ns); 381 } 382} 383 384template <class _Clock, class _Duration> 385void 386sleep_until(const chrono::time_point<_Clock, _Duration>& __t) 387{ 388 mutex __mut; 389 condition_variable __cv; 390 unique_lock<mutex> __lk(__mut); 391 while (_Clock::now() < __t) 392 __cv.wait_until(__lk, __t); 393} 394 395template <class _Duration> 396inline _LIBCPP_INLINE_VISIBILITY 397void 398sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) 399{ 400 this_thread::sleep_for(__t - chrono::steady_clock::now()); 401} 402 403inline _LIBCPP_INLINE_VISIBILITY 404void yield() _NOEXCEPT {__libcpp_thread_yield();} 405 406} // this_thread 407 408_LIBCPP_END_NAMESPACE_STD 409 410#endif // !_LIBCPP_HAS_NO_THREADS 411 412_LIBCPP_POP_MACROS 413 414#endif // _LIBCPP_THREAD 415