blob: 0629d70efda40ae68a8f3c14d3b2277aa2791af4 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- thread -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_THREAD
12#define _LIBCPP_THREAD
13
14/*
15
16 thread synopsis
17
Howard Hinnanta785e4e2010-08-21 21:01:59 +000018#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-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 Hinnant6e1d8512012-07-21 16:50:47 +000029 thread() noexcept;
Howard Hinnantbc8d3f92010-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 Hinnant6e1d8512012-07-21 16:50:47 +000034 thread(thread&& t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035
36 thread& operator=(const thread&) = delete;
Howard Hinnant6e1d8512012-07-21 16:50:47 +000037 thread& operator=(thread&& t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000038
Howard Hinnant6e1d8512012-07-21 16:50:47 +000039 void swap(thread& t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040
Howard Hinnant6e1d8512012-07-21 16:50:47 +000041 bool joinable() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 void join();
43 void detach();
Howard Hinnant6e1d8512012-07-21 16:50:47 +000044 id get_id() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045 native_handle_type native_handle();
46
Howard Hinnant6e1d8512012-07-21 16:50:47 +000047 static unsigned hardware_concurrency() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048};
49
Howard Hinnant6e1d8512012-07-21 16:50:47 +000050void swap(thread& x, thread& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051
52class thread::id
53{
54public:
Howard Hinnant6e1d8512012-07-21 16:50:47 +000055 id() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056};
57
Howard Hinnant6e1d8512012-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 Hinnantbc8d3f92010-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 Hinnant6e1d8512012-07-21 16:50:47 +000072thread::id get_id() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
Howard Hinnant6e1d8512012-07-21 16:50:47 +000074void yield() noexcept;
Howard Hinnantbc8d3f92010-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 Fiselier690d9992017-04-18 23:05:08 +000098#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant656bdc32011-05-16 18:40:35 +000099#include <tuple>
100#endif
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000101#include <__threading_support>
Eric Fiselier7f735c32016-09-03 08:07:40 +0000102#include <__debug>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103
Howard Hinnant08e17472011-10-17 20:05:10 +0000104#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000106#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107
Eric Fiselier018a3d52017-05-31 22:07:49 +0000108_LIBCPP_PUSH_MACROS
109#include <__undef_macros>
110
Howard Hinnanta785e4e2010-08-21 21:01:59 +0000111#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000113#ifdef _LIBCPP_HAS_NO_THREADS
114#error <thread> is not supported on this single threaded system
115#else // !_LIBCPP_HAS_NO_THREADS
116
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117_LIBCPP_BEGIN_NAMESPACE_STD
118
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000119template <class _Tp> class __thread_specific_ptr;
120class _LIBCPP_TYPE_VIS __thread_struct;
121class _LIBCPP_HIDDEN __thread_struct_imp;
122class __assoc_sub_state;
123
124_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
125
126class _LIBCPP_TYPE_VIS __thread_struct
127{
128 __thread_struct_imp* __p_;
129
130 __thread_struct(const __thread_struct&);
131 __thread_struct& operator=(const __thread_struct&);
132public:
133 __thread_struct();
134 ~__thread_struct();
135
136 void notify_all_at_thread_exit(condition_variable*, mutex*);
137 void __make_ready_at_thread_exit(__assoc_sub_state*);
138};
139
Howard Hinnant47499b12010-08-27 20:10:19 +0000140template <class _Tp>
141class __thread_specific_ptr
142{
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000143 __libcpp_tls_key __key_;
Howard Hinnant47499b12010-08-27 20:10:19 +0000144
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000145 // Only __thread_local_data() may construct a __thread_specific_ptr
146 // and only with _Tp == __thread_struct.
Eric Fiselierf99b59c2015-08-19 03:38:41 +0000147 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000148 __thread_specific_ptr();
149 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
150
Howard Hinnant47499b12010-08-27 20:10:19 +0000151 __thread_specific_ptr(const __thread_specific_ptr&);
152 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
153
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +0000154 static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
155
Howard Hinnant47499b12010-08-27 20:10:19 +0000156public:
157 typedef _Tp* pointer;
158
Howard Hinnant47499b12010-08-27 20:10:19 +0000159 ~__thread_specific_ptr();
160
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000161 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000162 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000164 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000166 pointer operator->() const {return get();}
Eric Fiselier7f735c32016-09-03 08:07:40 +0000167 void set_pointer(pointer __p);
Howard Hinnant47499b12010-08-27 20:10:19 +0000168};
169
170template <class _Tp>
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +0000171void _LIBCPP_TLS_DESTRUCTOR_CC
Howard Hinnant47499b12010-08-27 20:10:19 +0000172__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
173{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000174 delete static_cast<pointer>(__p);
Howard Hinnant47499b12010-08-27 20:10:19 +0000175}
176
177template <class _Tp>
178__thread_specific_ptr<_Tp>::__thread_specific_ptr()
179{
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +0000180 int __ec =
181 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
182 if (__ec)
183 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
Howard Hinnant47499b12010-08-27 20:10:19 +0000184}
185
186template <class _Tp>
187__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
188{
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000189 // __thread_specific_ptr is only created with a static storage duration
190 // so this destructor is only invoked during program termination. Invoking
191 // pthread_key_delete(__key_) may prevent other threads from deleting their
192 // thread local data. For this reason we leak the key.
Howard Hinnant47499b12010-08-27 20:10:19 +0000193}
194
195template <class _Tp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000196void
Eric Fiselier7f735c32016-09-03 08:07:40 +0000197__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
Howard Hinnant47499b12010-08-27 20:10:19 +0000198{
Eric Fiselier7f735c32016-09-03 08:07:40 +0000199 _LIBCPP_ASSERT(get() == nullptr,
200 "Attempting to overwrite thread local data");
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000201 __libcpp_tls_set(__key_, __p);
Howard Hinnant47499b12010-08-27 20:10:19 +0000202}
203
Howard Hinnant83eade62013-03-06 23:30:19 +0000204class _LIBCPP_TYPE_VIS thread;
205class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206
207namespace this_thread
208{
209
Howard Hinnant33be35e2012-09-14 00:39:16 +0000210_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000211
212} // this_thread
213
Eric Fiselier566bcb42016-04-21 22:54:21 +0000214template<> struct hash<__thread_id>;
Howard Hinnant40c13d32011-12-05 00:08:45 +0000215
Eric Fiselierc3589a82017-01-04 23:56:00 +0000216class _LIBCPP_TEMPLATE_VIS __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217{
Howard Hinnantadff4892010-05-24 17:49:41 +0000218 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
219 // NULL is the no-thread value on Darwin. Someone needs to check
220 // on other platforms. We assume 0 works everywhere for now.
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000221 __libcpp_thread_id __id_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222
223public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000225 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000226
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000227 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000228 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000229 {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000230 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000231 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232 {return !(__x == __y);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000233 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000234 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000235 {return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000236 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000237 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238 {return !(__y < __x);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000239 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000240 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241 {return __y < __x ;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000242 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000243 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244 {return !(__x < __y);}
245
246 template<class _CharT, class _Traits>
247 friend
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 basic_ostream<_CharT, _Traits>&
250 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
251 {return __os << __id.__id_;}
252
253private:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000254 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000255 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256
Howard Hinnant96c60b42012-08-19 15:13:16 +0000257 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant83eade62013-03-06 23:30:19 +0000258 friend class _LIBCPP_TYPE_VIS thread;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000259 friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260};
261
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262template<>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000263struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264 : public unary_function<__thread_id, size_t>
265{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000266 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf552ba2017-03-23 02:40:28 +0000267 size_t operator()(__thread_id __v) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268 {
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000269 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270 }
271};
272
273namespace this_thread
274{
275
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277__thread_id
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000278get_id() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279{
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000280 return __libcpp_thread_get_current_id();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000281}
282
283} // this_thread
284
Howard Hinnant83eade62013-03-06 23:30:19 +0000285class _LIBCPP_TYPE_VIS thread
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286{
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000287 __libcpp_thread_t __t_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000289 thread(const thread&);
290 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291public:
292 typedef __thread_id id;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000293 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000295 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54 +0000296 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
Eric Fiselier690d9992017-04-18 23:05:08 +0000297#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant99968442011-11-29 18:15:50 +0000298 template <class _Fp, class ..._Args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000299 class = typename enable_if
300 <
Marshall Clow483bc7c2018-03-20 22:37:37 +0000301 !is_same<typename __uncvref<_Fp>::type, thread>::value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302 >::type
303 >
Shoaib Meenai6b734922017-03-02 03:22:18 +0000304 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnant99968442011-11-29 18:15:50 +0000305 explicit thread(_Fp&& __f, _Args&&... __args);
Eric Fiselier690d9992017-04-18 23:05:08 +0000306#else // _LIBCPP_CXX03_LANG
Shoaib Meenai6b734922017-03-02 03:22:18 +0000307 template <class _Fp>
308 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
309 explicit thread(_Fp __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310#endif
311 ~thread();
312
Eric Fiselier690d9992017-04-18 23:05:08 +0000313#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000314 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54 +0000315 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000317 thread& operator=(thread&& __t) _NOEXCEPT;
Eric Fiselier690d9992017-04-18 23:05:08 +0000318#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000321 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000322
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000323 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54 +0000324 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325 void join();
326 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000327 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000328 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000330 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000332 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333};
334
Eric Fiselier690d9992017-04-18 23:05:08 +0000335#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant656bdc32011-05-16 18:40:35 +0000336
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000337template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant656bdc32011-05-16 18:40:35 +0000338inline _LIBCPP_INLINE_VISIBILITY
339void
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000340__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000341{
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000342 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35 +0000343}
344
Howard Hinnant99968442011-11-29 18:15:50 +0000345template <class _Fp>
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000346void* __thread_proxy(void* __vp)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000347{
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000348 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
Howard Hinnant99968442011-11-29 18:15:50 +0000349 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier7f735c32016-09-03 08:07:40 +0000350 __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000351 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Howard Hinnantc5e89612013-04-03 20:29:45 +0000352 __thread_execute(*__p, _Index());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000353 return nullptr;
354}
355
Howard Hinnant99968442011-11-29 18:15:50 +0000356template <class _Fp, class ..._Args,
Howard Hinnant656bdc32011-05-16 18:40:35 +0000357 class
358 >
Howard Hinnant99968442011-11-29 18:15:50 +0000359thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000360{
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000361 typedef unique_ptr<__thread_struct> _TSPtr;
362 _TSPtr __tsp(new __thread_struct);
363 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
364 _VSTD::unique_ptr<_Gp> __p(
365 new _Gp(std::move(__tsp),
366 __decay_copy(_VSTD::forward<_Fp>(__f)),
367 __decay_copy(_VSTD::forward<_Args>(__args))...));
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000368 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000369 if (__ec == 0)
370 __p.release();
371 else
372 __throw_system_error(__ec, "thread constructor failed");
373}
374
Eric Fiselier690d9992017-04-18 23:05:08 +0000375inline
376thread&
377thread::operator=(thread&& __t) _NOEXCEPT
378{
379 if (!__libcpp_thread_isnull(&__t_))
380 terminate();
381 __t_ = __t.__t_;
382 __t.__t_ = _LIBCPP_NULL_THREAD;
383 return *this;
384}
385
386#else // _LIBCPP_CXX03_LANG
Howard Hinnant656bdc32011-05-16 18:40:35 +0000387
Howard Hinnant99968442011-11-29 18:15:50 +0000388template <class _Fp>
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000389struct __thread_invoke_pair {
390 // This type is used to pass memory for thread local storage and a functor
391 // to a newly created thread because std::pair doesn't work with
392 // std::unique_ptr in C++03.
393 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
394 unique_ptr<__thread_struct> __tsp_;
395 _Fp __fn_;
396};
397
398template <class _Fp>
399void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400{
Howard Hinnant99968442011-11-29 18:15:50 +0000401 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier7f735c32016-09-03 08:07:40 +0000402 __thread_local_data().set_pointer(__p->__tsp_.release());
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000403 (__p->__fn_)();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 return nullptr;
405}
406
Howard Hinnant99968442011-11-29 18:15:50 +0000407template <class _Fp>
408thread::thread(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409{
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000410
411 typedef __thread_invoke_pair<_Fp> _InvokePair;
412 typedef std::unique_ptr<_InvokePair> _PairPtr;
413 _PairPtr __pp(new _InvokePair(__f));
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000414 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415 if (__ec == 0)
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000416 __pp.release();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417 else
418 __throw_system_error(__ec, "thread constructor failed");
419}
420
Eric Fiselier690d9992017-04-18 23:05:08 +0000421#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000423inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000424void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426namespace this_thread
427{
428
Joerg Sonnenberger5a0c9c32017-02-09 14:12:29 +0000429_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430
431template <class _Rep, class _Period>
432void
433sleep_for(const chrono::duration<_Rep, _Period>& __d)
434{
435 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000436 if (__d > duration<_Rep, _Period>::zero())
437 {
438 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
439 nanoseconds __ns;
440 if (__d < _Max)
441 {
442 __ns = duration_cast<nanoseconds>(__d);
443 if (__ns < __d)
444 ++__ns;
445 }
446 else
447 __ns = nanoseconds::max();
448 sleep_for(__ns);
449 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450}
451
452template <class _Clock, class _Duration>
453void
454sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
455{
456 using namespace chrono;
457 mutex __mut;
458 condition_variable __cv;
459 unique_lock<mutex> __lk(__mut);
460 while (_Clock::now() < __t)
461 __cv.wait_until(__lk, __t);
462}
463
464template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000465inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466void
Howard Hinnantf8f85212010-11-20 19:16:30 +0000467sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468{
469 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30 +0000470 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471}
472
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000473inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000474void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475
476} // this_thread
477
478_LIBCPP_END_NAMESPACE_STD
479
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000480#endif // !_LIBCPP_HAS_NO_THREADS
481
Eric Fiselier018a3d52017-05-31 22:07:49 +0000482_LIBCPP_POP_MACROS
483
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484#endif // _LIBCPP_THREAD