// -*- C++ -*- //===--------------------------- thread -----------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef _LIBCPP_THREAD #define _LIBCPP_THREAD /* thread synopsis #define __STDCPP_THREADS__ __cplusplus namespace std { class thread { public: class id; typedef pthread_t native_handle_type; thread(); template explicit thread(F&& f, Args&&... args); ~thread(); thread(const thread&) = delete; thread(thread&& t); thread& operator=(const thread&) = delete; thread& operator=(thread&& t); void swap(thread& t); bool joinable() const; void join(); void detach(); id get_id() const; native_handle_type native_handle(); static unsigned hardware_concurrency(); }; void swap(thread& x, thread& y); class thread::id { public: id(); }; bool operator==(thread::id x, thread::id y); bool operator!=(thread::id x, thread::id y); bool operator< (thread::id x, thread::id y); bool operator<=(thread::id x, thread::id y); bool operator> (thread::id x, thread::id y); bool operator>=(thread::id x, thread::id y); template basic_ostream& operator<<(basic_ostream& out, thread::id id); namespace this_thread { thread::id get_id(); void yield(); template void sleep_until(const chrono::time_point& abs_time); template void sleep_for(const chrono::duration& rel_time); } // this_thread } // std */ #include <__config> #include #include <__functional_base> #include #include #include #include #include #include #include <__mutex_base> #ifndef _LIBCPP_HAS_NO_VARIADICS #include #endif #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header #endif #define __STDCPP_THREADS__ __cplusplus _LIBCPP_BEGIN_NAMESPACE_STD template class __thread_specific_ptr { pthread_key_t __key_; __thread_specific_ptr(const __thread_specific_ptr&); __thread_specific_ptr& operator=(const __thread_specific_ptr&); static void __at_thread_exit(void*); public: typedef _Tp* pointer; __thread_specific_ptr(); ~__thread_specific_ptr(); _LIBCPP_INLINE_VISIBILITY pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));} _LIBCPP_INLINE_VISIBILITY pointer operator*() const {return *get();} _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return get();} pointer release(); void reset(pointer __p = nullptr); }; template void __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) { delete static_cast(__p); } template __thread_specific_ptr<_Tp>::__thread_specific_ptr() { int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit); if (__ec) throw system_error(error_code(__ec, system_category()), "__thread_specific_ptr construction failed"); } template __thread_specific_ptr<_Tp>::~__thread_specific_ptr() { pthread_key_delete(__key_); } template typename __thread_specific_ptr<_Tp>::pointer __thread_specific_ptr<_Tp>::release() { pointer __p = get(); pthread_setspecific(__key_, 0); return __p; } template void __thread_specific_ptr<_Tp>::reset(pointer __p) { pointer __p_old = get(); pthread_setspecific(__key_, __p); delete __p_old; } class thread; class __thread_id; namespace this_thread { __thread_id get_id(); } // this_thread class _LIBCPP_VISIBLE __thread_id { // FIXME: pthread_t is a pointer on Darwin but a long on Linux. // NULL is the no-thread value on Darwin. Someone needs to check // on other platforms. We assume 0 works everywhere for now. pthread_t __id_; public: _LIBCPP_INLINE_VISIBILITY __thread_id() : __id_(0) {} friend _LIBCPP_INLINE_VISIBILITY bool operator==(__thread_id __x, __thread_id __y) {return __x.__id_ == __y.__id_;} friend _LIBCPP_INLINE_VISIBILITY bool operator!=(__thread_id __x, __thread_id __y) {return !(__x == __y);} friend _LIBCPP_INLINE_VISIBILITY bool operator< (__thread_id __x, __thread_id __y) {return __x.__id_ < __y.__id_;} friend _LIBCPP_INLINE_VISIBILITY bool operator<=(__thread_id __x, __thread_id __y) {return !(__y < __x);} friend _LIBCPP_INLINE_VISIBILITY bool operator> (__thread_id __x, __thread_id __y) {return __y < __x ;} friend _LIBCPP_INLINE_VISIBILITY bool operator>=(__thread_id __x, __thread_id __y) {return !(__x < __y);} template friend _LIBCPP_INLINE_VISIBILITY basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) {return __os << __id.__id_;} private: _LIBCPP_INLINE_VISIBILITY __thread_id(pthread_t __id) : __id_(__id) {} friend __thread_id this_thread::get_id(); friend class _LIBCPP_VISIBLE thread; }; template struct hash; template<> struct _LIBCPP_VISIBLE hash<__thread_id> : public unary_function<__thread_id, size_t> { _LIBCPP_INLINE_VISIBILITY size_t operator()(__thread_id __v) const { const size_t* const __p = reinterpret_cast(&__v); return *__p; } }; namespace this_thread { inline _LIBCPP_INLINE_VISIBILITY __thread_id get_id() { return pthread_self(); } } // this_thread class _LIBCPP_VISIBLE thread { pthread_t __t_; thread(const thread&); thread& operator=(const thread&); public: typedef __thread_id id; typedef pthread_t native_handle_type; _LIBCPP_INLINE_VISIBILITY thread() : __t_(0) {} #ifndef _LIBCPP_HAS_NO_VARIADICS template ::type, thread>::value >::type > explicit thread(_Fp&& __f, _Args&&... __args); #else // _LIBCPP_HAS_NO_VARIADICS template explicit thread(_Fp __f); #endif ~thread(); #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES _LIBCPP_INLINE_VISIBILITY thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;} thread& operator=(thread&& __t); #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES _LIBCPP_INLINE_VISIBILITY void swap(thread& __t) {_VSTD::swap(__t_, __t.__t_);} _LIBCPP_INLINE_VISIBILITY bool joinable() const {return __t_ != 0;} void join(); void detach(); _LIBCPP_INLINE_VISIBILITY id get_id() const {return __t_;} _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return __t_;} static unsigned hardware_concurrency(); }; class __assoc_sub_state; class _LIBCPP_HIDDEN __thread_struct_imp; class __thread_struct { __thread_struct_imp* __p_; __thread_struct(const __thread_struct&); __thread_struct& operator=(const __thread_struct&); public: __thread_struct(); ~__thread_struct(); void notify_all_at_thread_exit(condition_variable*, mutex*); void __make_ready_at_thread_exit(__assoc_sub_state*); }; __thread_specific_ptr<__thread_struct>& __thread_local_data(); #ifndef _LIBCPP_HAS_NO_VARIADICS template inline _LIBCPP_INLINE_VISIBILITY void __threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>) { __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); } template void* __thread_proxy(void* __vp) { __thread_local_data().reset(new __thread_struct); std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); typedef typename __make_tuple_indices::value, 1>::type _Index; __threaad_execute(*__p, _Index()); return nullptr; } template thread::thread(_Fp&& __f, _Args&&... __args) { typedef tuple::type, typename decay<_Args>::type...> _Gp; _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)), __decay_copy(_VSTD::forward<_Args>(__args))...)); int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get()); if (__ec == 0) __p.release(); else __throw_system_error(__ec, "thread constructor failed"); } #else // _LIBCPP_HAS_NO_VARIADICS template void* __thread_proxy(void* __vp) { __thread_local_data().reset(new __thread_struct); std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); (*__p)(); return nullptr; } template thread::thread(_Fp __f) { std::unique_ptr<_Fp> __p(new _Fp(__f)); int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get()); if (__ec == 0) __p.release(); else __throw_system_error(__ec, "thread constructor failed"); } #endif // _LIBCPP_HAS_NO_VARIADICS #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES inline _LIBCPP_INLINE_VISIBILITY thread& thread::operator=(thread&& __t) { if (__t_ != 0) terminate(); __t_ = __t.__t_; __t.__t_ = 0; return *this; } #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES inline _LIBCPP_INLINE_VISIBILITY void swap(thread& __x, thread& __y) {__x.swap(__y);} namespace this_thread { void sleep_for(const chrono::nanoseconds& ns); template void sleep_for(const chrono::duration<_Rep, _Period>& __d) { using namespace chrono; nanoseconds __ns = duration_cast(__d); if (__ns < __d) ++__ns; sleep_for(__ns); } template void sleep_until(const chrono::time_point<_Clock, _Duration>& __t) { using namespace chrono; mutex __mut; condition_variable __cv; unique_lock __lk(__mut); while (_Clock::now() < __t) __cv.wait_until(__lk, __t); } template inline _LIBCPP_INLINE_VISIBILITY void sleep_until(const chrono::time_point& __t) { using namespace chrono; sleep_for(__t - steady_clock::now()); } inline _LIBCPP_INLINE_VISIBILITY void yield() {sched_yield();} } // this_thread _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_THREAD