blob: 360b43d5e555ae8996a9b996d67297fbee03c779 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- thread -----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_THREAD
12#define _LIBCPP_THREAD
13
14/*
15
16 thread synopsis
17
Howard Hinnant29aba232010-08-21 21:01:59 +000018#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantc51e1022010-05-11 19:42:16 +000019
20namespace std
21{
22
23class thread
24{
25public:
26 class id;
27 typedef pthread_t native_handle_type;
28
Howard Hinnant4bd34702012-07-21 16:50:47 +000029 thread() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000030 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
31 ~thread();
32
33 thread(const thread&) = delete;
Howard Hinnant4bd34702012-07-21 16:50:47 +000034 thread(thread&& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000035
36 thread& operator=(const thread&) = delete;
Howard Hinnant4bd34702012-07-21 16:50:47 +000037 thread& operator=(thread&& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000038
Howard Hinnant4bd34702012-07-21 16:50:47 +000039 void swap(thread& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000040
Howard Hinnant4bd34702012-07-21 16:50:47 +000041 bool joinable() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 void join();
43 void detach();
Howard Hinnant4bd34702012-07-21 16:50:47 +000044 id get_id() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000045 native_handle_type native_handle();
46
Howard Hinnant4bd34702012-07-21 16:50:47 +000047 static unsigned hardware_concurrency() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000048};
49
Howard Hinnant4bd34702012-07-21 16:50:47 +000050void swap(thread& x, thread& y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000051
52class thread::id
53{
54public:
Howard Hinnant4bd34702012-07-21 16:50:47 +000055 id() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000056};
57
Howard Hinnant4bd34702012-07-21 16:50:47 +000058bool operator==(thread::id x, thread::id y) noexcept;
59bool operator!=(thread::id x, thread::id y) noexcept;
60bool operator< (thread::id x, thread::id y) noexcept;
61bool operator<=(thread::id x, thread::id y) noexcept;
62bool operator> (thread::id x, thread::id y) noexcept;
63bool operator>=(thread::id x, thread::id y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000064
65template<class charT, class traits>
66basic_ostream<charT, traits>&
67operator<<(basic_ostream<charT, traits>& out, thread::id id);
68
69namespace this_thread
70{
71
Howard Hinnant4bd34702012-07-21 16:50:47 +000072thread::id get_id() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000073
Howard Hinnant4bd34702012-07-21 16:50:47 +000074void yield() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
76template <class Clock, class Duration>
77void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
78
79template <class Rep, class Period>
80void 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 Fiselierb6159752017-04-18 23:05:08 +000098#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +000099#include <tuple>
100#endif
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000101#include <__threading_support>
Eric Fiselier99245c52016-09-03 08:07:40 +0000102#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000104#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000106#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000107
Howard Hinnant29aba232010-08-21 21:01:59 +0000108#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantc51e1022010-05-11 19:42:16 +0000109
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000110#ifdef _LIBCPP_HAS_NO_THREADS
111#error <thread> is not supported on this single threaded system
112#else // !_LIBCPP_HAS_NO_THREADS
113
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114_LIBCPP_BEGIN_NAMESPACE_STD
115
Eric Fiselier80b0e152015-08-18 19:40:38 +0000116template <class _Tp> class __thread_specific_ptr;
117class _LIBCPP_TYPE_VIS __thread_struct;
118class _LIBCPP_HIDDEN __thread_struct_imp;
119class __assoc_sub_state;
120
121_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
122
123class _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&);
129public:
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 Hinnant3820f6b2010-08-27 20:10:19 +0000137template <class _Tp>
138class __thread_specific_ptr
139{
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000140 __libcpp_tls_key __key_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000141
Eric Fiselier80b0e152015-08-18 19:40:38 +0000142 // Only __thread_local_data() may construct a __thread_specific_ptr
143 // and only with _Tp == __thread_struct.
Eric Fiselierf34995b2015-08-19 03:38:41 +0000144 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier80b0e152015-08-18 19:40:38 +0000145 __thread_specific_ptr();
146 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
147
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000148 __thread_specific_ptr(const __thread_specific_ptr&);
149 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
150
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000151 static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
152
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000153public:
154 typedef _Tp* pointer;
155
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000156 ~__thread_specific_ptr();
157
Howard Hinnant186dca82010-09-23 17:31:07 +0000158 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000159 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
Howard Hinnant186dca82010-09-23 17:31:07 +0000160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000161 pointer operator*() const {return *get();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000163 pointer operator->() const {return get();}
Eric Fiselier99245c52016-09-03 08:07:40 +0000164 void set_pointer(pointer __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000165};
166
167template <class _Tp>
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000168void _LIBCPP_TLS_DESTRUCTOR_CC
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000169__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
170{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000171 delete static_cast<pointer>(__p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000172}
173
174template <class _Tp>
175__thread_specific_ptr<_Tp>::__thread_specific_ptr()
176{
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000177 int __ec =
178 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
179 if (__ec)
180 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000181}
182
183template <class _Tp>
184__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
185{
Eric Fiselier80b0e152015-08-18 19:40:38 +0000186 // __thread_specific_ptr is only created with a static storage duration
187 // so this destructor is only invoked during program termination. Invoking
188 // pthread_key_delete(__key_) may prevent other threads from deleting their
189 // thread local data. For this reason we leak the key.
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000190}
191
192template <class _Tp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000193void
Eric Fiselier99245c52016-09-03 08:07:40 +0000194__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000195{
Eric Fiselier99245c52016-09-03 08:07:40 +0000196 _LIBCPP_ASSERT(get() == nullptr,
197 "Attempting to overwrite thread local data");
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000198 __libcpp_tls_set(__key_, __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000199}
200
Howard Hinnant8331b762013-03-06 23:30:19 +0000201class _LIBCPP_TYPE_VIS thread;
202class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203
204namespace this_thread
205{
206
Howard Hinnanta54386e2012-09-14 00:39:16 +0000207_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209} // this_thread
210
Eric Fiselier6585c752016-04-21 22:54:21 +0000211template<> struct hash<__thread_id>;
Howard Hinnantad71c232011-12-05 00:08:45 +0000212
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000213class _LIBCPP_TEMPLATE_VIS __thread_id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214{
Howard Hinnant155c2af2010-05-24 17:49:41 +0000215 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
216 // NULL is the no-thread value on Darwin. Someone needs to check
217 // on other platforms. We assume 0 works everywhere for now.
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000218 __libcpp_thread_id __id_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219
220public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000222 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223
Howard Hinnant186dca82010-09-23 17:31:07 +0000224 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000225 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000226 {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000227 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000228 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 {return !(__x == __y);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000230 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000231 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000232 {return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000233 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000234 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 {return !(__y < __x);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000236 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000237 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238 {return __y < __x ;}
Howard Hinnant186dca82010-09-23 17:31:07 +0000239 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000240 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241 {return !(__x < __y);}
242
243 template<class _CharT, class _Traits>
244 friend
Howard Hinnant186dca82010-09-23 17:31:07 +0000245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246 basic_ostream<_CharT, _Traits>&
247 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
248 {return __os << __id.__id_;}
249
250private:
Howard Hinnant186dca82010-09-23 17:31:07 +0000251 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000252 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000253
Howard Hinnantd48ef422012-08-19 15:13:16 +0000254 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant8331b762013-03-06 23:30:19 +0000255 friend class _LIBCPP_TYPE_VIS thread;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000256 friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257};
258
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259template<>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000260struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261 : public unary_function<__thread_id, size_t>
262{
Howard Hinnant186dca82010-09-23 17:31:07 +0000263 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +0000264 size_t operator()(__thread_id __v) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000266 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267 }
268};
269
270namespace this_thread
271{
272
Howard Hinnant186dca82010-09-23 17:31:07 +0000273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274__thread_id
Howard Hinnant4bd34702012-07-21 16:50:47 +0000275get_id() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000277 return __libcpp_thread_get_current_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278}
279
280} // this_thread
281
Howard Hinnant8331b762013-03-06 23:30:19 +0000282class _LIBCPP_TYPE_VIS thread
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000284 __libcpp_thread_t __t_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285
Howard Hinnantaa87c0f2010-08-10 20:48:29 +0000286 thread(const thread&);
287 thread& operator=(const thread&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288public:
289 typedef __thread_id id;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000290 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnant186dca82010-09-23 17:31:07 +0000292 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000293 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
Eric Fiselierb6159752017-04-18 23:05:08 +0000294#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +0000295 template <class _Fp, class ..._Args,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296 class = typename enable_if
297 <
Howard Hinnantc834c512011-11-29 18:15:50 +0000298 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299 >::type
300 >
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000301 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc834c512011-11-29 18:15:50 +0000302 explicit thread(_Fp&& __f, _Args&&... __args);
Eric Fiselierb6159752017-04-18 23:05:08 +0000303#else // _LIBCPP_CXX03_LANG
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000304 template <class _Fp>
305 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
306 explicit thread(_Fp __f);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307#endif
308 ~thread();
309
Eric Fiselierb6159752017-04-18 23:05:08 +0000310#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000311 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000312 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000314 thread& operator=(thread&& __t) _NOEXCEPT;
Eric Fiselierb6159752017-04-18 23:05:08 +0000315#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316
Howard Hinnant186dca82010-09-23 17:31:07 +0000317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000318 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319
Howard Hinnant186dca82010-09-23 17:31:07 +0000320 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000321 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322 void join();
323 void detach();
Howard Hinnant186dca82010-09-23 17:31:07 +0000324 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000325 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000327 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328
Howard Hinnant4bd34702012-07-21 16:50:47 +0000329 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330};
331
Eric Fiselierb6159752017-04-18 23:05:08 +0000332#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000333
Eric Fiselierfd996062016-04-20 02:21:33 +0000334template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000335inline _LIBCPP_INLINE_VISIBILITY
336void
Eric Fiselierfd996062016-04-20 02:21:33 +0000337__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000338{
Eric Fiselierfd996062016-04-20 02:21:33 +0000339 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000340}
341
Howard Hinnantc834c512011-11-29 18:15:50 +0000342template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000343void* __thread_proxy(void* __vp)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000344{
Eric Fiselierfd996062016-04-20 02:21:33 +0000345 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
Howard Hinnantc834c512011-11-29 18:15:50 +0000346 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000347 __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000348 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Howard Hinnant3462e1e2013-04-03 20:29:45 +0000349 __thread_execute(*__p, _Index());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000350 return nullptr;
351}
352
Howard Hinnantc834c512011-11-29 18:15:50 +0000353template <class _Fp, class ..._Args,
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000354 class
355 >
Howard Hinnantc834c512011-11-29 18:15:50 +0000356thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000357{
Eric Fiselierfd996062016-04-20 02:21:33 +0000358 typedef unique_ptr<__thread_struct> _TSPtr;
359 _TSPtr __tsp(new __thread_struct);
360 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
361 _VSTD::unique_ptr<_Gp> __p(
362 new _Gp(std::move(__tsp),
363 __decay_copy(_VSTD::forward<_Fp>(__f)),
364 __decay_copy(_VSTD::forward<_Args>(__args))...));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000365 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000366 if (__ec == 0)
367 __p.release();
368 else
369 __throw_system_error(__ec, "thread constructor failed");
370}
371
Eric Fiselierb6159752017-04-18 23:05:08 +0000372inline
373thread&
374thread::operator=(thread&& __t) _NOEXCEPT
375{
376 if (!__libcpp_thread_isnull(&__t_))
377 terminate();
378 __t_ = __t.__t_;
379 __t.__t_ = _LIBCPP_NULL_THREAD;
380 return *this;
381}
382
383#else // _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000384
Howard Hinnantc834c512011-11-29 18:15:50 +0000385template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000386struct __thread_invoke_pair {
387 // This type is used to pass memory for thread local storage and a functor
388 // to a newly created thread because std::pair doesn't work with
389 // std::unique_ptr in C++03.
390 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
391 unique_ptr<__thread_struct> __tsp_;
392 _Fp __fn_;
393};
394
395template <class _Fp>
396void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397{
Howard Hinnantc834c512011-11-29 18:15:50 +0000398 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000399 __thread_local_data().set_pointer(__p->__tsp_.release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000400 (__p->__fn_)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401 return nullptr;
402}
403
Howard Hinnantc834c512011-11-29 18:15:50 +0000404template <class _Fp>
405thread::thread(_Fp __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406{
Eric Fiselierfd996062016-04-20 02:21:33 +0000407
408 typedef __thread_invoke_pair<_Fp> _InvokePair;
409 typedef std::unique_ptr<_InvokePair> _PairPtr;
410 _PairPtr __pp(new _InvokePair(__f));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000411 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 if (__ec == 0)
Eric Fiselierfd996062016-04-20 02:21:33 +0000413 __pp.release();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414 else
415 __throw_system_error(__ec, "thread constructor failed");
416}
417
Eric Fiselierb6159752017-04-18 23:05:08 +0000418#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
Howard Hinnant186dca82010-09-23 17:31:07 +0000420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000421void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423namespace this_thread
424{
425
Joerg Sonnenberger917a30a2017-02-09 14:12:29 +0000426_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427
428template <class _Rep, class _Period>
429void
430sleep_for(const chrono::duration<_Rep, _Period>& __d)
431{
432 using namespace chrono;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000433 if (__d > duration<_Rep, _Period>::zero())
434 {
435 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
436 nanoseconds __ns;
437 if (__d < _Max)
438 {
439 __ns = duration_cast<nanoseconds>(__d);
440 if (__ns < __d)
441 ++__ns;
442 }
443 else
444 __ns = nanoseconds::max();
445 sleep_for(__ns);
446 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447}
448
449template <class _Clock, class _Duration>
450void
451sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
452{
453 using namespace chrono;
454 mutex __mut;
455 condition_variable __cv;
456 unique_lock<mutex> __lk(__mut);
457 while (_Clock::now() < __t)
458 __cv.wait_until(__lk, __t);
459}
460
461template <class _Duration>
Howard Hinnant186dca82010-09-23 17:31:07 +0000462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000463void
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000464sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000465{
466 using namespace chrono;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000467 sleep_for(__t - steady_clock::now());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468}
469
Howard Hinnant186dca82010-09-23 17:31:07 +0000470inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000471void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000472
473} // this_thread
474
475_LIBCPP_END_NAMESPACE_STD
476
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000477#endif // !_LIBCPP_HAS_NO_THREADS
478
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479#endif // _LIBCPP_THREAD