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