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> |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 98 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 99 | #include <tuple> |
| 100 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 101 | #include <pthread.h> |
Sergey Dmitrouk | fff544e | 2014-12-08 14:50:21 +0000 | [diff] [blame] | 102 | #include <sched.h> |
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 | |
Howard Hinnant | a785e4e | 2010-08-21 21:01:59 +0000 | [diff] [blame] | 108 | #define __STDCPP_THREADS__ __cplusplus |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 109 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 110 | #ifdef _LIBCPP_HAS_NO_THREADS |
| 111 | #error <thread> is not supported on this single threaded system |
| 112 | #else // !_LIBCPP_HAS_NO_THREADS |
| 113 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 115 | |
Eric Fiselier | 0376dfa | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 116 | template <class _Tp> class __thread_specific_ptr; |
| 117 | class _LIBCPP_TYPE_VIS __thread_struct; |
| 118 | class _LIBCPP_HIDDEN __thread_struct_imp; |
| 119 | class __assoc_sub_state; |
| 120 | |
| 121 | _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); |
| 122 | |
| 123 | class _LIBCPP_TYPE_VIS __thread_struct |
| 124 | { |
| 125 | __thread_struct_imp* __p_; |
| 126 | |
| 127 | __thread_struct(const __thread_struct&); |
| 128 | __thread_struct& operator=(const __thread_struct&); |
| 129 | public: |
| 130 | __thread_struct(); |
| 131 | ~__thread_struct(); |
| 132 | |
| 133 | void notify_all_at_thread_exit(condition_variable*, mutex*); |
| 134 | void __make_ready_at_thread_exit(__assoc_sub_state*); |
| 135 | }; |
| 136 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 137 | template <class _Tp> |
| 138 | class __thread_specific_ptr |
| 139 | { |
| 140 | pthread_key_t __key_; |
| 141 | |
Eric Fiselier | 0376dfa | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 142 | // Only __thread_local_data() may construct a __thread_specific_ptr |
| 143 | // and only with _Tp == __thread_struct. |
Eric Fiselier | f99b59c | 2015-08-19 03:38:41 +0000 | [diff] [blame^] | 144 | static_assert((is_same<_Tp, __thread_struct>::value), ""); |
Eric Fiselier | 0376dfa | 2015-08-18 19:40:38 +0000 | [diff] [blame] | 145 | __thread_specific_ptr(); |
| 146 | friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); |
| 147 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 148 | __thread_specific_ptr(const __thread_specific_ptr&); |
| 149 | __thread_specific_ptr& operator=(const __thread_specific_ptr&); |
| 150 | |
| 151 | static void __at_thread_exit(void*); |
| 152 | public: |
| 153 | typedef _Tp* pointer; |
| 154 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 155 | ~__thread_specific_ptr(); |
| 156 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 157 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 158 | pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 159 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 160 | pointer operator*() const {return *get();} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 161 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 162 | pointer operator->() const {return get();} |
| 163 | pointer release(); |
| 164 | void reset(pointer __p = nullptr); |
| 165 | }; |
| 166 | |
| 167 | template <class _Tp> |
| 168 | void |
| 169 | __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) |
| 170 | { |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame] | 171 | delete static_cast<pointer>(__p); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | template <class _Tp> |
| 175 | __thread_specific_ptr<_Tp>::__thread_specific_ptr() |
| 176 | { |
| 177 | int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit); |
Howard Hinnant | 2c0d3ed | 2013-03-28 15:00:04 +0000 | [diff] [blame] | 178 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame] | 179 | if (__ec) |
| 180 | throw system_error(error_code(__ec, system_category()), |
| 181 | "__thread_specific_ptr construction failed"); |
Howard Hinnant | 2c0d3ed | 2013-03-28 15:00:04 +0000 | [diff] [blame] | 182 | #endif |
Howard Hinnant | 47499b1 | 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 | 0376dfa | 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 | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | template <class _Tp> |
| 195 | typename __thread_specific_ptr<_Tp>::pointer |
| 196 | __thread_specific_ptr<_Tp>::release() |
| 197 | { |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame] | 198 | pointer __p = get(); |
| 199 | pthread_setspecific(__key_, 0); |
| 200 | return __p; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | template <class _Tp> |
| 204 | void |
| 205 | __thread_specific_ptr<_Tp>::reset(pointer __p) |
| 206 | { |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame] | 207 | pointer __p_old = get(); |
| 208 | pthread_setspecific(__key_, __p); |
| 209 | delete __p_old; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 212 | class _LIBCPP_TYPE_VIS thread; |
| 213 | class _LIBCPP_TYPE_VIS __thread_id; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | |
| 215 | namespace this_thread |
| 216 | { |
| 217 | |
Howard Hinnant | 33be35e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 218 | _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | |
| 220 | } // this_thread |
| 221 | |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 222 | template<> struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>; |
Howard Hinnant | 40c13d3 | 2011-12-05 00:08:45 +0000 | [diff] [blame] | 223 | |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 224 | class _LIBCPP_TYPE_VIS_ONLY __thread_id |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 225 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 226 | // FIXME: pthread_t is a pointer on Darwin but a long on Linux. |
| 227 | // NULL is the no-thread value on Darwin. Someone needs to check |
| 228 | // on other platforms. We assume 0 works everywhere for now. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | pthread_t __id_; |
| 230 | |
| 231 | public: |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 232 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 233 | __thread_id() _NOEXCEPT : __id_(0) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 234 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 235 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 236 | bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 237 | {return __x.__id_ == __y.__id_;} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 238 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 239 | bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 240 | {return !(__x == __y);} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 241 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 242 | bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 243 | {return __x.__id_ < __y.__id_;} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 244 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 245 | bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 246 | {return !(__y < __x);} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 247 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 248 | bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | {return __y < __x ;} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 250 | friend _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 251 | bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 252 | {return !(__x < __y);} |
| 253 | |
| 254 | template<class _CharT, class _Traits> |
| 255 | friend |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 256 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | basic_ostream<_CharT, _Traits>& |
| 258 | operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) |
| 259 | {return __os << __id.__id_;} |
| 260 | |
| 261 | private: |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 262 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 263 | __thread_id(pthread_t __id) : __id_(__id) {} |
| 264 | |
Howard Hinnant | 96c60b4 | 2012-08-19 15:13:16 +0000 | [diff] [blame] | 265 | friend __thread_id this_thread::get_id() _NOEXCEPT; |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 266 | friend class _LIBCPP_TYPE_VIS thread; |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 267 | friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 270 | template<> |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 271 | struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 272 | : public unary_function<__thread_id, size_t> |
| 273 | { |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 274 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 275 | size_t operator()(__thread_id __v) const |
| 276 | { |
Howard Hinnant | 40c13d3 | 2011-12-05 00:08:45 +0000 | [diff] [blame] | 277 | return hash<pthread_t>()(__v.__id_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 278 | } |
| 279 | }; |
| 280 | |
| 281 | namespace this_thread |
| 282 | { |
| 283 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 284 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 285 | __thread_id |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 286 | get_id() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 287 | { |
| 288 | return pthread_self(); |
| 289 | } |
| 290 | |
| 291 | } // this_thread |
| 292 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 293 | class _LIBCPP_TYPE_VIS thread |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 294 | { |
| 295 | pthread_t __t_; |
| 296 | |
Howard Hinnant | 60a0a8e | 2010-08-10 20:48:29 +0000 | [diff] [blame] | 297 | thread(const thread&); |
| 298 | thread& operator=(const thread&); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 299 | public: |
| 300 | typedef __thread_id id; |
| 301 | typedef pthread_t native_handle_type; |
| 302 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 303 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 304 | thread() _NOEXCEPT : __t_(0) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 305 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 306 | template <class _Fp, class ..._Args, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 307 | class = typename enable_if |
| 308 | < |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 309 | !is_same<typename decay<_Fp>::type, thread>::value |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 310 | >::type |
| 311 | > |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 312 | explicit thread(_Fp&& __f, _Args&&... __args); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 313 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 314 | template <class _Fp> explicit thread(_Fp __f); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 315 | #endif |
| 316 | ~thread(); |
| 317 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 318 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 319 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 320 | thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;} |
| 321 | thread& operator=(thread&& __t) _NOEXCEPT; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 322 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 323 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 324 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 325 | void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 326 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 327 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 328 | bool joinable() const _NOEXCEPT {return __t_ != 0;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 329 | void join(); |
| 330 | void detach(); |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 331 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 332 | id get_id() const _NOEXCEPT {return __t_;} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 333 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 334 | native_handle_type native_handle() _NOEXCEPT {return __t_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 335 | |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 336 | static unsigned hardware_concurrency() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 339 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 340 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 341 | template <class _Fp, class ..._Args, size_t ..._Indices> |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 342 | inline _LIBCPP_INLINE_VISIBILITY |
| 343 | void |
Howard Hinnant | c5e8961 | 2013-04-03 20:29:45 +0000 | [diff] [blame] | 344 | __thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>) |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 345 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 346 | __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 349 | template <class _Fp> |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 350 | void* |
| 351 | __thread_proxy(void* __vp) |
| 352 | { |
| 353 | __thread_local_data().reset(new __thread_struct); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 354 | std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); |
| 355 | typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; |
Howard Hinnant | c5e8961 | 2013-04-03 20:29:45 +0000 | [diff] [blame] | 356 | __thread_execute(*__p, _Index()); |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 357 | return nullptr; |
| 358 | } |
| 359 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 360 | template <class _Fp, class ..._Args, |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 361 | class |
| 362 | > |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 363 | thread::thread(_Fp&& __f, _Args&&... __args) |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 364 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 365 | typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp; |
| 366 | _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)), |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 367 | __decay_copy(_VSTD::forward<_Args>(__args))...)); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 368 | int __ec = pthread_create(&__t_, 0, &__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 | |
| 375 | #else // _LIBCPP_HAS_NO_VARIADICS |
| 376 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 377 | template <class _Fp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 378 | void* |
| 379 | __thread_proxy(void* __vp) |
| 380 | { |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 381 | __thread_local_data().reset(new __thread_struct); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 382 | std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | (*__p)(); |
| 384 | return nullptr; |
| 385 | } |
| 386 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 387 | template <class _Fp> |
| 388 | thread::thread(_Fp __f) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 389 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 390 | std::unique_ptr<_Fp> __p(new _Fp(__f)); |
| 391 | int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 392 | if (__ec == 0) |
| 393 | __p.release(); |
| 394 | else |
| 395 | __throw_system_error(__ec, "thread constructor failed"); |
| 396 | } |
| 397 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 398 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 399 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 400 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 401 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 402 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 403 | thread& |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 404 | thread::operator=(thread&& __t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 405 | { |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 406 | if (__t_ != 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 407 | terminate(); |
| 408 | __t_ = __t.__t_; |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 409 | __t.__t_ = 0; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 410 | return *this; |
| 411 | } |
| 412 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 413 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 414 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 415 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 416 | void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 417 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 418 | namespace this_thread |
| 419 | { |
| 420 | |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 421 | _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 422 | |
| 423 | template <class _Rep, class _Period> |
| 424 | void |
| 425 | sleep_for(const chrono::duration<_Rep, _Period>& __d) |
| 426 | { |
| 427 | using namespace chrono; |
Howard Hinnant | cf115d2 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 428 | if (__d > duration<_Rep, _Period>::zero()) |
| 429 | { |
| 430 | _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max(); |
| 431 | nanoseconds __ns; |
| 432 | if (__d < _Max) |
| 433 | { |
| 434 | __ns = duration_cast<nanoseconds>(__d); |
| 435 | if (__ns < __d) |
| 436 | ++__ns; |
| 437 | } |
| 438 | else |
| 439 | __ns = nanoseconds::max(); |
| 440 | sleep_for(__ns); |
| 441 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | template <class _Clock, class _Duration> |
| 445 | void |
| 446 | sleep_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 447 | { |
| 448 | using namespace chrono; |
| 449 | mutex __mut; |
| 450 | condition_variable __cv; |
| 451 | unique_lock<mutex> __lk(__mut); |
| 452 | while (_Clock::now() < __t) |
| 453 | __cv.wait_until(__lk, __t); |
| 454 | } |
| 455 | |
| 456 | template <class _Duration> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 457 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 458 | void |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 459 | sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 460 | { |
| 461 | using namespace chrono; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 462 | sleep_for(__t - steady_clock::now()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 465 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6e1d851 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 466 | void yield() _NOEXCEPT {sched_yield();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 467 | |
| 468 | } // this_thread |
| 469 | |
| 470 | _LIBCPP_END_NAMESPACE_STD |
| 471 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 472 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 473 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 474 | #endif // _LIBCPP_THREAD |