14684ddb6SLionel Sambuc// -*- C++ -*- 24684ddb6SLionel Sambuc//===--------------------------- thread -----------------------------------===// 34684ddb6SLionel Sambuc// 44684ddb6SLionel Sambuc// The LLVM Compiler Infrastructure 54684ddb6SLionel Sambuc// 64684ddb6SLionel Sambuc// This file is dual licensed under the MIT and the University of Illinois Open 74684ddb6SLionel Sambuc// Source Licenses. See LICENSE.TXT for details. 84684ddb6SLionel Sambuc// 94684ddb6SLionel Sambuc//===----------------------------------------------------------------------===// 104684ddb6SLionel Sambuc 114684ddb6SLionel Sambuc#ifndef _LIBCPP_THREAD 124684ddb6SLionel Sambuc#define _LIBCPP_THREAD 134684ddb6SLionel Sambuc 144684ddb6SLionel Sambuc/* 154684ddb6SLionel Sambuc 164684ddb6SLionel Sambuc thread synopsis 174684ddb6SLionel Sambuc 184684ddb6SLionel Sambuc#define __STDCPP_THREADS__ __cplusplus 194684ddb6SLionel Sambuc 204684ddb6SLionel Sambucnamespace std 214684ddb6SLionel Sambuc{ 224684ddb6SLionel Sambuc 234684ddb6SLionel Sambucclass thread 244684ddb6SLionel Sambuc{ 254684ddb6SLionel Sambucpublic: 264684ddb6SLionel Sambuc class id; 274684ddb6SLionel Sambuc typedef pthread_t native_handle_type; 284684ddb6SLionel Sambuc 294684ddb6SLionel Sambuc thread() noexcept; 304684ddb6SLionel Sambuc template <class F, class ...Args> explicit thread(F&& f, Args&&... args); 314684ddb6SLionel Sambuc ~thread(); 324684ddb6SLionel Sambuc 334684ddb6SLionel Sambuc thread(const thread&) = delete; 344684ddb6SLionel Sambuc thread(thread&& t) noexcept; 354684ddb6SLionel Sambuc 364684ddb6SLionel Sambuc thread& operator=(const thread&) = delete; 374684ddb6SLionel Sambuc thread& operator=(thread&& t) noexcept; 384684ddb6SLionel Sambuc 394684ddb6SLionel Sambuc void swap(thread& t) noexcept; 404684ddb6SLionel Sambuc 414684ddb6SLionel Sambuc bool joinable() const noexcept; 424684ddb6SLionel Sambuc void join(); 434684ddb6SLionel Sambuc void detach(); 444684ddb6SLionel Sambuc id get_id() const noexcept; 454684ddb6SLionel Sambuc native_handle_type native_handle(); 464684ddb6SLionel Sambuc 474684ddb6SLionel Sambuc static unsigned hardware_concurrency() noexcept; 484684ddb6SLionel Sambuc}; 494684ddb6SLionel Sambuc 504684ddb6SLionel Sambucvoid swap(thread& x, thread& y) noexcept; 514684ddb6SLionel Sambuc 524684ddb6SLionel Sambucclass thread::id 534684ddb6SLionel Sambuc{ 544684ddb6SLionel Sambucpublic: 554684ddb6SLionel Sambuc id() noexcept; 564684ddb6SLionel Sambuc}; 574684ddb6SLionel Sambuc 584684ddb6SLionel Sambucbool operator==(thread::id x, thread::id y) noexcept; 594684ddb6SLionel Sambucbool operator!=(thread::id x, thread::id y) noexcept; 604684ddb6SLionel Sambucbool operator< (thread::id x, thread::id y) noexcept; 614684ddb6SLionel Sambucbool operator<=(thread::id x, thread::id y) noexcept; 624684ddb6SLionel Sambucbool operator> (thread::id x, thread::id y) noexcept; 634684ddb6SLionel Sambucbool operator>=(thread::id x, thread::id y) noexcept; 644684ddb6SLionel Sambuc 654684ddb6SLionel Sambuctemplate<class charT, class traits> 664684ddb6SLionel Sambucbasic_ostream<charT, traits>& 674684ddb6SLionel Sambucoperator<<(basic_ostream<charT, traits>& out, thread::id id); 684684ddb6SLionel Sambuc 694684ddb6SLionel Sambucnamespace this_thread 704684ddb6SLionel Sambuc{ 714684ddb6SLionel Sambuc 724684ddb6SLionel Sambucthread::id get_id() noexcept; 734684ddb6SLionel Sambuc 744684ddb6SLionel Sambucvoid yield() noexcept; 754684ddb6SLionel Sambuc 764684ddb6SLionel Sambuctemplate <class Clock, class Duration> 774684ddb6SLionel Sambucvoid sleep_until(const chrono::time_point<Clock, Duration>& abs_time); 784684ddb6SLionel Sambuc 794684ddb6SLionel Sambuctemplate <class Rep, class Period> 804684ddb6SLionel Sambucvoid sleep_for(const chrono::duration<Rep, Period>& rel_time); 814684ddb6SLionel Sambuc 824684ddb6SLionel Sambuc} // this_thread 834684ddb6SLionel Sambuc 844684ddb6SLionel Sambuc} // std 854684ddb6SLionel Sambuc 864684ddb6SLionel Sambuc*/ 874684ddb6SLionel Sambuc 884684ddb6SLionel Sambuc#include <__config> 894684ddb6SLionel Sambuc#include <iosfwd> 904684ddb6SLionel Sambuc#include <__functional_base> 914684ddb6SLionel Sambuc#include <type_traits> 924684ddb6SLionel Sambuc#include <cstddef> 934684ddb6SLionel Sambuc#include <functional> 944684ddb6SLionel Sambuc#include <memory> 954684ddb6SLionel Sambuc#include <system_error> 964684ddb6SLionel Sambuc#include <chrono> 974684ddb6SLionel Sambuc#include <__mutex_base> 984684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 994684ddb6SLionel Sambuc#include <tuple> 1004684ddb6SLionel Sambuc#endif 1014684ddb6SLionel Sambuc#include <pthread.h> 102*0a6a1f1dSLionel Sambuc#include <sched.h> 1034684ddb6SLionel Sambuc 1044684ddb6SLionel Sambuc#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1054684ddb6SLionel Sambuc#pragma GCC system_header 1064684ddb6SLionel Sambuc#endif 1074684ddb6SLionel Sambuc 1084684ddb6SLionel Sambuc#define __STDCPP_THREADS__ __cplusplus 1094684ddb6SLionel Sambuc 110*0a6a1f1dSLionel Sambuc#ifdef _LIBCPP_HAS_NO_THREADS 111*0a6a1f1dSLionel Sambuc#error <thread> is not supported on this single threaded system 112*0a6a1f1dSLionel Sambuc#else // !_LIBCPP_HAS_NO_THREADS 113*0a6a1f1dSLionel Sambuc 1144684ddb6SLionel Sambuc_LIBCPP_BEGIN_NAMESPACE_STD 1154684ddb6SLionel Sambuc 116*0a6a1f1dSLionel Sambuctemplate <class _Tp> class __thread_specific_ptr; 117*0a6a1f1dSLionel Sambucclass _LIBCPP_TYPE_VIS __thread_struct; 118*0a6a1f1dSLionel Sambucclass _LIBCPP_HIDDEN __thread_struct_imp; 119*0a6a1f1dSLionel Sambucclass __assoc_sub_state; 120*0a6a1f1dSLionel Sambuc 121*0a6a1f1dSLionel Sambuc_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); 122*0a6a1f1dSLionel Sambuc 123*0a6a1f1dSLionel Sambucclass _LIBCPP_TYPE_VIS __thread_struct 124*0a6a1f1dSLionel Sambuc{ 125*0a6a1f1dSLionel Sambuc __thread_struct_imp* __p_; 126*0a6a1f1dSLionel Sambuc 127*0a6a1f1dSLionel Sambuc __thread_struct(const __thread_struct&); 128*0a6a1f1dSLionel Sambuc __thread_struct& operator=(const __thread_struct&); 129*0a6a1f1dSLionel Sambucpublic: 130*0a6a1f1dSLionel Sambuc __thread_struct(); 131*0a6a1f1dSLionel Sambuc ~__thread_struct(); 132*0a6a1f1dSLionel Sambuc 133*0a6a1f1dSLionel Sambuc void notify_all_at_thread_exit(condition_variable*, mutex*); 134*0a6a1f1dSLionel Sambuc void __make_ready_at_thread_exit(__assoc_sub_state*); 135*0a6a1f1dSLionel Sambuc}; 136*0a6a1f1dSLionel Sambuc 1374684ddb6SLionel Sambuctemplate <class _Tp> 1384684ddb6SLionel Sambucclass __thread_specific_ptr 1394684ddb6SLionel Sambuc{ 1404684ddb6SLionel Sambuc pthread_key_t __key_; 1414684ddb6SLionel Sambuc 142*0a6a1f1dSLionel Sambuc // Only __thread_local_data() may construct a __thread_specific_ptr 143*0a6a1f1dSLionel Sambuc // and only with _Tp == __thread_struct. 144*0a6a1f1dSLionel Sambuc static_assert((is_same<_Tp, __thread_struct>::value), ""); 145*0a6a1f1dSLionel Sambuc __thread_specific_ptr(); 146*0a6a1f1dSLionel Sambuc friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); 147*0a6a1f1dSLionel Sambuc 1484684ddb6SLionel Sambuc __thread_specific_ptr(const __thread_specific_ptr&); 1494684ddb6SLionel Sambuc __thread_specific_ptr& operator=(const __thread_specific_ptr&); 1504684ddb6SLionel Sambuc 1514684ddb6SLionel Sambuc static void __at_thread_exit(void*); 1524684ddb6SLionel Sambucpublic: 1534684ddb6SLionel Sambuc typedef _Tp* pointer; 1544684ddb6SLionel Sambuc 1554684ddb6SLionel Sambuc ~__thread_specific_ptr(); 1564684ddb6SLionel Sambuc 1574684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 1584684ddb6SLionel Sambuc pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));} 1594684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 1604684ddb6SLionel Sambuc pointer operator*() const {return *get();} 1614684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 1624684ddb6SLionel Sambuc pointer operator->() const {return get();} 1634684ddb6SLionel Sambuc pointer release(); 1644684ddb6SLionel Sambuc void reset(pointer __p = nullptr); 1654684ddb6SLionel Sambuc}; 1664684ddb6SLionel Sambuc 1674684ddb6SLionel Sambuctemplate <class _Tp> 1684684ddb6SLionel Sambucvoid 1694684ddb6SLionel Sambuc__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) 1704684ddb6SLionel Sambuc{ 1714684ddb6SLionel Sambuc delete static_cast<pointer>(__p); 1724684ddb6SLionel Sambuc} 1734684ddb6SLionel Sambuc 1744684ddb6SLionel Sambuctemplate <class _Tp> 1754684ddb6SLionel Sambuc__thread_specific_ptr<_Tp>::__thread_specific_ptr() 1764684ddb6SLionel Sambuc{ 1774684ddb6SLionel Sambuc int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit); 1784684ddb6SLionel Sambuc#ifndef _LIBCPP_NO_EXCEPTIONS 1794684ddb6SLionel Sambuc if (__ec) 1804684ddb6SLionel Sambuc throw system_error(error_code(__ec, system_category()), 1814684ddb6SLionel Sambuc "__thread_specific_ptr construction failed"); 1824684ddb6SLionel Sambuc#endif 1834684ddb6SLionel Sambuc} 1844684ddb6SLionel Sambuc 1854684ddb6SLionel Sambuctemplate <class _Tp> 1864684ddb6SLionel Sambuc__thread_specific_ptr<_Tp>::~__thread_specific_ptr() 1874684ddb6SLionel Sambuc{ 188*0a6a1f1dSLionel Sambuc // __thread_specific_ptr is only created with a static storage duration 189*0a6a1f1dSLionel Sambuc // so this destructor is only invoked during program termination. Invoking 190*0a6a1f1dSLionel Sambuc // pthread_key_delete(__key_) may prevent other threads from deleting their 191*0a6a1f1dSLionel Sambuc // thread local data. For this reason we leak the key. 1924684ddb6SLionel Sambuc} 1934684ddb6SLionel Sambuc 1944684ddb6SLionel Sambuctemplate <class _Tp> 1954684ddb6SLionel Sambuctypename __thread_specific_ptr<_Tp>::pointer 1964684ddb6SLionel Sambuc__thread_specific_ptr<_Tp>::release() 1974684ddb6SLionel Sambuc{ 1984684ddb6SLionel Sambuc pointer __p = get(); 1994684ddb6SLionel Sambuc pthread_setspecific(__key_, 0); 2004684ddb6SLionel Sambuc return __p; 2014684ddb6SLionel Sambuc} 2024684ddb6SLionel Sambuc 2034684ddb6SLionel Sambuctemplate <class _Tp> 2044684ddb6SLionel Sambucvoid 2054684ddb6SLionel Sambuc__thread_specific_ptr<_Tp>::reset(pointer __p) 2064684ddb6SLionel Sambuc{ 2074684ddb6SLionel Sambuc pointer __p_old = get(); 2084684ddb6SLionel Sambuc pthread_setspecific(__key_, __p); 2094684ddb6SLionel Sambuc delete __p_old; 2104684ddb6SLionel Sambuc} 2114684ddb6SLionel Sambuc 2124684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS thread; 2134684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS __thread_id; 2144684ddb6SLionel Sambuc 2154684ddb6SLionel Sambucnamespace this_thread 2164684ddb6SLionel Sambuc{ 2174684ddb6SLionel Sambuc 2184684ddb6SLionel Sambuc_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; 2194684ddb6SLionel Sambuc 2204684ddb6SLionel Sambuc} // this_thread 2214684ddb6SLionel Sambuc 2224684ddb6SLionel Sambuctemplate<> struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>; 2234684ddb6SLionel Sambuc 2244684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS_ONLY __thread_id 2254684ddb6SLionel Sambuc{ 2264684ddb6SLionel Sambuc // FIXME: pthread_t is a pointer on Darwin but a long on Linux. 2274684ddb6SLionel Sambuc // NULL is the no-thread value on Darwin. Someone needs to check 2284684ddb6SLionel Sambuc // on other platforms. We assume 0 works everywhere for now. 2294684ddb6SLionel Sambuc pthread_t __id_; 2304684ddb6SLionel Sambuc 2314684ddb6SLionel Sambucpublic: 2324684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 2334684ddb6SLionel Sambuc __thread_id() _NOEXCEPT : __id_(0) {} 2344684ddb6SLionel Sambuc 2354684ddb6SLionel Sambuc friend _LIBCPP_INLINE_VISIBILITY 2364684ddb6SLionel Sambuc bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT 2374684ddb6SLionel Sambuc {return __x.__id_ == __y.__id_;} 2384684ddb6SLionel Sambuc friend _LIBCPP_INLINE_VISIBILITY 2394684ddb6SLionel Sambuc bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT 2404684ddb6SLionel Sambuc {return !(__x == __y);} 2414684ddb6SLionel Sambuc friend _LIBCPP_INLINE_VISIBILITY 2424684ddb6SLionel Sambuc bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT 2434684ddb6SLionel Sambuc {return __x.__id_ < __y.__id_;} 2444684ddb6SLionel Sambuc friend _LIBCPP_INLINE_VISIBILITY 2454684ddb6SLionel Sambuc bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT 2464684ddb6SLionel Sambuc {return !(__y < __x);} 2474684ddb6SLionel Sambuc friend _LIBCPP_INLINE_VISIBILITY 2484684ddb6SLionel Sambuc bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT 2494684ddb6SLionel Sambuc {return __y < __x ;} 2504684ddb6SLionel Sambuc friend _LIBCPP_INLINE_VISIBILITY 2514684ddb6SLionel Sambuc bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT 2524684ddb6SLionel Sambuc {return !(__x < __y);} 2534684ddb6SLionel Sambuc 2544684ddb6SLionel Sambuc template<class _CharT, class _Traits> 2554684ddb6SLionel Sambuc friend 2564684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 2574684ddb6SLionel Sambuc basic_ostream<_CharT, _Traits>& 2584684ddb6SLionel Sambuc operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) 2594684ddb6SLionel Sambuc {return __os << __id.__id_;} 2604684ddb6SLionel Sambuc 2614684ddb6SLionel Sambucprivate: 2624684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 2634684ddb6SLionel Sambuc __thread_id(pthread_t __id) : __id_(__id) {} 2644684ddb6SLionel Sambuc 2654684ddb6SLionel Sambuc friend __thread_id this_thread::get_id() _NOEXCEPT; 2664684ddb6SLionel Sambuc friend class _LIBCPP_TYPE_VIS thread; 2674684ddb6SLionel Sambuc friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>; 2684684ddb6SLionel Sambuc}; 2694684ddb6SLionel Sambuc 2704684ddb6SLionel Sambuctemplate<> 2714684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id> 2724684ddb6SLionel Sambuc : public unary_function<__thread_id, size_t> 2734684ddb6SLionel Sambuc{ 2744684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 2754684ddb6SLionel Sambuc size_t operator()(__thread_id __v) const 2764684ddb6SLionel Sambuc { 2774684ddb6SLionel Sambuc return hash<pthread_t>()(__v.__id_); 2784684ddb6SLionel Sambuc } 2794684ddb6SLionel Sambuc}; 2804684ddb6SLionel Sambuc 2814684ddb6SLionel Sambucnamespace this_thread 2824684ddb6SLionel Sambuc{ 2834684ddb6SLionel Sambuc 2844684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 2854684ddb6SLionel Sambuc__thread_id 2864684ddb6SLionel Sambucget_id() _NOEXCEPT 2874684ddb6SLionel Sambuc{ 2884684ddb6SLionel Sambuc return pthread_self(); 2894684ddb6SLionel Sambuc} 2904684ddb6SLionel Sambuc 2914684ddb6SLionel Sambuc} // this_thread 2924684ddb6SLionel Sambuc 2934684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS thread 2944684ddb6SLionel Sambuc{ 2954684ddb6SLionel Sambuc pthread_t __t_; 2964684ddb6SLionel Sambuc 2974684ddb6SLionel Sambuc thread(const thread&); 2984684ddb6SLionel Sambuc thread& operator=(const thread&); 2994684ddb6SLionel Sambucpublic: 3004684ddb6SLionel Sambuc typedef __thread_id id; 3014684ddb6SLionel Sambuc typedef pthread_t native_handle_type; 3024684ddb6SLionel Sambuc 3034684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 3044684ddb6SLionel Sambuc thread() _NOEXCEPT : __t_(0) {} 3054684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 3064684ddb6SLionel Sambuc template <class _Fp, class ..._Args, 3074684ddb6SLionel Sambuc class = typename enable_if 3084684ddb6SLionel Sambuc < 3094684ddb6SLionel Sambuc !is_same<typename decay<_Fp>::type, thread>::value 3104684ddb6SLionel Sambuc >::type 3114684ddb6SLionel Sambuc > 3124684ddb6SLionel Sambuc explicit thread(_Fp&& __f, _Args&&... __args); 3134684ddb6SLionel Sambuc#else // _LIBCPP_HAS_NO_VARIADICS 3144684ddb6SLionel Sambuc template <class _Fp> explicit thread(_Fp __f); 3154684ddb6SLionel Sambuc#endif 3164684ddb6SLionel Sambuc ~thread(); 3174684ddb6SLionel Sambuc 3184684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 3194684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 3204684ddb6SLionel Sambuc thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;} 3214684ddb6SLionel Sambuc thread& operator=(thread&& __t) _NOEXCEPT; 3224684ddb6SLionel Sambuc#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 3234684ddb6SLionel Sambuc 3244684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 3254684ddb6SLionel Sambuc void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} 3264684ddb6SLionel Sambuc 3274684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 3284684ddb6SLionel Sambuc bool joinable() const _NOEXCEPT {return __t_ != 0;} 3294684ddb6SLionel Sambuc void join(); 3304684ddb6SLionel Sambuc void detach(); 3314684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 3324684ddb6SLionel Sambuc id get_id() const _NOEXCEPT {return __t_;} 3334684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 3344684ddb6SLionel Sambuc native_handle_type native_handle() _NOEXCEPT {return __t_;} 3354684ddb6SLionel Sambuc 3364684ddb6SLionel Sambuc static unsigned hardware_concurrency() _NOEXCEPT; 3374684ddb6SLionel Sambuc}; 3384684ddb6SLionel Sambuc 3394684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 3404684ddb6SLionel Sambuc 3414684ddb6SLionel Sambuctemplate <class _Fp, class ..._Args, size_t ..._Indices> 3424684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 3434684ddb6SLionel Sambucvoid 3444684ddb6SLionel Sambuc__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>) 3454684ddb6SLionel Sambuc{ 3464684ddb6SLionel Sambuc __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); 3474684ddb6SLionel Sambuc} 3484684ddb6SLionel Sambuc 3494684ddb6SLionel Sambuctemplate <class _Fp> 3504684ddb6SLionel Sambucvoid* 3514684ddb6SLionel Sambuc__thread_proxy(void* __vp) 3524684ddb6SLionel Sambuc{ 3534684ddb6SLionel Sambuc __thread_local_data().reset(new __thread_struct); 3544684ddb6SLionel Sambuc std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 3554684ddb6SLionel Sambuc typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; 3564684ddb6SLionel Sambuc __thread_execute(*__p, _Index()); 3574684ddb6SLionel Sambuc return nullptr; 3584684ddb6SLionel Sambuc} 3594684ddb6SLionel Sambuc 3604684ddb6SLionel Sambuctemplate <class _Fp, class ..._Args, 3614684ddb6SLionel Sambuc class 3624684ddb6SLionel Sambuc > 3634684ddb6SLionel Sambucthread::thread(_Fp&& __f, _Args&&... __args) 3644684ddb6SLionel Sambuc{ 3654684ddb6SLionel Sambuc typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp; 3664684ddb6SLionel Sambuc _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)), 3674684ddb6SLionel Sambuc __decay_copy(_VSTD::forward<_Args>(__args))...)); 3684684ddb6SLionel Sambuc int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get()); 3694684ddb6SLionel Sambuc if (__ec == 0) 3704684ddb6SLionel Sambuc __p.release(); 3714684ddb6SLionel Sambuc else 3724684ddb6SLionel Sambuc __throw_system_error(__ec, "thread constructor failed"); 3734684ddb6SLionel Sambuc} 3744684ddb6SLionel Sambuc 3754684ddb6SLionel Sambuc#else // _LIBCPP_HAS_NO_VARIADICS 3764684ddb6SLionel Sambuc 3774684ddb6SLionel Sambuctemplate <class _Fp> 3784684ddb6SLionel Sambucvoid* 3794684ddb6SLionel Sambuc__thread_proxy(void* __vp) 3804684ddb6SLionel Sambuc{ 3814684ddb6SLionel Sambuc __thread_local_data().reset(new __thread_struct); 3824684ddb6SLionel Sambuc std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 3834684ddb6SLionel Sambuc (*__p)(); 3844684ddb6SLionel Sambuc return nullptr; 3854684ddb6SLionel Sambuc} 3864684ddb6SLionel Sambuc 3874684ddb6SLionel Sambuctemplate <class _Fp> 3884684ddb6SLionel Sambucthread::thread(_Fp __f) 3894684ddb6SLionel Sambuc{ 3904684ddb6SLionel Sambuc std::unique_ptr<_Fp> __p(new _Fp(__f)); 3914684ddb6SLionel Sambuc int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get()); 3924684ddb6SLionel Sambuc if (__ec == 0) 3934684ddb6SLionel Sambuc __p.release(); 3944684ddb6SLionel Sambuc else 3954684ddb6SLionel Sambuc __throw_system_error(__ec, "thread constructor failed"); 3964684ddb6SLionel Sambuc} 3974684ddb6SLionel Sambuc 3984684ddb6SLionel Sambuc#endif // _LIBCPP_HAS_NO_VARIADICS 3994684ddb6SLionel Sambuc 4004684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 4014684ddb6SLionel Sambuc 4024684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 4034684ddb6SLionel Sambucthread& 4044684ddb6SLionel Sambucthread::operator=(thread&& __t) _NOEXCEPT 4054684ddb6SLionel Sambuc{ 4064684ddb6SLionel Sambuc if (__t_ != 0) 4074684ddb6SLionel Sambuc terminate(); 4084684ddb6SLionel Sambuc __t_ = __t.__t_; 4094684ddb6SLionel Sambuc __t.__t_ = 0; 4104684ddb6SLionel Sambuc return *this; 4114684ddb6SLionel Sambuc} 4124684ddb6SLionel Sambuc 4134684ddb6SLionel Sambuc#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 4144684ddb6SLionel Sambuc 4154684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 4164684ddb6SLionel Sambucvoid swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} 4174684ddb6SLionel Sambuc 4184684ddb6SLionel Sambucnamespace this_thread 4194684ddb6SLionel Sambuc{ 4204684ddb6SLionel Sambuc 4214684ddb6SLionel Sambuc_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns); 4224684ddb6SLionel Sambuc 4234684ddb6SLionel Sambuctemplate <class _Rep, class _Period> 4244684ddb6SLionel Sambucvoid 4254684ddb6SLionel Sambucsleep_for(const chrono::duration<_Rep, _Period>& __d) 4264684ddb6SLionel Sambuc{ 4274684ddb6SLionel Sambuc using namespace chrono; 4284684ddb6SLionel Sambuc if (__d > duration<_Rep, _Period>::zero()) 4294684ddb6SLionel Sambuc { 4304684ddb6SLionel Sambuc _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max(); 4314684ddb6SLionel Sambuc nanoseconds __ns; 4324684ddb6SLionel Sambuc if (__d < _Max) 4334684ddb6SLionel Sambuc { 4344684ddb6SLionel Sambuc __ns = duration_cast<nanoseconds>(__d); 4354684ddb6SLionel Sambuc if (__ns < __d) 4364684ddb6SLionel Sambuc ++__ns; 4374684ddb6SLionel Sambuc } 4384684ddb6SLionel Sambuc else 4394684ddb6SLionel Sambuc __ns = nanoseconds::max(); 4404684ddb6SLionel Sambuc sleep_for(__ns); 4414684ddb6SLionel Sambuc } 4424684ddb6SLionel Sambuc} 4434684ddb6SLionel Sambuc 4444684ddb6SLionel Sambuctemplate <class _Clock, class _Duration> 4454684ddb6SLionel Sambucvoid 4464684ddb6SLionel Sambucsleep_until(const chrono::time_point<_Clock, _Duration>& __t) 4474684ddb6SLionel Sambuc{ 4484684ddb6SLionel Sambuc using namespace chrono; 4494684ddb6SLionel Sambuc mutex __mut; 4504684ddb6SLionel Sambuc condition_variable __cv; 4514684ddb6SLionel Sambuc unique_lock<mutex> __lk(__mut); 4524684ddb6SLionel Sambuc while (_Clock::now() < __t) 4534684ddb6SLionel Sambuc __cv.wait_until(__lk, __t); 4544684ddb6SLionel Sambuc} 4554684ddb6SLionel Sambuc 4564684ddb6SLionel Sambuctemplate <class _Duration> 4574684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 4584684ddb6SLionel Sambucvoid 4594684ddb6SLionel Sambucsleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) 4604684ddb6SLionel Sambuc{ 4614684ddb6SLionel Sambuc using namespace chrono; 4624684ddb6SLionel Sambuc sleep_for(__t - steady_clock::now()); 4634684ddb6SLionel Sambuc} 4644684ddb6SLionel Sambuc 4654684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 4664684ddb6SLionel Sambucvoid yield() _NOEXCEPT {sched_yield();} 4674684ddb6SLionel Sambuc 4684684ddb6SLionel Sambuc} // this_thread 4694684ddb6SLionel Sambuc 4704684ddb6SLionel Sambuc_LIBCPP_END_NAMESPACE_STD 4714684ddb6SLionel Sambuc 472*0a6a1f1dSLionel Sambuc#endif // !_LIBCPP_HAS_NO_THREADS 473*0a6a1f1dSLionel Sambuc 4744684ddb6SLionel Sambuc#endif // _LIBCPP_THREAD 475