Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- thread -----------------------------------===// |
| 3 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_THREAD |
| 12 | #define _LIBCPP_THREAD |
| 13 | |
| 14 | /* |
| 15 | |
| 16 | thread synopsis |
| 17 | |
Howard Hinnant | e8e7af2 | 2010-08-21 21:01:59 | [diff] [blame] | 18 | #define __STDCPP_THREADS__ __cplusplus |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [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> |
| 98 | #include <pthread.h> |
| 99 | |
| 100 | #pragma GCC system_header |
| 101 | |
Howard Hinnant | e8e7af2 | 2010-08-21 21:01:59 | [diff] [blame] | 102 | #define __STDCPP_THREADS__ __cplusplus |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 103 | |
| 104 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 105 | |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 106 | template <class _Tp> |
| 107 | class __thread_specific_ptr |
| 108 | { |
| 109 | pthread_key_t __key_; |
| 110 | |
| 111 | __thread_specific_ptr(const __thread_specific_ptr&); |
| 112 | __thread_specific_ptr& operator=(const __thread_specific_ptr&); |
| 113 | |
| 114 | static void __at_thread_exit(void*); |
| 115 | public: |
| 116 | typedef _Tp* pointer; |
| 117 | |
| 118 | __thread_specific_ptr(); |
| 119 | ~__thread_specific_ptr(); |
| 120 | |
| 121 | pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));} |
| 122 | pointer operator*() const {return *get();} |
| 123 | pointer operator->() const {return get();} |
| 124 | pointer release(); |
| 125 | void reset(pointer __p = nullptr); |
| 126 | }; |
| 127 | |
| 128 | template <class _Tp> |
| 129 | void |
| 130 | __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) |
| 131 | { |
| 132 | delete static_cast<pointer>(__p); |
| 133 | } |
| 134 | |
| 135 | template <class _Tp> |
| 136 | __thread_specific_ptr<_Tp>::__thread_specific_ptr() |
| 137 | { |
| 138 | int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit); |
| 139 | if (__ec) |
| 140 | throw system_error(error_code(__ec, system_category()), |
| 141 | "__thread_specific_ptr construction failed"); |
| 142 | } |
| 143 | |
| 144 | template <class _Tp> |
| 145 | __thread_specific_ptr<_Tp>::~__thread_specific_ptr() |
| 146 | { |
| 147 | pthread_key_delete(__key_); |
| 148 | } |
| 149 | |
| 150 | template <class _Tp> |
| 151 | typename __thread_specific_ptr<_Tp>::pointer |
| 152 | __thread_specific_ptr<_Tp>::release() |
| 153 | { |
| 154 | pointer __p = get(); |
| 155 | pthread_setspecific(__key_, 0); |
| 156 | return __p; |
| 157 | } |
| 158 | |
| 159 | template <class _Tp> |
| 160 | void |
| 161 | __thread_specific_ptr<_Tp>::reset(pointer __p) |
| 162 | { |
| 163 | pointer __p_old = get(); |
| 164 | pthread_setspecific(__key_, __p); |
| 165 | delete __p_old; |
| 166 | } |
| 167 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 168 | class thread; |
| 169 | class __thread_id; |
| 170 | |
| 171 | namespace this_thread |
| 172 | { |
| 173 | |
| 174 | __thread_id get_id(); |
| 175 | |
| 176 | } // this_thread |
| 177 | |
| 178 | class __thread_id |
| 179 | { |
Howard Hinnant | 128ba71 | 2010-05-24 17:49:41 | [diff] [blame] | 180 | // FIXME: pthread_t is a pointer on Darwin but a long on Linux. |
| 181 | // NULL is the no-thread value on Darwin. Someone needs to check |
| 182 | // on other platforms. We assume 0 works everywhere for now. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 183 | pthread_t __id_; |
| 184 | |
| 185 | public: |
| 186 | __thread_id() : __id_(0) {} |
| 187 | |
| 188 | friend bool operator==(__thread_id __x, __thread_id __y) |
| 189 | {return __x.__id_ == __y.__id_;} |
| 190 | friend bool operator!=(__thread_id __x, __thread_id __y) |
| 191 | {return !(__x == __y);} |
| 192 | friend bool operator< (__thread_id __x, __thread_id __y) |
| 193 | {return __x.__id_ < __y.__id_;} |
| 194 | friend bool operator<=(__thread_id __x, __thread_id __y) |
| 195 | {return !(__y < __x);} |
| 196 | friend bool operator> (__thread_id __x, __thread_id __y) |
| 197 | {return __y < __x ;} |
| 198 | friend bool operator>=(__thread_id __x, __thread_id __y) |
| 199 | {return !(__x < __y);} |
| 200 | |
| 201 | template<class _CharT, class _Traits> |
| 202 | friend |
| 203 | basic_ostream<_CharT, _Traits>& |
| 204 | operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) |
| 205 | {return __os << __id.__id_;} |
| 206 | |
| 207 | private: |
| 208 | __thread_id(pthread_t __id) : __id_(__id) {} |
| 209 | |
| 210 | friend __thread_id this_thread::get_id(); |
| 211 | friend class thread; |
| 212 | }; |
| 213 | |
| 214 | template<class _Tp> struct hash; |
| 215 | |
| 216 | template<> |
| 217 | struct hash<__thread_id> |
| 218 | : public unary_function<__thread_id, size_t> |
| 219 | { |
| 220 | size_t operator()(__thread_id __v) const |
| 221 | { |
| 222 | const size_t* const __p = reinterpret_cast<const size_t*>(&__v); |
| 223 | return *__p; |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | namespace this_thread |
| 228 | { |
| 229 | |
| 230 | inline |
| 231 | __thread_id |
| 232 | get_id() |
| 233 | { |
| 234 | return pthread_self(); |
| 235 | } |
| 236 | |
| 237 | } // this_thread |
| 238 | |
| 239 | class thread |
| 240 | { |
| 241 | pthread_t __t_; |
| 242 | |
Howard Hinnant | eb26925 | 2010-08-10 20:48:29 | [diff] [blame] | 243 | #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS |
| 244 | thread(const thread&) = delete; |
| 245 | thread& operator=(const thread&) = delete; |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 | [diff] [blame] | 246 | #else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS |
Howard Hinnant | eb26925 | 2010-08-10 20:48:29 | [diff] [blame] | 247 | thread(const thread&); |
| 248 | thread& operator=(const thread&); |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 | [diff] [blame] | 249 | #endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 250 | public: |
| 251 | typedef __thread_id id; |
| 252 | typedef pthread_t native_handle_type; |
| 253 | |
| 254 | thread() : __t_(0) {} |
| 255 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 256 | template <class _F, class ..._Args, |
| 257 | class = typename enable_if |
| 258 | < |
| 259 | !is_same<typename decay<_F>::type, thread>::value |
| 260 | >::type |
| 261 | > |
| 262 | explicit thread(_F&& __f, _Args&&... __args); |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 | [diff] [blame] | 263 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 264 | template <class _F> explicit thread(_F __f); |
| 265 | #endif |
| 266 | ~thread(); |
| 267 | |
| 268 | #ifdef _LIBCPP_MOVE |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 269 | thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 270 | thread& operator=(thread&& __t); |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 | [diff] [blame] | 271 | #endif // _LIBCPP_MOVE |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 272 | |
| 273 | void swap(thread& __t) {_STD::swap(__t_, __t.__t_);} |
| 274 | |
Howard Hinnant | 128ba71 | 2010-05-24 17:49:41 | [diff] [blame] | 275 | bool joinable() const {return __t_ != 0;} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 276 | void join(); |
| 277 | void detach(); |
| 278 | id get_id() const {return __t_;} |
| 279 | native_handle_type native_handle() {return __t_;} |
| 280 | |
| 281 | static unsigned hardware_concurrency(); |
| 282 | }; |
| 283 | |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 284 | class __assoc_sub_state; |
| 285 | |
| 286 | class __thread_struct_imp; |
| 287 | |
| 288 | class __thread_struct |
| 289 | { |
| 290 | __thread_struct_imp* __p_; |
| 291 | |
| 292 | __thread_struct(const __thread_struct&); |
| 293 | __thread_struct& operator=(const __thread_struct&); |
| 294 | public: |
| 295 | __thread_struct(); |
| 296 | ~__thread_struct(); |
| 297 | |
Howard Hinnant | b77c0c0 | 2010-09-03 21:46:37 | [diff] [blame^] | 298 | void notify_all_at_thread_exit(condition_variable*, mutex*); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 299 | void __make_ready_at_thread_exit(__assoc_sub_state*); |
| 300 | }; |
| 301 | |
| 302 | extern __thread_specific_ptr<__thread_struct> __thread_local_data; |
| 303 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 304 | template <class _F> |
| 305 | void* |
| 306 | __thread_proxy(void* __vp) |
| 307 | { |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 308 | __thread_local_data.reset(new __thread_struct); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 309 | std::unique_ptr<_F> __p(static_cast<_F*>(__vp)); |
| 310 | (*__p)(); |
| 311 | return nullptr; |
| 312 | } |
| 313 | |
| 314 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 315 | |
| 316 | template <class _F, class ..._Args, |
| 317 | class |
| 318 | > |
| 319 | thread::thread(_F&& __f, _Args&&... __args) |
| 320 | { |
| 321 | typedef decltype(bind(std::forward<_F>(__f), std::forward<_Args>(__args)...)) _G; |
| 322 | std::unique_ptr<_G> __p(new _G(bind(std::forward<_F>(__f), |
| 323 | std::forward<_Args>(__args)...))); |
| 324 | int __ec = pthread_create(&__t_, 0, &__thread_proxy<_G>, __p.get()); |
| 325 | if (__ec == 0) |
| 326 | __p.release(); |
| 327 | else |
| 328 | __throw_system_error(__ec, "thread constructor failed"); |
| 329 | } |
| 330 | |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 | [diff] [blame] | 331 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 332 | |
| 333 | template <class _F> |
| 334 | thread::thread(_F __f) |
| 335 | { |
| 336 | std::unique_ptr<_F> __p(new _F(__f)); |
| 337 | int __ec = pthread_create(&__t_, 0, &__thread_proxy<_F>, __p.get()); |
| 338 | if (__ec == 0) |
| 339 | __p.release(); |
| 340 | else |
| 341 | __throw_system_error(__ec, "thread constructor failed"); |
| 342 | } |
| 343 | |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 | [diff] [blame] | 344 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 345 | |
| 346 | #ifdef _LIBCPP_MOVE |
| 347 | |
| 348 | inline |
| 349 | thread& |
| 350 | thread::operator=(thread&& __t) |
| 351 | { |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 352 | if (__t_ != 0) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 353 | terminate(); |
| 354 | __t_ = __t.__t_; |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 355 | __t.__t_ = 0; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 356 | return *this; |
| 357 | } |
| 358 | |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 | [diff] [blame] | 359 | #endif // _LIBCPP_MOVE |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 360 | |
| 361 | inline |
| 362 | void swap(thread& __x, thread& __y) {__x.swap(__y);} |
| 363 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 364 | namespace this_thread |
| 365 | { |
| 366 | |
| 367 | void sleep_for(const chrono::nanoseconds& ns); |
| 368 | |
| 369 | template <class _Rep, class _Period> |
| 370 | void |
| 371 | sleep_for(const chrono::duration<_Rep, _Period>& __d) |
| 372 | { |
| 373 | using namespace chrono; |
| 374 | nanoseconds __ns = duration_cast<nanoseconds>(__d); |
| 375 | if (__ns < __d) |
| 376 | ++__ns; |
| 377 | sleep_for(__ns); |
| 378 | } |
| 379 | |
| 380 | template <class _Clock, class _Duration> |
| 381 | void |
| 382 | sleep_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 383 | { |
| 384 | using namespace chrono; |
| 385 | mutex __mut; |
| 386 | condition_variable __cv; |
| 387 | unique_lock<mutex> __lk(__mut); |
| 388 | while (_Clock::now() < __t) |
| 389 | __cv.wait_until(__lk, __t); |
| 390 | } |
| 391 | |
| 392 | template <class _Duration> |
| 393 | inline |
| 394 | void |
| 395 | sleep_until(const chrono::time_point<chrono::monotonic_clock, _Duration>& __t) |
| 396 | { |
| 397 | using namespace chrono; |
| 398 | sleep_for(__t - monotonic_clock::now()); |
| 399 | } |
| 400 | |
| 401 | inline |
| 402 | void yield() {sched_yield();} |
| 403 | |
| 404 | } // this_thread |
| 405 | |
| 406 | _LIBCPP_END_NAMESPACE_STD |
| 407 | |
| 408 | #endif // _LIBCPP_THREAD |