Home | History | Annotate | Download | only in include
      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 
     20 namespace std
     21 {
     22 
     23 class thread
     24 {
     25 public:
     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 
     50 void swap(thread& x, thread& y) noexcept;
     51 
     52 class thread::id
     53 {
     54 public:
     55     id() noexcept;
     56 };
     57 
     58 bool operator==(thread::id x, thread::id y) noexcept;
     59 bool operator!=(thread::id x, thread::id y) noexcept;
     60 bool operator< (thread::id x, thread::id y) noexcept;
     61 bool operator<=(thread::id x, thread::id y) noexcept;
     62 bool operator> (thread::id x, thread::id y) noexcept;
     63 bool operator>=(thread::id x, thread::id y) noexcept;
     64 
     65 template<class charT, class traits>
     66 basic_ostream<charT, traits>&
     67 operator<<(basic_ostream<charT, traits>& out, thread::id id);
     68 
     69 namespace this_thread
     70 {
     71 
     72 thread::id get_id() noexcept;
     73 
     74 void yield() noexcept;
     75 
     76 template <class Clock, class Duration>
     77 void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
     78 
     79 template <class Rep, class Period>
     80 void 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_CXX03_LANG
     99 #include <tuple>
    100 #endif
    101 #include <__threading_support>
    102 #include <__debug>
    103 
    104 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
    105 #pragma GCC system_header
    106 #endif
    107 
    108 _LIBCPP_PUSH_MACROS
    109 #include <__undef_macros>
    110 
    111 #define __STDCPP_THREADS__ __cplusplus
    112 
    113 #ifdef _LIBCPP_HAS_NO_THREADS
    114 #error <thread> is not supported on this single threaded system
    115 #else // !_LIBCPP_HAS_NO_THREADS
    116 
    117 _LIBCPP_BEGIN_NAMESPACE_STD
    118 
    119 template <class _Tp> class __thread_specific_ptr;
    120 class _LIBCPP_TYPE_VIS __thread_struct;
    121 class _LIBCPP_HIDDEN __thread_struct_imp;
    122 class __assoc_sub_state;
    123 
    124 _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
    125 
    126 class _LIBCPP_TYPE_VIS __thread_struct
    127 {
    128     __thread_struct_imp* __p_;
    129 
    130     __thread_struct(const __thread_struct&);
    131     __thread_struct& operator=(const __thread_struct&);
    132 public:
    133     __thread_struct();
    134     ~__thread_struct();
    135 
    136     void notify_all_at_thread_exit(condition_variable*, mutex*);
    137     void __make_ready_at_thread_exit(__assoc_sub_state*);
    138 };
    139 
    140 template <class _Tp>
    141 class __thread_specific_ptr
    142 {
    143     __libcpp_tls_key __key_;
    144 
    145      // Only __thread_local_data() may construct a __thread_specific_ptr
    146      // and only with _Tp == __thread_struct.
    147     static_assert((is_same<_Tp, __thread_struct>::value), "");
    148     __thread_specific_ptr();
    149     friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
    150 
    151     __thread_specific_ptr(const __thread_specific_ptr&);
    152     __thread_specific_ptr& operator=(const __thread_specific_ptr&);
    153 
    154     static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
    155 
    156 public:
    157     typedef _Tp* pointer;
    158 
    159     ~__thread_specific_ptr();
    160 
    161     _LIBCPP_INLINE_VISIBILITY
    162     pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
    163     _LIBCPP_INLINE_VISIBILITY
    164     pointer operator*() const {return *get();}
    165     _LIBCPP_INLINE_VISIBILITY
    166     pointer operator->() const {return get();}
    167     void set_pointer(pointer __p);
    168 };
    169 
    170 template <class _Tp>
    171 void _LIBCPP_TLS_DESTRUCTOR_CC
    172 __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
    173 {
    174     delete static_cast<pointer>(__p);
    175 }
    176 
    177 template <class _Tp>
    178 __thread_specific_ptr<_Tp>::__thread_specific_ptr()
    179 {
    180   int __ec =
    181       __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
    182   if (__ec)
    183     __throw_system_error(__ec, "__thread_specific_ptr construction failed");
    184 }
    185 
    186 template <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 
    195 template <class _Tp>
    196 void
    197 __thread_specific_ptr<_Tp>::set_pointer(pointer __p)
    198 {
    199     _LIBCPP_ASSERT(get() == nullptr,
    200                    "Attempting to overwrite thread local data");
    201     __libcpp_tls_set(__key_, __p);
    202 }
    203 
    204 class _LIBCPP_TYPE_VIS thread;
    205 class _LIBCPP_TYPE_VIS __thread_id;
    206 
    207 namespace this_thread
    208 {
    209 
    210 _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
    211 
    212 }  // this_thread
    213 
    214 template<> struct hash<__thread_id>;
    215 
    216 class _LIBCPP_TEMPLATE_VIS __thread_id
    217 {
    218     // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
    219     // NULL is the no-thread value on Darwin.  Someone needs to check
    220     // on other platforms.  We assume 0 works everywhere for now.
    221     __libcpp_thread_id __id_;
    222 
    223 public:
    224     _LIBCPP_INLINE_VISIBILITY
    225     __thread_id() _NOEXCEPT : __id_(0) {}
    226 
    227     friend _LIBCPP_INLINE_VISIBILITY
    228         bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
    229         {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
    230     friend _LIBCPP_INLINE_VISIBILITY
    231         bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
    232         {return !(__x == __y);}
    233     friend _LIBCPP_INLINE_VISIBILITY
    234         bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
    235         {return  __libcpp_thread_id_less(__x.__id_, __y.__id_);}
    236     friend _LIBCPP_INLINE_VISIBILITY
    237         bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
    238         {return !(__y < __x);}
    239     friend _LIBCPP_INLINE_VISIBILITY
    240         bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
    241         {return   __y < __x ;}
    242     friend _LIBCPP_INLINE_VISIBILITY
    243         bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
    244         {return !(__x < __y);}
    245 
    246     template<class _CharT, class _Traits>
    247     friend
    248     _LIBCPP_INLINE_VISIBILITY
    249     basic_ostream<_CharT, _Traits>&
    250     operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
    251         {return __os << __id.__id_;}
    252 
    253 private:
    254     _LIBCPP_INLINE_VISIBILITY
    255     __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
    256 
    257     friend __thread_id this_thread::get_id() _NOEXCEPT;
    258     friend class _LIBCPP_TYPE_VIS thread;
    259     friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
    260 };
    261 
    262 template<>
    263 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
    264     : public unary_function<__thread_id, size_t>
    265 {
    266     _LIBCPP_INLINE_VISIBILITY
    267     size_t operator()(__thread_id __v) const _NOEXCEPT
    268     {
    269         return hash<__libcpp_thread_id>()(__v.__id_);
    270     }
    271 };
    272 
    273 namespace this_thread
    274 {
    275 
    276 inline _LIBCPP_INLINE_VISIBILITY
    277 __thread_id
    278 get_id() _NOEXCEPT
    279 {
    280     return __libcpp_thread_get_current_id();
    281 }
    282 
    283 }  // this_thread
    284 
    285 class _LIBCPP_TYPE_VIS thread
    286 {
    287     __libcpp_thread_t __t_;
    288 
    289     thread(const thread&);
    290     thread& operator=(const thread&);
    291 public:
    292     typedef __thread_id id;
    293     typedef __libcpp_thread_t native_handle_type;
    294 
    295     _LIBCPP_INLINE_VISIBILITY
    296     thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
    297 #ifndef _LIBCPP_CXX03_LANG
    298     template <class _Fp, class ..._Args,
    299               class = typename enable_if
    300               <
    301                    !is_same<typename decay<_Fp>::type, thread>::value
    302               >::type
    303              >
    304         _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
    305         explicit thread(_Fp&& __f, _Args&&... __args);
    306 #else  // _LIBCPP_CXX03_LANG
    307     template <class _Fp>
    308     _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
    309     explicit thread(_Fp __f);
    310 #endif
    311     ~thread();
    312 
    313 #ifndef _LIBCPP_CXX03_LANG
    314     _LIBCPP_INLINE_VISIBILITY
    315     thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
    316     _LIBCPP_INLINE_VISIBILITY
    317     thread& operator=(thread&& __t) _NOEXCEPT;
    318 #endif  // _LIBCPP_CXX03_LANG
    319 
    320     _LIBCPP_INLINE_VISIBILITY
    321     void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
    322 
    323     _LIBCPP_INLINE_VISIBILITY
    324     bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
    325     void join();
    326     void detach();
    327     _LIBCPP_INLINE_VISIBILITY
    328     id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
    329     _LIBCPP_INLINE_VISIBILITY
    330     native_handle_type native_handle() _NOEXCEPT {return __t_;}
    331 
    332     static unsigned hardware_concurrency() _NOEXCEPT;
    333 };
    334 
    335 #ifndef _LIBCPP_CXX03_LANG
    336 
    337 template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
    338 inline _LIBCPP_INLINE_VISIBILITY
    339 void
    340 __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
    341 {
    342     __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    343 }
    344 
    345 template <class _Fp>
    346 void* __thread_proxy(void* __vp)
    347 {
    348     // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
    349     std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
    350     __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
    351     typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
    352     __thread_execute(*__p, _Index());
    353     return nullptr;
    354 }
    355 
    356 template <class _Fp, class ..._Args,
    357           class
    358          >
    359 thread::thread(_Fp&& __f, _Args&&... __args)
    360 {
    361     typedef unique_ptr<__thread_struct> _TSPtr;
    362     _TSPtr __tsp(new __thread_struct);
    363     typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
    364     _VSTD::unique_ptr<_Gp> __p(
    365             new _Gp(std::move(__tsp),
    366                     __decay_copy(_VSTD::forward<_Fp>(__f)),
    367                     __decay_copy(_VSTD::forward<_Args>(__args))...));
    368     int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
    369     if (__ec == 0)
    370         __p.release();
    371     else
    372         __throw_system_error(__ec, "thread constructor failed");
    373 }
    374 
    375 inline
    376 thread&
    377 thread::operator=(thread&& __t) _NOEXCEPT
    378 {
    379     if (!__libcpp_thread_isnull(&__t_))
    380         terminate();
    381     __t_ = __t.__t_;
    382     __t.__t_ = _LIBCPP_NULL_THREAD;
    383     return *this;
    384 }
    385 
    386 #else  // _LIBCPP_CXX03_LANG
    387 
    388 template <class _Fp>
    389 struct __thread_invoke_pair {
    390     // This type is used to pass memory for thread local storage and a functor
    391     // to a newly created thread because std::pair doesn't work with
    392     // std::unique_ptr in C++03.
    393     __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
    394     unique_ptr<__thread_struct> __tsp_;
    395     _Fp __fn_;
    396 };
    397 
    398 template <class _Fp>
    399 void* __thread_proxy_cxx03(void* __vp)
    400 {
    401     std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
    402     __thread_local_data().set_pointer(__p->__tsp_.release());
    403     (__p->__fn_)();
    404     return nullptr;
    405 }
    406 
    407 template <class _Fp>
    408 thread::thread(_Fp __f)
    409 {
    410 
    411     typedef __thread_invoke_pair<_Fp> _InvokePair;
    412     typedef std::unique_ptr<_InvokePair> _PairPtr;
    413     _PairPtr __pp(new _InvokePair(__f));
    414     int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
    415     if (__ec == 0)
    416         __pp.release();
    417     else
    418         __throw_system_error(__ec, "thread constructor failed");
    419 }
    420 
    421 #endif  // _LIBCPP_CXX03_LANG
    422 
    423 inline _LIBCPP_INLINE_VISIBILITY
    424 void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
    425 
    426 namespace this_thread
    427 {
    428 
    429 _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
    430 
    431 template <class _Rep, class _Period>
    432 void
    433 sleep_for(const chrono::duration<_Rep, _Period>& __d)
    434 {
    435     using namespace chrono;
    436     if (__d > duration<_Rep, _Period>::zero())
    437     {
    438         _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
    439         nanoseconds __ns;
    440         if (__d < _Max)
    441         {
    442             __ns = duration_cast<nanoseconds>(__d);
    443             if (__ns < __d)
    444                 ++__ns;
    445         }
    446         else
    447             __ns = nanoseconds::max();
    448         sleep_for(__ns);
    449     }
    450 }
    451 
    452 template <class _Clock, class _Duration>
    453 void
    454 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
    455 {
    456     using namespace chrono;
    457     mutex __mut;
    458     condition_variable __cv;
    459     unique_lock<mutex> __lk(__mut);
    460     while (_Clock::now() < __t)
    461         __cv.wait_until(__lk, __t);
    462 }
    463 
    464 template <class _Duration>
    465 inline _LIBCPP_INLINE_VISIBILITY
    466 void
    467 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
    468 {
    469     using namespace chrono;
    470     sleep_for(__t - steady_clock::now());
    471 }
    472 
    473 inline _LIBCPP_INLINE_VISIBILITY
    474 void yield() _NOEXCEPT {__libcpp_thread_yield();}
    475 
    476 }  // this_thread
    477 
    478 _LIBCPP_END_NAMESPACE_STD
    479 
    480 #endif // !_LIBCPP_HAS_NO_THREADS
    481 
    482 _LIBCPP_POP_MACROS
    483 
    484 #endif  // _LIBCPP_THREAD
    485