1// -*- C++ -*- 2//===--------------------------- thread -----------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_THREAD 12#define _LIBCPP_THREAD 13 14/* 15 16 thread synopsis 17 18#define __STDCPP_THREADS__ __cplusplus 19 20namespace std 21{ 22 23class thread 24{ 25public: 26 class id; 27 typedef pthread_t native_handle_type; 28 29 thread() noexcept; 30 template <class F, class ...Args> explicit thread(F&& f, Args&&... args); 31 ~thread(); 32 33 thread(const thread&) = delete; 34 thread(thread&& t) noexcept; 35 36 thread& operator=(const thread&) = delete; 37 thread& operator=(thread&& t) noexcept; 38 39 void swap(thread& t) noexcept; 40 41 bool joinable() const noexcept; 42 void join(); 43 void detach(); 44 id get_id() const noexcept; 45 native_handle_type native_handle(); 46 47 static unsigned hardware_concurrency() noexcept; 48}; 49 50void swap(thread& x, thread& y) noexcept; 51 52class thread::id 53{ 54public: 55 id() noexcept; 56}; 57 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; 61bool operator<=(thread::id x, thread::id y) noexcept; 62bool operator> (thread::id x, thread::id y) noexcept; 63bool operator>=(thread::id x, thread::id y) noexcept; 64 65template<class charT, class traits> 66basic_ostream<charT, traits>& 67operator<<(basic_ostream<charT, traits>& out, thread::id id); 68 69namespace this_thread 70{ 71 72thread::id get_id() noexcept; 73 74void yield() noexcept; 75 76template <class Clock, class Duration> 77void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); 78 79template <class Rep, class Period> 80void sleep_for(const chrono::duration<Rep, Period>& rel_time); 81 82} // this_thread 83 84} // std 85 86*/ 87 88#include <__config> 89#include <iosfwd> 90#include <__functional_base> 91#include <type_traits> 92#include <cstddef> 93#include <functional> 94#include <memory> 95#include <system_error> 96#include <chrono> 97#include <__mutex_base> 98#ifndef _LIBCPP_HAS_NO_VARIADICS 99#include <tuple> 100#endif 101#include <__threading_support> 102 103#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 104#pragma GCC system_header 105#endif 106 107#define __STDCPP_THREADS__ __cplusplus 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_tl_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 static void __at_thread_exit(void*); 151public: 152 typedef _Tp* pointer; 153 154 ~__thread_specific_ptr(); 155 156 _LIBCPP_INLINE_VISIBILITY 157 pointer get() const {return static_cast<_Tp*>(__libcpp_tl_get(__key_));} 158 _LIBCPP_INLINE_VISIBILITY 159 pointer operator*() const {return *get();} 160 _LIBCPP_INLINE_VISIBILITY 161 pointer operator->() const {return get();} 162 pointer release(); 163 void reset(pointer __p = nullptr); 164}; 165 166template <class _Tp> 167void 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 = __libcpp_tl_create( 177 &__key_, 178 &__thread_specific_ptr::__at_thread_exit); 179#ifndef _LIBCPP_NO_EXCEPTIONS 180 if (__ec) 181 throw system_error(error_code(__ec, system_category()), 182 "__thread_specific_ptr construction failed"); 183#endif 184} 185 186template <class _Tp> 187__thread_specific_ptr<_Tp>::~__thread_specific_ptr() 188{ 189 // __thread_specific_ptr is only created with a static storage duration 190 // so this destructor is only invoked during program termination. Invoking 191 // pthread_key_delete(__key_) may prevent other threads from deleting their 192 // thread local data. For this reason we leak the key. 193} 194 195template <class _Tp> 196typename __thread_specific_ptr<_Tp>::pointer 197__thread_specific_ptr<_Tp>::release() 198{ 199 pointer __p = get(); 200 __libcpp_tl_set(__key_, nullptr); 201 return __p; 202} 203 204template <class _Tp> 205void 206__thread_specific_ptr<_Tp>::reset(pointer __p) 207{ 208 pointer __p_old = get(); 209 __libcpp_tl_set(__key_, __p); 210 delete __p_old; 211} 212 213class _LIBCPP_TYPE_VIS thread; 214class _LIBCPP_TYPE_VIS __thread_id; 215 216namespace this_thread 217{ 218 219_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; 220 221} // this_thread 222 223template<> struct hash<__thread_id>; 224 225class _LIBCPP_TYPE_VIS_ONLY __thread_id 226{ 227 // FIXME: pthread_t is a pointer on Darwin but a long on Linux. 228 // NULL is the no-thread value on Darwin. Someone needs to check 229 // on other platforms. We assume 0 works everywhere for now. 230 __libcpp_thread_id __id_; 231 232public: 233 _LIBCPP_INLINE_VISIBILITY 234 __thread_id() _NOEXCEPT : __id_(0) {} 235 236 friend _LIBCPP_INLINE_VISIBILITY 237 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT 238 {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);} 239 friend _LIBCPP_INLINE_VISIBILITY 240 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT 241 {return !(__x == __y);} 242 friend _LIBCPP_INLINE_VISIBILITY 243 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT 244 {return __libcpp_thread_id_less(__x.__id_, __y.__id_);} 245 friend _LIBCPP_INLINE_VISIBILITY 246 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT 247 {return !(__y < __x);} 248 friend _LIBCPP_INLINE_VISIBILITY 249 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT 250 {return __y < __x ;} 251 friend _LIBCPP_INLINE_VISIBILITY 252 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT 253 {return !(__x < __y);} 254 255 template<class _CharT, class _Traits> 256 friend 257 _LIBCPP_INLINE_VISIBILITY 258 basic_ostream<_CharT, _Traits>& 259 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) 260 {return __os << __id.__id_;} 261 262private: 263 _LIBCPP_INLINE_VISIBILITY 264 __thread_id(__libcpp_thread_id __id) : __id_(__id) {} 265 266 friend __thread_id this_thread::get_id() _NOEXCEPT; 267 friend class _LIBCPP_TYPE_VIS thread; 268 friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>; 269}; 270 271template<> 272struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id> 273 : public unary_function<__thread_id, size_t> 274{ 275 _LIBCPP_INLINE_VISIBILITY 276 size_t operator()(__thread_id __v) const 277 { 278 return hash<__libcpp_thread_id>()(__v.__id_); 279 } 280}; 281 282namespace this_thread 283{ 284 285inline _LIBCPP_INLINE_VISIBILITY 286__thread_id 287get_id() _NOEXCEPT 288{ 289 return __libcpp_thread_get_current_id(); 290} 291 292} // this_thread 293 294class _LIBCPP_TYPE_VIS thread 295{ 296 __libcpp_thread_t __t_; 297 298 thread(const thread&); 299 thread& operator=(const thread&); 300public: 301 typedef __thread_id id; 302 typedef __libcpp_thread_t native_handle_type; 303 304 _LIBCPP_INLINE_VISIBILITY 305 thread() _NOEXCEPT : __t_(0) {} 306#ifndef _LIBCPP_HAS_NO_VARIADICS 307 template <class _Fp, class ..._Args, 308 class = typename enable_if 309 < 310 !is_same<typename decay<_Fp>::type, thread>::value 311 >::type 312 > 313 explicit thread(_Fp&& __f, _Args&&... __args); 314#else // _LIBCPP_HAS_NO_VARIADICS 315 template <class _Fp> explicit thread(_Fp __f); 316#endif 317 ~thread(); 318 319#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 320 _LIBCPP_INLINE_VISIBILITY 321 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;} 322 _LIBCPP_INLINE_VISIBILITY 323 thread& operator=(thread&& __t) _NOEXCEPT; 324#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 325 326 _LIBCPP_INLINE_VISIBILITY 327 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} 328 329 _LIBCPP_INLINE_VISIBILITY 330 bool joinable() const _NOEXCEPT {return __t_ != 0;} 331 void join(); 332 void detach(); 333 _LIBCPP_INLINE_VISIBILITY 334 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);} 335 _LIBCPP_INLINE_VISIBILITY 336 native_handle_type native_handle() _NOEXCEPT {return __t_;} 337 338 static unsigned hardware_concurrency() _NOEXCEPT; 339}; 340 341#ifndef _LIBCPP_HAS_NO_VARIADICS 342 343template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices> 344inline _LIBCPP_INLINE_VISIBILITY 345void 346__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) 347{ 348 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); 349} 350 351template <class _Fp> 352void* __thread_proxy(void* __vp) 353{ 354 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...> 355 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 356 __thread_local_data().reset(_VSTD::get<0>(*__p).release()); 357 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; 358 __thread_execute(*__p, _Index()); 359 return nullptr; 360} 361 362template <class _Fp, class ..._Args, 363 class 364 > 365thread::thread(_Fp&& __f, _Args&&... __args) 366{ 367 typedef unique_ptr<__thread_struct> _TSPtr; 368 _TSPtr __tsp(new __thread_struct); 369 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp; 370 _VSTD::unique_ptr<_Gp> __p( 371 new _Gp(std::move(__tsp), 372 __decay_copy(_VSTD::forward<_Fp>(__f)), 373 __decay_copy(_VSTD::forward<_Args>(__args))...)); 374 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); 375 if (__ec == 0) 376 __p.release(); 377 else 378 __throw_system_error(__ec, "thread constructor failed"); 379} 380 381#else // _LIBCPP_HAS_NO_VARIADICS 382 383template <class _Fp> 384struct __thread_invoke_pair { 385 // This type is used to pass memory for thread local storage and a functor 386 // to a newly created thread because std::pair doesn't work with 387 // std::unique_ptr in C++03. 388 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} 389 unique_ptr<__thread_struct> __tsp_; 390 _Fp __fn_; 391}; 392 393template <class _Fp> 394void* __thread_proxy_cxx03(void* __vp) 395{ 396 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 397 __thread_local_data().reset(__p->__tsp_.release()); 398 (__p->__fn_)(); 399 return nullptr; 400} 401 402template <class _Fp> 403thread::thread(_Fp __f) 404{ 405 406 typedef __thread_invoke_pair<_Fp> _InvokePair; 407 typedef std::unique_ptr<_InvokePair> _PairPtr; 408 _PairPtr __pp(new _InvokePair(__f)); 409 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); 410 if (__ec == 0) 411 __pp.release(); 412 else 413 __throw_system_error(__ec, "thread constructor failed"); 414} 415 416#endif // _LIBCPP_HAS_NO_VARIADICS 417 418#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 419 420inline 421thread& 422thread::operator=(thread&& __t) _NOEXCEPT 423{ 424 if (__t_ != 0) 425 terminate(); 426 __t_ = __t.__t_; 427 __t.__t_ = 0; 428 return *this; 429} 430 431#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 432 433inline _LIBCPP_INLINE_VISIBILITY 434void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} 435 436namespace this_thread 437{ 438 439_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns); 440 441template <class _Rep, class _Period> 442void 443sleep_for(const chrono::duration<_Rep, _Period>& __d) 444{ 445 using namespace chrono; 446 if (__d > duration<_Rep, _Period>::zero()) 447 { 448 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max(); 449 nanoseconds __ns; 450 if (__d < _Max) 451 { 452 __ns = duration_cast<nanoseconds>(__d); 453 if (__ns < __d) 454 ++__ns; 455 } 456 else 457 __ns = nanoseconds::max(); 458 sleep_for(__ns); 459 } 460} 461 462template <class _Clock, class _Duration> 463void 464sleep_until(const chrono::time_point<_Clock, _Duration>& __t) 465{ 466 using namespace chrono; 467 mutex __mut; 468 condition_variable __cv; 469 unique_lock<mutex> __lk(__mut); 470 while (_Clock::now() < __t) 471 __cv.wait_until(__lk, __t); 472} 473 474template <class _Duration> 475inline _LIBCPP_INLINE_VISIBILITY 476void 477sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) 478{ 479 using namespace chrono; 480 sleep_for(__t - steady_clock::now()); 481} 482 483inline _LIBCPP_INLINE_VISIBILITY 484void yield() _NOEXCEPT {__libcpp_thread_yield();} 485 486} // this_thread 487 488_LIBCPP_END_NAMESPACE_STD 489 490#endif // !_LIBCPP_HAS_NO_THREADS 491 492#endif // _LIBCPP_THREAD 493