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 <pthread.h> 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#if !_LIBCPP_SINGLE_THREADED 110 111_LIBCPP_BEGIN_NAMESPACE_STD 112 113template <class _Tp> 114class __thread_specific_ptr 115{ 116 pthread_key_t __key_; 117 118 __thread_specific_ptr(const __thread_specific_ptr&); 119 __thread_specific_ptr& operator=(const __thread_specific_ptr&); 120 121 static void __at_thread_exit(void*); 122public: 123 typedef _Tp* pointer; 124 125 __thread_specific_ptr(); 126 ~__thread_specific_ptr(); 127 128 _LIBCPP_INLINE_VISIBILITY 129 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));} 130 _LIBCPP_INLINE_VISIBILITY 131 pointer operator*() const {return *get();} 132 _LIBCPP_INLINE_VISIBILITY 133 pointer operator->() const {return get();} 134 pointer release(); 135 void reset(pointer __p = nullptr); 136}; 137 138template <class _Tp> 139void 140__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) 141{ 142 delete static_cast<pointer>(__p); 143} 144 145template <class _Tp> 146__thread_specific_ptr<_Tp>::__thread_specific_ptr() 147{ 148 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit); 149#ifndef _LIBCPP_NO_EXCEPTIONS 150 if (__ec) 151 throw system_error(error_code(__ec, system_category()), 152 "__thread_specific_ptr construction failed"); 153#endif 154} 155 156template <class _Tp> 157__thread_specific_ptr<_Tp>::~__thread_specific_ptr() 158{ 159 pthread_key_delete(__key_); 160} 161 162template <class _Tp> 163typename __thread_specific_ptr<_Tp>::pointer 164__thread_specific_ptr<_Tp>::release() 165{ 166 pointer __p = get(); 167 pthread_setspecific(__key_, 0); 168 return __p; 169} 170 171template <class _Tp> 172void 173__thread_specific_ptr<_Tp>::reset(pointer __p) 174{ 175 pointer __p_old = get(); 176 pthread_setspecific(__key_, __p); 177 delete __p_old; 178} 179 180class _LIBCPP_TYPE_VIS thread; 181class _LIBCPP_TYPE_VIS __thread_id; 182 183namespace this_thread 184{ 185 186_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; 187 188} // this_thread 189 190template<> struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>; 191 192class _LIBCPP_TYPE_VIS_ONLY __thread_id 193{ 194 // FIXME: pthread_t is a pointer on Darwin but a long on Linux. 195 // NULL is the no-thread value on Darwin. Someone needs to check 196 // on other platforms. We assume 0 works everywhere for now. 197 pthread_t __id_; 198 199public: 200 _LIBCPP_INLINE_VISIBILITY 201 __thread_id() _NOEXCEPT : __id_(0) {} 202 203 friend _LIBCPP_INLINE_VISIBILITY 204 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT 205 {return __x.__id_ == __y.__id_;} 206 friend _LIBCPP_INLINE_VISIBILITY 207 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT 208 {return !(__x == __y);} 209 friend _LIBCPP_INLINE_VISIBILITY 210 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT 211 {return __x.__id_ < __y.__id_;} 212 friend _LIBCPP_INLINE_VISIBILITY 213 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT 214 {return !(__y < __x);} 215 friend _LIBCPP_INLINE_VISIBILITY 216 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT 217 {return __y < __x ;} 218 friend _LIBCPP_INLINE_VISIBILITY 219 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT 220 {return !(__x < __y);} 221 222 template<class _CharT, class _Traits> 223 friend 224 _LIBCPP_INLINE_VISIBILITY 225 basic_ostream<_CharT, _Traits>& 226 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) 227 {return __os << __id.__id_;} 228 229private: 230 _LIBCPP_INLINE_VISIBILITY 231 __thread_id(pthread_t __id) : __id_(__id) {} 232 233 friend __thread_id this_thread::get_id() _NOEXCEPT; 234 friend class _LIBCPP_TYPE_VIS thread; 235 friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>; 236}; 237 238template<> 239struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id> 240 : public unary_function<__thread_id, size_t> 241{ 242 _LIBCPP_INLINE_VISIBILITY 243 size_t operator()(__thread_id __v) const 244 { 245 return hash<pthread_t>()(__v.__id_); 246 } 247}; 248 249namespace this_thread 250{ 251 252inline _LIBCPP_INLINE_VISIBILITY 253__thread_id 254get_id() _NOEXCEPT 255{ 256 return pthread_self(); 257} 258 259} // this_thread 260 261class _LIBCPP_TYPE_VIS thread 262{ 263 pthread_t __t_; 264 265 thread(const thread&); 266 thread& operator=(const thread&); 267public: 268 typedef __thread_id id; 269 typedef pthread_t native_handle_type; 270 271 _LIBCPP_INLINE_VISIBILITY 272 thread() _NOEXCEPT : __t_(0) {} 273#ifndef _LIBCPP_HAS_NO_VARIADICS 274 template <class _Fp, class ..._Args, 275 class = typename enable_if 276 < 277 !is_same<typename decay<_Fp>::type, thread>::value 278 >::type 279 > 280 explicit thread(_Fp&& __f, _Args&&... __args); 281#else // _LIBCPP_HAS_NO_VARIADICS 282 template <class _Fp> explicit thread(_Fp __f); 283#endif 284 ~thread(); 285 286#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 287 _LIBCPP_INLINE_VISIBILITY 288 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;} 289 thread& operator=(thread&& __t) _NOEXCEPT; 290#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 291 292 _LIBCPP_INLINE_VISIBILITY 293 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} 294 295 _LIBCPP_INLINE_VISIBILITY 296 bool joinable() const _NOEXCEPT {return __t_ != 0;} 297 void join(); 298 void detach(); 299 _LIBCPP_INLINE_VISIBILITY 300 id get_id() const _NOEXCEPT {return __t_;} 301 _LIBCPP_INLINE_VISIBILITY 302 native_handle_type native_handle() _NOEXCEPT {return __t_;} 303 304 static unsigned hardware_concurrency() _NOEXCEPT; 305}; 306 307class __assoc_sub_state; 308 309class _LIBCPP_HIDDEN __thread_struct_imp; 310 311class _LIBCPP_TYPE_VIS __thread_struct 312{ 313 __thread_struct_imp* __p_; 314 315 __thread_struct(const __thread_struct&); 316 __thread_struct& operator=(const __thread_struct&); 317public: 318 __thread_struct(); 319 ~__thread_struct(); 320 321 void notify_all_at_thread_exit(condition_variable*, mutex*); 322 void __make_ready_at_thread_exit(__assoc_sub_state*); 323}; 324 325_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); 326 327#ifndef _LIBCPP_HAS_NO_VARIADICS 328 329template <class _Fp, class ..._Args, size_t ..._Indices> 330inline _LIBCPP_INLINE_VISIBILITY 331void 332__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>) 333{ 334 __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); 335} 336 337template <class _Fp> 338void* 339__thread_proxy(void* __vp) 340{ 341 __thread_local_data().reset(new __thread_struct); 342 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 343 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; 344 __thread_execute(*__p, _Index()); 345 return nullptr; 346} 347 348template <class _Fp, class ..._Args, 349 class 350 > 351thread::thread(_Fp&& __f, _Args&&... __args) 352{ 353 typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp; 354 _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)), 355 __decay_copy(_VSTD::forward<_Args>(__args))...)); 356 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get()); 357 if (__ec == 0) 358 __p.release(); 359 else 360 __throw_system_error(__ec, "thread constructor failed"); 361} 362 363#else // _LIBCPP_HAS_NO_VARIADICS 364 365template <class _Fp> 366void* 367__thread_proxy(void* __vp) 368{ 369 __thread_local_data().reset(new __thread_struct); 370 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 371 (*__p)(); 372 return nullptr; 373} 374 375template <class _Fp> 376thread::thread(_Fp __f) 377{ 378 std::unique_ptr<_Fp> __p(new _Fp(__f)); 379 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get()); 380 if (__ec == 0) 381 __p.release(); 382 else 383 __throw_system_error(__ec, "thread constructor failed"); 384} 385 386#endif // _LIBCPP_HAS_NO_VARIADICS 387 388#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 389 390inline _LIBCPP_INLINE_VISIBILITY 391thread& 392thread::operator=(thread&& __t) _NOEXCEPT 393{ 394 if (__t_ != 0) 395 terminate(); 396 __t_ = __t.__t_; 397 __t.__t_ = 0; 398 return *this; 399} 400 401#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 402 403inline _LIBCPP_INLINE_VISIBILITY 404void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} 405 406namespace this_thread 407{ 408 409_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns); 410 411template <class _Rep, class _Period> 412void 413sleep_for(const chrono::duration<_Rep, _Period>& __d) 414{ 415 using namespace chrono; 416 if (__d > duration<_Rep, _Period>::zero()) 417 { 418 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max(); 419 nanoseconds __ns; 420 if (__d < _Max) 421 { 422 __ns = duration_cast<nanoseconds>(__d); 423 if (__ns < __d) 424 ++__ns; 425 } 426 else 427 __ns = nanoseconds::max(); 428 sleep_for(__ns); 429 } 430} 431 432template <class _Clock, class _Duration> 433void 434sleep_until(const chrono::time_point<_Clock, _Duration>& __t) 435{ 436 using namespace chrono; 437 mutex __mut; 438 condition_variable __cv; 439 unique_lock<mutex> __lk(__mut); 440 while (_Clock::now() < __t) 441 __cv.wait_until(__lk, __t); 442} 443 444template <class _Duration> 445inline _LIBCPP_INLINE_VISIBILITY 446void 447sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) 448{ 449 using namespace chrono; 450 sleep_for(__t - steady_clock::now()); 451} 452 453inline _LIBCPP_INLINE_VISIBILITY 454void yield() _NOEXCEPT {sched_yield();} 455 456} // this_thread 457 458_LIBCPP_END_NAMESPACE_STD 459 460#endif // !_LIBCPP_SINGLE_THREADED 461 462#endif // _LIBCPP_THREAD 463