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