Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- thread -----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_THREAD |
| 11 | #define _LIBCPP_THREAD |
| 12 | |
| 13 | /* |
| 14 | |
| 15 | thread synopsis |
| 16 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | class thread |
| 21 | { |
| 22 | public: |
| 23 | class id; |
| 24 | typedef pthread_t native_handle_type; |
| 25 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 26 | thread() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | template <class F, class ...Args> explicit thread(F&& f, Args&&... args); |
| 28 | ~thread(); |
| 29 | |
| 30 | thread(const thread&) = delete; |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 31 | thread(thread&& t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 32 | |
| 33 | thread& operator=(const thread&) = delete; |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 34 | thread& operator=(thread&& t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 35 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 36 | void swap(thread& t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 38 | bool joinable() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | void join(); |
| 40 | void detach(); |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 41 | id get_id() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | native_handle_type native_handle(); |
| 43 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 44 | static unsigned hardware_concurrency() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 47 | void swap(thread& x, thread& y) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | |
| 49 | class thread::id |
| 50 | { |
| 51 | public: |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 52 | id() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 55 | bool operator==(thread::id x, thread::id y) noexcept; |
| 56 | bool operator!=(thread::id x, thread::id y) noexcept; |
| 57 | bool operator< (thread::id x, thread::id y) noexcept; |
| 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; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 61 | |
| 62 | template<class charT, class traits> |
| 63 | basic_ostream<charT, traits>& |
| 64 | operator<<(basic_ostream<charT, traits>& out, thread::id id); |
| 65 | |
| 66 | namespace this_thread |
| 67 | { |
| 68 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 69 | thread::id get_id() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 71 | void yield() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | |
| 73 | template <class Clock, class Duration> |
| 74 | void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 75 | |
| 76 | template <class Rep, class Period> |
| 77 | void sleep_for(const chrono::duration<Rep, Period>& rel_time); |
| 78 | |
| 79 | } // this_thread |
| 80 | |
| 81 | } // std |
| 82 | |
| 83 | */ |
| 84 | |
| 85 | #include <__config> |
| 86 | #include <iosfwd> |
| 87 | #include <__functional_base> |
| 88 | #include <type_traits> |
| 89 | #include <cstddef> |
| 90 | #include <functional> |
| 91 | #include <memory> |
| 92 | #include <system_error> |
| 93 | #include <chrono> |
| 94 | #include <__mutex_base> |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 95 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 96 | #include <tuple> |
| 97 | #endif |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 98 | #include <__threading_support> |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 99 | #include <__debug> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 100 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 101 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 102 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 103 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 104 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 105 | _LIBCPP_PUSH_MACROS |
| 106 | #include <__undef_macros> |
| 107 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 108 | #ifdef _LIBCPP_HAS_NO_THREADS |
| 109 | #error <thread> is not supported on this single threaded system |
| 110 | #else // !_LIBCPP_HAS_NO_THREADS |
| 111 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 113 | |
Eric Fiselier | 80b0e15 | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 114 | template <class _Tp> class __thread_specific_ptr; |
| 115 | class _LIBCPP_TYPE_VIS __thread_struct; |
| 116 | class _LIBCPP_HIDDEN __thread_struct_imp; |
| 117 | class __assoc_sub_state; |
| 118 | |
| 119 | _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); |
| 120 | |
| 121 | class _LIBCPP_TYPE_VIS __thread_struct |
| 122 | { |
| 123 | __thread_struct_imp* __p_; |
| 124 | |
| 125 | __thread_struct(const __thread_struct&); |
| 126 | __thread_struct& operator=(const __thread_struct&); |
| 127 | public: |
| 128 | __thread_struct(); |
| 129 | ~__thread_struct(); |
| 130 | |
| 131 | void notify_all_at_thread_exit(condition_variable*, mutex*); |
| 132 | void __make_ready_at_thread_exit(__assoc_sub_state*); |
| 133 | }; |
| 134 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 135 | template <class _Tp> |
| 136 | class __thread_specific_ptr |
| 137 | { |
Asiri Rathnayake | 94340d2 | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 138 | __libcpp_tls_key __key_; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 139 | |
Eric Fiselier | 80b0e15 | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 140 | // Only __thread_local_data() may construct a __thread_specific_ptr |
| 141 | // and only with _Tp == __thread_struct. |
Eric Fiselier | f34995b | 2015-08-19 03:38:41 +0000 | [diff] [blame] | 142 | static_assert((is_same<_Tp, __thread_struct>::value), ""); |
Eric Fiselier | 80b0e15 | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 143 | __thread_specific_ptr(); |
| 144 | friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); |
| 145 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 146 | __thread_specific_ptr(const __thread_specific_ptr&); |
| 147 | __thread_specific_ptr& operator=(const __thread_specific_ptr&); |
| 148 | |
Louis Dionne | 5254b37 | 2018-10-25 12:13:43 +0000 | [diff] [blame] | 149 | _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); |
Saleem Abdulrasool | b75a574 | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 150 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 151 | public: |
| 152 | typedef _Tp* pointer; |
| 153 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 154 | ~__thread_specific_ptr(); |
| 155 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 156 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | 94340d2 | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 157 | pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 158 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 159 | pointer operator*() const {return *get();} |
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();} |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 162 | void set_pointer(pointer __p); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | template <class _Tp> |
Saleem Abdulrasool | b75a574 | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 166 | void _LIBCPP_TLS_DESTRUCTOR_CC |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 167 | __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) |
| 168 | { |
Howard Hinnant | 0eef25b | 2011-10-17 20:08:59 +0000 | [diff] [blame] | 169 | delete static_cast<pointer>(__p); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | template <class _Tp> |
| 173 | __thread_specific_ptr<_Tp>::__thread_specific_ptr() |
| 174 | { |
Saleem Abdulrasool | b75a574 | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 175 | int __ec = |
| 176 | __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit); |
| 177 | if (__ec) |
| 178 | __throw_system_error(__ec, "__thread_specific_ptr construction failed"); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | template <class _Tp> |
| 182 | __thread_specific_ptr<_Tp>::~__thread_specific_ptr() |
| 183 | { |
Eric Fiselier | 80b0e15 | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 184 | // __thread_specific_ptr is only created with a static storage duration |
| 185 | // so this destructor is only invoked during program termination. Invoking |
| 186 | // pthread_key_delete(__key_) may prevent other threads from deleting their |
| 187 | // thread local data. For this reason we leak the key. |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | template <class _Tp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 191 | void |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 192 | __thread_specific_ptr<_Tp>::set_pointer(pointer __p) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 193 | { |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 194 | _LIBCPP_ASSERT(get() == nullptr, |
| 195 | "Attempting to overwrite thread local data"); |
Asiri Rathnayake | 94340d2 | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 196 | __libcpp_tls_set(__key_, __p); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 199 | template<> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 200 | struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 201 | : public unary_function<__thread_id, size_t> |
| 202 | { |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 203 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4903689 | 2017-03-23 02:40:28 +0000 | [diff] [blame] | 204 | size_t operator()(__thread_id __v) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 205 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 206 | return hash<__libcpp_thread_id>()(__v.__id_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 207 | } |
| 208 | }; |
| 209 | |
Marshall Clow | 5865536 | 2019-08-14 16:21:27 +0000 | [diff] [blame] | 210 | template<class _CharT, class _Traits> |
| 211 | _LIBCPP_INLINE_VISIBILITY |
| 212 | basic_ostream<_CharT, _Traits>& |
| 213 | operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) |
| 214 | {return __os << __id.__id_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 215 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 216 | class _LIBCPP_TYPE_VIS thread |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 217 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 218 | __libcpp_thread_t __t_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | |
Howard Hinnant | aa87c0f | 2010-08-10 20:48:29 +0000 | [diff] [blame] | 220 | thread(const thread&); |
| 221 | thread& operator=(const thread&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 222 | public: |
| 223 | typedef __thread_id id; |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 224 | typedef __libcpp_thread_t native_handle_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 225 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 226 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | 3bab36f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 227 | thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 228 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 229 | template <class _Fp, class ..._Args, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 230 | class = typename enable_if |
| 231 | < |
Marshall Clow | aaf031b | 2018-03-20 22:37:37 +0000 | [diff] [blame] | 232 | !is_same<typename __uncvref<_Fp>::type, thread>::value |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | >::type |
| 234 | > |
Shoaib Meenai | 55f3a46 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 235 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 236 | explicit thread(_Fp&& __f, _Args&&... __args); |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 237 | #else // _LIBCPP_CXX03_LANG |
Shoaib Meenai | 55f3a46 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 238 | template <class _Fp> |
| 239 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
| 240 | explicit thread(_Fp __f); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 241 | #endif |
| 242 | ~thread(); |
| 243 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 244 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 0b1dda0 | 2020-06-03 11:51:59 -0400 | [diff] [blame] | 245 | thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { |
| 246 | __t.__t_ = _LIBCPP_NULL_THREAD; |
| 247 | } |
| 248 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 249 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 0b1dda0 | 2020-06-03 11:51:59 -0400 | [diff] [blame] | 250 | thread& operator=(thread&& __t) _NOEXCEPT { |
| 251 | if (!__libcpp_thread_isnull(&__t_)) |
| 252 | terminate(); |
| 253 | __t_ = __t.__t_; |
| 254 | __t.__t_ = _LIBCPP_NULL_THREAD; |
| 255 | return *this; |
| 256 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 258 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 259 | void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 260 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 261 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | 3bab36f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 262 | bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 263 | void join(); |
| 264 | void detach(); |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 265 | _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 266 | id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);} |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 267 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 268 | native_handle_type native_handle() _NOEXCEPT {return __t_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 269 | |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 270 | static unsigned hardware_concurrency() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 273 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 274 | |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 275 | template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices> |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 276 | inline _LIBCPP_INLINE_VISIBILITY |
| 277 | void |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 278 | __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 279 | { |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 280 | _VSTD::__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] | 281 | } |
| 282 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 283 | template <class _Fp> |
Louis Dionne | 3b967d0 | 2019-12-10 18:00:42 -0500 | [diff] [blame] | 284 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 285 | void* __thread_proxy(void* __vp) |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 286 | { |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 287 | // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...> |
| 288 | unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); |
| 289 | __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release()); |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 290 | typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 291 | _VSTD::__thread_execute(*__p.get(), _Index()); |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 292 | return nullptr; |
| 293 | } |
| 294 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 295 | template <class _Fp, class ..._Args, |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 296 | class |
| 297 | > |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 298 | thread::thread(_Fp&& __f, _Args&&... __args) |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 299 | { |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 300 | typedef unique_ptr<__thread_struct> _TSPtr; |
| 301 | _TSPtr __tsp(new __thread_struct); |
| 302 | typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp; |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 303 | unique_ptr<_Gp> __p( |
| 304 | new _Gp(_VSTD::move(__tsp), |
| 305 | _VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)), |
| 306 | _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...)); |
| 307 | int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 308 | if (__ec == 0) |
| 309 | __p.release(); |
| 310 | else |
| 311 | __throw_system_error(__ec, "thread constructor failed"); |
| 312 | } |
| 313 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 314 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | 4eea16b | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 315 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 316 | template <class _Fp> |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 317 | struct __thread_invoke_pair { |
| 318 | // This type is used to pass memory for thread local storage and a functor |
| 319 | // to a newly created thread because std::pair doesn't work with |
| 320 | // std::unique_ptr in C++03. |
| 321 | __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} |
| 322 | unique_ptr<__thread_struct> __tsp_; |
| 323 | _Fp __fn_; |
| 324 | }; |
| 325 | |
| 326 | template <class _Fp> |
| 327 | void* __thread_proxy_cxx03(void* __vp) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 328 | { |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 329 | unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 330 | __thread_local_data().set_pointer(__p->__tsp_.release()); |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 331 | (__p->__fn_)(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 332 | return nullptr; |
| 333 | } |
| 334 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 335 | template <class _Fp> |
| 336 | thread::thread(_Fp __f) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 337 | { |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 338 | |
| 339 | typedef __thread_invoke_pair<_Fp> _InvokePair; |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 340 | typedef unique_ptr<_InvokePair> _PairPtr; |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 341 | _PairPtr __pp(new _InvokePair(__f)); |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 342 | int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 343 | if (__ec == 0) |
Eric Fiselier | fd99606 | 2016-04-20 02:21:33 +0000 | [diff] [blame] | 344 | __pp.release(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 345 | else |
| 346 | __throw_system_error(__ec, "thread constructor failed"); |
| 347 | } |
| 348 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 349 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 350 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 351 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 352 | void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 353 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 354 | namespace this_thread |
| 355 | { |
| 356 | |
Joerg Sonnenberger | 917a30a | 2017-02-09 14:12:29 +0000 | [diff] [blame] | 357 | _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 358 | |
| 359 | template <class _Rep, class _Period> |
| 360 | void |
| 361 | sleep_for(const chrono::duration<_Rep, _Period>& __d) |
| 362 | { |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 363 | if (__d > chrono::duration<_Rep, _Period>::zero()) |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 364 | { |
Marshall Clow | 4aef5c3 | 2019-04-02 14:46:36 +0000 | [diff] [blame] | 365 | #if defined(_LIBCPP_COMPILER_GCC) && (__powerpc__ || __POWERPC__) |
| 366 | // GCC's long double const folding is incomplete for IBM128 long doubles. |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 367 | _LIBCPP_CONSTEXPR chrono::duration<long double> _Max = chrono::duration<long double>(ULLONG_MAX/1000000000ULL) ; |
Joel E. Denny | e123d26 | 2020-06-10 12:40:43 -0400 | [diff] [blame] | 368 | #else |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 369 | _LIBCPP_CONSTEXPR chrono::duration<long double> _Max = chrono::nanoseconds::max(); |
Marshall Clow | 4aef5c3 | 2019-04-02 14:46:36 +0000 | [diff] [blame] | 370 | #endif |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 371 | chrono::nanoseconds __ns; |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 372 | if (__d < _Max) |
| 373 | { |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 374 | __ns = chrono::duration_cast<chrono::nanoseconds>(__d); |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 375 | if (__ns < __d) |
| 376 | ++__ns; |
| 377 | } |
| 378 | else |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 379 | __ns = chrono::nanoseconds::max(); |
| 380 | this_thread::sleep_for(__ns); |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 381 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | template <class _Clock, class _Duration> |
| 385 | void |
| 386 | sleep_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 387 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 388 | mutex __mut; |
| 389 | condition_variable __cv; |
| 390 | unique_lock<mutex> __lk(__mut); |
| 391 | while (_Clock::now() < __t) |
| 392 | __cv.wait_until(__lk, __t); |
| 393 | } |
| 394 | |
| 395 | template <class _Duration> |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 396 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 397 | void |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 398 | sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 399 | { |
Arthur O'Dwyer | baac139 | 2020-11-27 14:44:53 -0500 | [diff] [blame] | 400 | this_thread::sleep_for(__t - chrono::steady_clock::now()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Howard Hinnant | 186dca8 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 403 | inline _LIBCPP_INLINE_VISIBILITY |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 404 | void yield() _NOEXCEPT {__libcpp_thread_yield();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 405 | |
| 406 | } // this_thread |
| 407 | |
| 408 | _LIBCPP_END_NAMESPACE_STD |
| 409 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 410 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 411 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 412 | _LIBCPP_POP_MACROS |
| 413 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 414 | #endif // _LIBCPP_THREAD |