Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- thread -----------------------------------===// |
| 3 | // |
Howard Hinnant | c566dc3 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | ee11c31 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_THREAD |
| 12 | #define _LIBCPP_THREAD |
| 13 | |
| 14 | /* |
| 15 | |
| 16 | thread synopsis |
| 17 | |
Howard Hinnant | 29aba23 | 2010-08-21 21:01:59 +0000 | [diff] [blame] | 18 | #define __STDCPP_THREADS__ __cplusplus |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | |
| 20 | namespace std |
| 21 | { |
| 22 | |
| 23 | class thread |
| 24 | { |
| 25 | public: |
| 26 | class id; |
| 27 | typedef pthread_t native_handle_type; |
| 28 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 29 | thread() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | template <class F, class ...Args> explicit thread(F&& f, Args&&... args); |
| 31 | ~thread(); |
| 32 | |
| 33 | thread(const thread&) = delete; |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 34 | thread(thread&& t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 35 | |
| 36 | thread& operator=(const thread&) = delete; |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 37 | thread& operator=(thread&& t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 39 | void swap(thread& t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 41 | bool joinable() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | void join(); |
| 43 | void detach(); |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 44 | id get_id() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | native_handle_type native_handle(); |
| 46 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 47 | static unsigned hardware_concurrency() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 50 | void swap(thread& x, thread& y) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 51 | |
| 52 | class thread::id |
| 53 | { |
| 54 | public: |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 55 | id() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 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; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 72 | thread::id get_id() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 73 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 74 | void yield() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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> |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 98 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 99 | #include <tuple> |
| 100 | #endif |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 101 | #include <__threading_support> |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 102 | #include <__debug> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 103 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 104 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 105 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 106 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | |
Howard Hinnant | 29aba23 | 2010-08-21 21:01:59 +0000 | [diff] [blame] | 108 | #define __STDCPP_THREADS__ __cplusplus |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 109 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 110 | #ifdef _LIBCPP_HAS_NO_THREADS |
| 111 | #error <thread> is not supported on this single threaded system |
| 112 | #else // !_LIBCPP_HAS_NO_THREADS |
| 113 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 115 | |
Eric Fiselier | 80b0e15 | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 116 | template <class _Tp> class __thread_specific_ptr; |
| 117 | class _LIBCPP_TYPE_VIS __thread_struct; |
| 118 | class _LIBCPP_HIDDEN __thread_struct_imp; |
| 119 | class __assoc_sub_state; |
| 120 | |
| 121 | _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); |
| 122 | |
| 123 | class _LIBCPP_TYPE_VIS __thread_struct |
| 124 | { |
| 125 | __thread_struct_imp* __p_; |
| 126 | |
| 127 | __thread_struct(const __thread_struct&); |
| 128 | __thread_struct& operator=(const __thread_struct&); |
| 129 | public: |
| 130 | __thread_struct(); |
| 131 | ~__thread_struct(); |
| 132 | |
| 133 | void notify_all_at_thread_exit(condition_variable*, mutex*); |
| 134 | void __make_ready_at_thread_exit(__assoc_sub_state*); |
| 135 | }; |
| 136 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 137 | template <class _Tp> |
| 138 | class __thread_specific_ptr |
| 139 | { |
Asiri Rathnayake | 94340d2 | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 140 | __libcpp_tls_key __key_; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 141 | |
Eric Fiselier | 80b0e15 | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 142 | // Only __thread_local_data() may construct a __thread_specific_ptr |
| 143 | // and only with _Tp == __thread_struct. |
Eric Fiselier | f34995b | 2015-08-19 03:38:41 +0000 | [diff] [blame] | 144 | static_assert((is_same<_Tp, __thread_struct>::value), ""); |
Eric Fiselier | 80b0e15 | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 145 | __thread_specific_ptr(); |
| 146 | friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); |
| 147 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 148 | __thread_specific_ptr(const __thread_specific_ptr&); |
| 149 | __thread_specific_ptr& operator=(const __thread_specific_ptr&); |
| 150 | |
Saleem Abdulrasool | b75a574 | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 151 | static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); |
| 152 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 153 | public: |
| 154 | typedef _Tp* pointer; |
| 155 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 156 | ~__thread_specific_ptr(); |
| 157 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 158 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | 94340d2 | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 159 | pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 160 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 161 | pointer operator*() const {return *get();} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 162 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 163 | pointer operator->() const {return get();} |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 164 | void set_pointer(pointer __p); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | template <class _Tp> |
Saleem Abdulrasool | b75a574 | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 168 | void _LIBCPP_TLS_DESTRUCTOR_CC |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 169 | __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) |
| 170 | { |
Howard Hinnant | 0eef25b | 2011-10-17 20:08:59 +0000 | [diff] [blame] | 171 | delete static_cast<pointer>(__p); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | template <class _Tp> |
| 175 | __thread_specific_ptr<_Tp>::__thread_specific_ptr() |
| 176 | { |
Saleem Abdulrasool | b75a574 | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 177 | int __ec = |
| 178 | __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit); |
| 179 | if (__ec) |
| 180 | __throw_system_error(__ec, "__thread_specific_ptr construction failed"); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | template <class _Tp> |
| 184 | __thread_specific_ptr<_Tp>::~__thread_specific_ptr() |
| 185 | { |
Eric Fiselier | 80b0e15 | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 186 | // __thread_specific_ptr is only created with a static storage duration |
| 187 | // so this destructor is only invoked during program termination. Invoking |
| 188 | // pthread_key_delete(__key_) may prevent other threads from deleting their |
| 189 | // thread local data. For this reason we leak the key. |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | template <class _Tp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 193 | void |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 194 | __thread_specific_ptr<_Tp>::set_pointer(pointer __p) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 195 | { |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 196 | _LIBCPP_ASSERT(get() == nullptr, |
| 197 | "Attempting to overwrite thread local data"); |
Asiri Rathnayake | 94340d2 | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 198 | __libcpp_tls_set(__key_, __p); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 201 | class _LIBCPP_TYPE_VIS thread; |
| 202 | class _LIBCPP_TYPE_VIS __thread_id; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 203 | |
| 204 | namespace this_thread |
| 205 | { |
| 206 | |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 207 | _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 208 | |
| 209 | } // this_thread |
| 210 | |
Eric Fiselier | 6585c75 | 2016-04-21 22:54:21 +0000 | [diff] [blame] | 211 | template<> struct hash<__thread_id>; |
Howard Hinnant | ad71c23 | 2011-12-05 00:08:45 +0000 | [diff] [blame] | 212 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 213 | class _LIBCPP_TEMPLATE_VIS __thread_id |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | { |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 215 | // FIXME: pthread_t is a pointer on Darwin but a long on Linux. |
| 216 | // NULL is the no-thread value on Darwin. Someone needs to check |
| 217 | // on other platforms. We assume 0 works everywhere for now. |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 218 | __libcpp_thread_id __id_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | |
| 220 | public: |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 221 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 222 | __thread_id() _NOEXCEPT : __id_(0) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 223 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 224 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 225 | bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 226 | {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 227 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 228 | bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | {return !(__x == __y);} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 230 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 231 | bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 232 | {return __libcpp_thread_id_less(__x.__id_, __y.__id_);} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 233 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 234 | bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | {return !(__y < __x);} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 236 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 237 | bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 238 | {return __y < __x ;} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 239 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 240 | bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 241 | {return !(__x < __y);} |
| 242 | |
| 243 | template<class _CharT, class _Traits> |
| 244 | friend |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 245 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 246 | basic_ostream<_CharT, _Traits>& |
| 247 | operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) |
| 248 | {return __os << __id.__id_;} |
| 249 | |
| 250 | private: |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 251 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 252 | __thread_id(__libcpp_thread_id __id) : __id_(__id) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 253 | |
Howard Hinnant | d48ef42 | 2012-08-19 15:13:16 +0000 | [diff] [blame] | 254 | friend __thread_id this_thread::get_id() _NOEXCEPT; |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 255 | friend class _LIBCPP_TYPE_VIS thread; |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 256 | friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 259 | template<> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 260 | struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 261 | : public unary_function<__thread_id, size_t> |
| 262 | { |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 263 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 264 | size_t operator()(__thread_id __v) const |
| 265 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 266 | return hash<__libcpp_thread_id>()(__v.__id_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 267 | } |
| 268 | }; |
| 269 | |
| 270 | namespace this_thread |
| 271 | { |
| 272 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 273 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 274 | __thread_id |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 275 | get_id() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 276 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 277 | return __libcpp_thread_get_current_id(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | } // this_thread |
| 281 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 282 | class _LIBCPP_TYPE_VIS thread |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 283 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 284 | __libcpp_thread_t __t_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 285 | |
Howard Hinnant | aa87c0f | 2010-08-10 20:48:29 +0000 | [diff] [blame] | 286 | thread(const thread&); |
| 287 | thread& operator=(const thread&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 288 | public: |
| 289 | typedef __thread_id id; |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 290 | typedef __libcpp_thread_t native_handle_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 291 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 292 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | 3bab36f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 293 | thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 294 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 295 | template <class _Fp, class ..._Args, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 296 | class = typename enable_if |
| 297 | < |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 298 | !is_same<typename decay<_Fp>::type, thread>::value |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 299 | >::type |
| 300 | > |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 301 | explicit thread(_Fp&& __f, _Args&&... __args); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 302 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 303 | template <class _Fp> explicit thread(_Fp __f); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 304 | #endif |
| 305 | ~thread(); |
| 306 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 307 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 308 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | 3bab36f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 309 | thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 310 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 311 | thread& operator=(thread&& __t) _NOEXCEPT; |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 312 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 313 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 314 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 315 | void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 316 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 317 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | 3bab36f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 318 | bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 319 | void join(); |
| 320 | void detach(); |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 321 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 322 | id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 323 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 324 | native_handle_type native_handle() _NOEXCEPT {return __t_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 325 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 326 | static unsigned hardware_concurrency() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 329 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 330 | |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 331 | template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices> |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 332 | inline _LIBCPP_INLINE_VISIBILITY |
| 333 | void |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 334 | __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 335 | { |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 336 | __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 339 | template <class _Fp> |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 340 | void* __thread_proxy(void* __vp) |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 341 | { |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 342 | // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...> |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 343 | std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 344 | __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release()); |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 345 | typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; |
Howard Hinnant | 3462e1e | 2013-04-03 20:29:45 +0000 | [diff] [blame] | 346 | __thread_execute(*__p, _Index()); |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 347 | return nullptr; |
| 348 | } |
| 349 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 350 | template <class _Fp, class ..._Args, |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 351 | class |
| 352 | > |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 353 | thread::thread(_Fp&& __f, _Args&&... __args) |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 354 | { |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 355 | typedef unique_ptr<__thread_struct> _TSPtr; |
| 356 | _TSPtr __tsp(new __thread_struct); |
| 357 | typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp; |
| 358 | _VSTD::unique_ptr<_Gp> __p( |
| 359 | new _Gp(std::move(__tsp), |
| 360 | __decay_copy(_VSTD::forward<_Fp>(__f)), |
| 361 | __decay_copy(_VSTD::forward<_Args>(__args))...)); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 362 | int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 363 | if (__ec == 0) |
| 364 | __p.release(); |
| 365 | else |
| 366 | __throw_system_error(__ec, "thread constructor failed"); |
| 367 | } |
| 368 | |
| 369 | #else // _LIBCPP_HAS_NO_VARIADICS |
| 370 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 371 | template <class _Fp> |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 372 | struct __thread_invoke_pair { |
| 373 | // This type is used to pass memory for thread local storage and a functor |
| 374 | // to a newly created thread because std::pair doesn't work with |
| 375 | // std::unique_ptr in C++03. |
| 376 | __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} |
| 377 | unique_ptr<__thread_struct> __tsp_; |
| 378 | _Fp __fn_; |
| 379 | }; |
| 380 | |
| 381 | template <class _Fp> |
| 382 | void* __thread_proxy_cxx03(void* __vp) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 384 | std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 385 | __thread_local_data().set_pointer(__p->__tsp_.release()); |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 386 | (__p->__fn_)(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 387 | return nullptr; |
| 388 | } |
| 389 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 390 | template <class _Fp> |
| 391 | thread::thread(_Fp __f) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 392 | { |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 393 | |
| 394 | typedef __thread_invoke_pair<_Fp> _InvokePair; |
| 395 | typedef std::unique_ptr<_InvokePair> _PairPtr; |
| 396 | _PairPtr __pp(new _InvokePair(__f)); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 397 | int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 398 | if (__ec == 0) |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 399 | __pp.release(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 400 | else |
| 401 | __throw_system_error(__ec, "thread constructor failed"); |
| 402 | } |
| 403 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 404 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 405 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 406 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 407 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 408 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 409 | thread& |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 410 | thread::operator=(thread&& __t) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 411 | { |
Asiri Rathnayake | 3bab36f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 412 | if (!__libcpp_thread_isnull(&__t_)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 413 | terminate(); |
| 414 | __t_ = __t.__t_; |
Asiri Rathnayake | 3bab36f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 415 | __t.__t_ = _LIBCPP_NULL_THREAD; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 416 | return *this; |
| 417 | } |
| 418 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 419 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 420 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 421 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 422 | void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 423 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 424 | namespace this_thread |
| 425 | { |
| 426 | |
Joerg Sonnenberger | 917a30a | 2017-02-09 14:12:29 +0000 | [diff] [blame^] | 427 | _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 428 | |
| 429 | template <class _Rep, class _Period> |
| 430 | void |
| 431 | sleep_for(const chrono::duration<_Rep, _Period>& __d) |
| 432 | { |
| 433 | using namespace chrono; |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 434 | if (__d > duration<_Rep, _Period>::zero()) |
| 435 | { |
| 436 | _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max(); |
| 437 | nanoseconds __ns; |
| 438 | if (__d < _Max) |
| 439 | { |
| 440 | __ns = duration_cast<nanoseconds>(__d); |
| 441 | if (__ns < __d) |
| 442 | ++__ns; |
| 443 | } |
| 444 | else |
| 445 | __ns = nanoseconds::max(); |
| 446 | sleep_for(__ns); |
| 447 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | template <class _Clock, class _Duration> |
| 451 | void |
| 452 | sleep_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 453 | { |
| 454 | using namespace chrono; |
| 455 | mutex __mut; |
| 456 | condition_variable __cv; |
| 457 | unique_lock<mutex> __lk(__mut); |
| 458 | while (_Clock::now() < __t) |
| 459 | __cv.wait_until(__lk, __t); |
| 460 | } |
| 461 | |
| 462 | template <class _Duration> |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 463 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 464 | void |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 465 | sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 466 | { |
| 467 | using namespace chrono; |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 468 | sleep_for(__t - steady_clock::now()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 471 | inline _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 472 | void yield() _NOEXCEPT {__libcpp_thread_yield();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 473 | |
| 474 | } // this_thread |
| 475 | |
| 476 | _LIBCPP_END_NAMESPACE_STD |
| 477 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 478 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 479 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 480 | #endif // _LIBCPP_THREAD |