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 | |
| 29 | thread(); |
| 30 | template <class F, class ...Args> explicit thread(F&& f, Args&&... args); |
| 31 | ~thread(); |
| 32 | |
| 33 | thread(const thread&) = delete; |
| 34 | thread(thread&& t); |
| 35 | |
| 36 | thread& operator=(const thread&) = delete; |
| 37 | thread& operator=(thread&& t); |
| 38 | |
| 39 | void swap(thread& t); |
| 40 | |
| 41 | bool joinable() const; |
| 42 | void join(); |
| 43 | void detach(); |
| 44 | id get_id() const; |
| 45 | native_handle_type native_handle(); |
| 46 | |
| 47 | static unsigned hardware_concurrency(); |
| 48 | }; |
| 49 | |
| 50 | void swap(thread& x, thread& y); |
| 51 | |
| 52 | class thread::id |
| 53 | { |
| 54 | public: |
| 55 | id(); |
| 56 | }; |
| 57 | |
| 58 | bool operator==(thread::id x, thread::id y); |
| 59 | bool operator!=(thread::id x, thread::id y); |
| 60 | bool operator< (thread::id x, thread::id y); |
| 61 | bool operator<=(thread::id x, thread::id y); |
| 62 | bool operator> (thread::id x, thread::id y); |
| 63 | bool operator>=(thread::id x, thread::id y); |
| 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 | |
| 72 | thread::id get_id(); |
| 73 | |
| 74 | void yield(); |
| 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> |
| 102 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 103 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 104 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 105 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 106 | |
Howard Hinnant | a785e4e | 2010-08-21 21:01:59 +0000 | [diff] [blame] | 107 | #define __STDCPP_THREADS__ __cplusplus |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 108 | |
| 109 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 110 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 111 | template <class _Tp> |
| 112 | class __thread_specific_ptr |
| 113 | { |
| 114 | pthread_key_t __key_; |
| 115 | |
| 116 | __thread_specific_ptr(const __thread_specific_ptr&); |
| 117 | __thread_specific_ptr& operator=(const __thread_specific_ptr&); |
| 118 | |
| 119 | static void __at_thread_exit(void*); |
| 120 | public: |
| 121 | typedef _Tp* pointer; |
| 122 | |
| 123 | __thread_specific_ptr(); |
| 124 | ~__thread_specific_ptr(); |
| 125 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 126 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 127 | pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 128 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 129 | pointer operator*() const {return *get();} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 130 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 131 | pointer operator->() const {return get();} |
| 132 | pointer release(); |
| 133 | void reset(pointer __p = nullptr); |
| 134 | }; |
| 135 | |
| 136 | template <class _Tp> |
| 137 | void |
| 138 | __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) |
| 139 | { |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame^] | 140 | delete static_cast<pointer>(__p); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | template <class _Tp> |
| 144 | __thread_specific_ptr<_Tp>::__thread_specific_ptr() |
| 145 | { |
| 146 | int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit); |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame^] | 147 | if (__ec) |
| 148 | throw system_error(error_code(__ec, system_category()), |
| 149 | "__thread_specific_ptr construction failed"); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | template <class _Tp> |
| 153 | __thread_specific_ptr<_Tp>::~__thread_specific_ptr() |
| 154 | { |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame^] | 155 | pthread_key_delete(__key_); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | template <class _Tp> |
| 159 | typename __thread_specific_ptr<_Tp>::pointer |
| 160 | __thread_specific_ptr<_Tp>::release() |
| 161 | { |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame^] | 162 | pointer __p = get(); |
| 163 | pthread_setspecific(__key_, 0); |
| 164 | return __p; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | template <class _Tp> |
| 168 | void |
| 169 | __thread_specific_ptr<_Tp>::reset(pointer __p) |
| 170 | { |
Howard Hinnant | 8db4aca | 2011-10-17 20:08:59 +0000 | [diff] [blame^] | 171 | pointer __p_old = get(); |
| 172 | pthread_setspecific(__key_, __p); |
| 173 | delete __p_old; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 176 | class thread; |
| 177 | class __thread_id; |
| 178 | |
| 179 | namespace this_thread |
| 180 | { |
| 181 | |
| 182 | __thread_id get_id(); |
| 183 | |
| 184 | } // this_thread |
| 185 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 186 | class _LIBCPP_VISIBLE __thread_id |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 187 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 188 | // FIXME: pthread_t is a pointer on Darwin but a long on Linux. |
| 189 | // NULL is the no-thread value on Darwin. Someone needs to check |
| 190 | // on other platforms. We assume 0 works everywhere for now. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 191 | pthread_t __id_; |
| 192 | |
| 193 | public: |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 194 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 195 | __thread_id() : __id_(0) {} |
| 196 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 197 | friend _LIBCPP_INLINE_VISIBILITY |
| 198 | bool operator==(__thread_id __x, __thread_id __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 199 | {return __x.__id_ == __y.__id_;} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 200 | friend _LIBCPP_INLINE_VISIBILITY |
| 201 | bool operator!=(__thread_id __x, __thread_id __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 202 | {return !(__x == __y);} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 203 | friend _LIBCPP_INLINE_VISIBILITY |
| 204 | bool operator< (__thread_id __x, __thread_id __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 205 | {return __x.__id_ < __y.__id_;} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 206 | friend _LIBCPP_INLINE_VISIBILITY |
| 207 | bool operator<=(__thread_id __x, __thread_id __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 208 | {return !(__y < __x);} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 209 | friend _LIBCPP_INLINE_VISIBILITY |
| 210 | bool operator> (__thread_id __x, __thread_id __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 211 | {return __y < __x ;} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 212 | friend _LIBCPP_INLINE_VISIBILITY |
| 213 | bool operator>=(__thread_id __x, __thread_id __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | {return !(__x < __y);} |
| 215 | |
| 216 | template<class _CharT, class _Traits> |
| 217 | friend |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 218 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | basic_ostream<_CharT, _Traits>& |
| 220 | operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) |
| 221 | {return __os << __id.__id_;} |
| 222 | |
| 223 | private: |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 224 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 225 | __thread_id(pthread_t __id) : __id_(__id) {} |
| 226 | |
| 227 | friend __thread_id this_thread::get_id(); |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 228 | friend class _LIBCPP_VISIBLE thread; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | template<class _Tp> struct hash; |
| 232 | |
| 233 | template<> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 234 | struct _LIBCPP_VISIBLE hash<__thread_id> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | : public unary_function<__thread_id, size_t> |
| 236 | { |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 237 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 238 | size_t operator()(__thread_id __v) const |
| 239 | { |
| 240 | const size_t* const __p = reinterpret_cast<const size_t*>(&__v); |
| 241 | return *__p; |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | namespace this_thread |
| 246 | { |
| 247 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 248 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | __thread_id |
| 250 | get_id() |
| 251 | { |
| 252 | return pthread_self(); |
| 253 | } |
| 254 | |
| 255 | } // this_thread |
| 256 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 257 | class _LIBCPP_VISIBLE thread |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 258 | { |
| 259 | pthread_t __t_; |
| 260 | |
Howard Hinnant | 60a0a8e | 2010-08-10 20:48:29 +0000 | [diff] [blame] | 261 | thread(const thread&); |
| 262 | thread& operator=(const thread&); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 263 | public: |
| 264 | typedef __thread_id id; |
| 265 | typedef pthread_t native_handle_type; |
| 266 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 267 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 268 | thread() : __t_(0) {} |
| 269 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 270 | template <class _F, class ..._Args, |
| 271 | class = typename enable_if |
| 272 | < |
| 273 | !is_same<typename decay<_F>::type, thread>::value |
| 274 | >::type |
| 275 | > |
| 276 | explicit thread(_F&& __f, _Args&&... __args); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 277 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 278 | template <class _F> explicit thread(_F __f); |
| 279 | #endif |
| 280 | ~thread(); |
| 281 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 282 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 283 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 284 | thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 285 | thread& operator=(thread&& __t); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 286 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 287 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 288 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 289 | void swap(thread& __t) {_VSTD::swap(__t_, __t.__t_);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 290 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 291 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 292 | bool joinable() const {return __t_ != 0;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 293 | void join(); |
| 294 | void detach(); |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 295 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 296 | id get_id() const {return __t_;} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 297 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 298 | native_handle_type native_handle() {return __t_;} |
| 299 | |
| 300 | static unsigned hardware_concurrency(); |
| 301 | }; |
| 302 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 303 | class __assoc_sub_state; |
| 304 | |
Howard Hinnant | 2d72b1e | 2010-12-17 14:46:43 +0000 | [diff] [blame] | 305 | class _LIBCPP_HIDDEN __thread_struct_imp; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 306 | |
| 307 | class __thread_struct |
| 308 | { |
| 309 | __thread_struct_imp* __p_; |
| 310 | |
| 311 | __thread_struct(const __thread_struct&); |
| 312 | __thread_struct& operator=(const __thread_struct&); |
| 313 | public: |
| 314 | __thread_struct(); |
| 315 | ~__thread_struct(); |
| 316 | |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 317 | void notify_all_at_thread_exit(condition_variable*, mutex*); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 318 | void __make_ready_at_thread_exit(__assoc_sub_state*); |
| 319 | }; |
| 320 | |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 321 | __thread_specific_ptr<__thread_struct>& __thread_local_data(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 322 | |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 323 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 324 | |
| 325 | template <class _F, class ..._Args, size_t ..._Indices> |
| 326 | inline _LIBCPP_INLINE_VISIBILITY |
| 327 | void |
| 328 | __threaad_execute(tuple<_F, _Args...>& __t, __tuple_indices<_Indices...>) |
| 329 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 330 | __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] | 331 | } |
| 332 | |
| 333 | template <class _F> |
| 334 | void* |
| 335 | __thread_proxy(void* __vp) |
| 336 | { |
| 337 | __thread_local_data().reset(new __thread_struct); |
| 338 | std::unique_ptr<_F> __p(static_cast<_F*>(__vp)); |
| 339 | typedef typename __make_tuple_indices<tuple_size<_F>::value, 1>::type _Index; |
| 340 | __threaad_execute(*__p, _Index()); |
| 341 | return nullptr; |
| 342 | } |
| 343 | |
| 344 | template <class _F, class ..._Args, |
| 345 | class |
| 346 | > |
| 347 | thread::thread(_F&& __f, _Args&&... __args) |
| 348 | { |
| 349 | typedef tuple<typename decay<_F>::type, typename decay<_Args>::type...> _G; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 350 | _VSTD::unique_ptr<_G> __p(new _G(__decay_copy(_VSTD::forward<_F>(__f)), |
| 351 | __decay_copy(_VSTD::forward<_Args>(__args))...)); |
Howard Hinnant | 656bdc3 | 2011-05-16 18:40:35 +0000 | [diff] [blame] | 352 | int __ec = pthread_create(&__t_, 0, &__thread_proxy<_G>, __p.get()); |
| 353 | if (__ec == 0) |
| 354 | __p.release(); |
| 355 | else |
| 356 | __throw_system_error(__ec, "thread constructor failed"); |
| 357 | } |
| 358 | |
| 359 | #else // _LIBCPP_HAS_NO_VARIADICS |
| 360 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 361 | template <class _F> |
| 362 | void* |
| 363 | __thread_proxy(void* __vp) |
| 364 | { |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 365 | __thread_local_data().reset(new __thread_struct); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 366 | std::unique_ptr<_F> __p(static_cast<_F*>(__vp)); |
| 367 | (*__p)(); |
| 368 | return nullptr; |
| 369 | } |
| 370 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 371 | template <class _F> |
| 372 | thread::thread(_F __f) |
| 373 | { |
| 374 | std::unique_ptr<_F> __p(new _F(__f)); |
| 375 | int __ec = pthread_create(&__t_, 0, &__thread_proxy<_F>, __p.get()); |
| 376 | if (__ec == 0) |
| 377 | __p.release(); |
| 378 | else |
| 379 | __throw_system_error(__ec, "thread constructor failed"); |
| 380 | } |
| 381 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 382 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 384 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 385 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 386 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 387 | thread& |
| 388 | thread::operator=(thread&& __t) |
| 389 | { |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 390 | if (__t_ != 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 391 | terminate(); |
| 392 | __t_ = __t.__t_; |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 393 | __t.__t_ = 0; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 394 | return *this; |
| 395 | } |
| 396 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 397 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 398 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 399 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 400 | void swap(thread& __x, thread& __y) {__x.swap(__y);} |
| 401 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 402 | namespace this_thread |
| 403 | { |
| 404 | |
| 405 | void sleep_for(const chrono::nanoseconds& ns); |
| 406 | |
| 407 | template <class _Rep, class _Period> |
| 408 | void |
| 409 | sleep_for(const chrono::duration<_Rep, _Period>& __d) |
| 410 | { |
| 411 | using namespace chrono; |
| 412 | nanoseconds __ns = duration_cast<nanoseconds>(__d); |
| 413 | if (__ns < __d) |
| 414 | ++__ns; |
| 415 | sleep_for(__ns); |
| 416 | } |
| 417 | |
| 418 | template <class _Clock, class _Duration> |
| 419 | void |
| 420 | sleep_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 421 | { |
| 422 | using namespace chrono; |
| 423 | mutex __mut; |
| 424 | condition_variable __cv; |
| 425 | unique_lock<mutex> __lk(__mut); |
| 426 | while (_Clock::now() < __t) |
| 427 | __cv.wait_until(__lk, __t); |
| 428 | } |
| 429 | |
| 430 | template <class _Duration> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 431 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 432 | void |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 433 | sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | { |
| 435 | using namespace chrono; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 436 | sleep_for(__t - steady_clock::now()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 439 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 440 | void yield() {sched_yield();} |
| 441 | |
| 442 | } // this_thread |
| 443 | |
| 444 | _LIBCPP_END_NAMESPACE_STD |
| 445 | |
| 446 | #endif // _LIBCPP_THREAD |