blob: 022021c5addee66c5e0e27e1db332d19999c53cf [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>
Howard Hinnant4eea16b2011-05-16 18:40:35 +000098#ifndef _LIBCPP_HAS_NO_VARIADICS
99#include <tuple>
100#endif
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000101#include <__threading_support>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000103#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000105#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106
Howard Hinnant29aba232010-08-21 21:01:59 +0000107#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000109#ifdef _LIBCPP_HAS_NO_THREADS
110#error <thread> is not supported on this single threaded system
111#else // !_LIBCPP_HAS_NO_THREADS
112
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113_LIBCPP_BEGIN_NAMESPACE_STD
114
Eric Fiselier80b0e152015-08-18 19:40:38 +0000115template <class _Tp> class __thread_specific_ptr;
116class _LIBCPP_TYPE_VIS __thread_struct;
117class _LIBCPP_HIDDEN __thread_struct_imp;
118class __assoc_sub_state;
119
120_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
121
122class _LIBCPP_TYPE_VIS __thread_struct
123{
124 __thread_struct_imp* __p_;
125
126 __thread_struct(const __thread_struct&);
127 __thread_struct& operator=(const __thread_struct&);
128public:
129 __thread_struct();
130 ~__thread_struct();
131
132 void notify_all_at_thread_exit(condition_variable*, mutex*);
133 void __make_ready_at_thread_exit(__assoc_sub_state*);
134};
135
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000136template <class _Tp>
137class __thread_specific_ptr
138{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000139 __libcpp_tl_key __key_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000140
Eric Fiselier80b0e152015-08-18 19:40:38 +0000141 // Only __thread_local_data() may construct a __thread_specific_ptr
142 // and only with _Tp == __thread_struct.
Eric Fiselierf34995b2015-08-19 03:38:41 +0000143 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier80b0e152015-08-18 19:40:38 +0000144 __thread_specific_ptr();
145 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
146
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000147 __thread_specific_ptr(const __thread_specific_ptr&);
148 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
149
150 static void __at_thread_exit(void*);
151public:
152 typedef _Tp* pointer;
153
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000154 ~__thread_specific_ptr();
155
Howard Hinnant186dca82010-09-23 17:31:07 +0000156 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000157 pointer get() const {return static_cast<_Tp*>(__libcpp_tl_get(__key_));}
Howard Hinnant186dca82010-09-23 17:31:07 +0000158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000159 pointer operator*() const {return *get();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000161 pointer operator->() const {return get();}
162 pointer release();
163 void reset(pointer __p = nullptr);
164};
165
166template <class _Tp>
167void
168__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
169{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000170 delete static_cast<pointer>(__p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000171}
172
173template <class _Tp>
174__thread_specific_ptr<_Tp>::__thread_specific_ptr()
175{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000176 int __ec = __libcpp_tl_create(
177 &__key_,
178 &__thread_specific_ptr::__at_thread_exit);
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000179 if (__ec)
Eric Fiselier20159752016-05-30 23:15:19 +0000180 __throw_system_error(__ec,
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000181 "__thread_specific_ptr construction failed");
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000182}
183
184template <class _Tp>
185__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
186{
Eric Fiselier80b0e152015-08-18 19:40:38 +0000187 // __thread_specific_ptr is only created with a static storage duration
188 // so this destructor is only invoked during program termination. Invoking
189 // pthread_key_delete(__key_) may prevent other threads from deleting their
190 // thread local data. For this reason we leak the key.
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000191}
192
193template <class _Tp>
194typename __thread_specific_ptr<_Tp>::pointer
195__thread_specific_ptr<_Tp>::release()
196{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000197 pointer __p = get();
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000198 __libcpp_tl_set(__key_, nullptr);
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000199 return __p;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000200}
201
202template <class _Tp>
203void
204__thread_specific_ptr<_Tp>::reset(pointer __p)
205{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000206 pointer __p_old = get();
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000207 __libcpp_tl_set(__key_, __p);
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000208 delete __p_old;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000209}
210
Howard Hinnant8331b762013-03-06 23:30:19 +0000211class _LIBCPP_TYPE_VIS thread;
212class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213
214namespace this_thread
215{
216
Howard Hinnanta54386e2012-09-14 00:39:16 +0000217_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218
219} // this_thread
220
Eric Fiselier6585c752016-04-21 22:54:21 +0000221template<> struct hash<__thread_id>;
Howard Hinnantad71c232011-12-05 00:08:45 +0000222
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000223class _LIBCPP_TYPE_VIS_ONLY __thread_id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224{
Howard Hinnant155c2af2010-05-24 17:49:41 +0000225 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
226 // NULL is the no-thread value on Darwin. Someone needs to check
227 // on other platforms. We assume 0 works everywhere for now.
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000228 __libcpp_thread_id __id_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229
230public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000232 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233
Howard Hinnant186dca82010-09-23 17:31:07 +0000234 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000235 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000236 {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000237 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000238 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 {return !(__x == __y);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000240 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000241 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000242 {return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000243 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000244 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245 {return !(__y < __x);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000246 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000247 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248 {return __y < __x ;}
Howard Hinnant186dca82010-09-23 17:31:07 +0000249 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000250 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251 {return !(__x < __y);}
252
253 template<class _CharT, class _Traits>
254 friend
Howard Hinnant186dca82010-09-23 17:31:07 +0000255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 basic_ostream<_CharT, _Traits>&
257 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
258 {return __os << __id.__id_;}
259
260private:
Howard Hinnant186dca82010-09-23 17:31:07 +0000261 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000262 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263
Howard Hinnantd48ef422012-08-19 15:13:16 +0000264 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant8331b762013-03-06 23:30:19 +0000265 friend class _LIBCPP_TYPE_VIS thread;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000266 friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267};
268
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269template<>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000270struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000271 : public unary_function<__thread_id, size_t>
272{
Howard Hinnant186dca82010-09-23 17:31:07 +0000273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274 size_t operator()(__thread_id __v) const
275 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000276 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277 }
278};
279
280namespace this_thread
281{
282
Howard Hinnant186dca82010-09-23 17:31:07 +0000283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284__thread_id
Howard Hinnant4bd34702012-07-21 16:50:47 +0000285get_id() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000287 return __libcpp_thread_get_current_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288}
289
290} // this_thread
291
Howard Hinnant8331b762013-03-06 23:30:19 +0000292class _LIBCPP_TYPE_VIS thread
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000294 __libcpp_thread_t __t_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295
Howard Hinnantaa87c0f2010-08-10 20:48:29 +0000296 thread(const thread&);
297 thread& operator=(const thread&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298public:
299 typedef __thread_id id;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000300 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301
Howard Hinnant186dca82010-09-23 17:31:07 +0000302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000303 thread() _NOEXCEPT : __t_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc834c512011-11-29 18:15:50 +0000305 template <class _Fp, class ..._Args,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306 class = typename enable_if
307 <
Howard Hinnantc834c512011-11-29 18:15:50 +0000308 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309 >::type
310 >
Howard Hinnantc834c512011-11-29 18:15:50 +0000311 explicit thread(_Fp&& __f, _Args&&... __args);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000312#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc834c512011-11-29 18:15:50 +0000313 template <class _Fp> explicit thread(_Fp __f);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314#endif
315 ~thread();
316
Howard Hinnant74279a52010-09-04 23:28:19 +0000317#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant186dca82010-09-23 17:31:07 +0000318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000319 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000321 thread& operator=(thread&& __t) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +0000322#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323
Howard Hinnant186dca82010-09-23 17:31:07 +0000324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000325 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326
Howard Hinnant186dca82010-09-23 17:31:07 +0000327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000328 bool joinable() const _NOEXCEPT {return __t_ != 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329 void join();
330 void detach();
Howard Hinnant186dca82010-09-23 17:31:07 +0000331 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000332 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000334 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000335
Howard Hinnant4bd34702012-07-21 16:50:47 +0000336 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337};
338
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000339#ifndef _LIBCPP_HAS_NO_VARIADICS
340
Eric Fiselierfd996062016-04-20 02:21:33 +0000341template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000342inline _LIBCPP_INLINE_VISIBILITY
343void
Eric Fiselierfd996062016-04-20 02:21:33 +0000344__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000345{
Eric Fiselierfd996062016-04-20 02:21:33 +0000346 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000347}
348
Howard Hinnantc834c512011-11-29 18:15:50 +0000349template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000350void* __thread_proxy(void* __vp)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000351{
Eric Fiselierfd996062016-04-20 02:21:33 +0000352 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
Howard Hinnantc834c512011-11-29 18:15:50 +0000353 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselierfd996062016-04-20 02:21:33 +0000354 __thread_local_data().reset(_VSTD::get<0>(*__p).release());
355 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Howard Hinnant3462e1e2013-04-03 20:29:45 +0000356 __thread_execute(*__p, _Index());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000357 return nullptr;
358}
359
Howard Hinnantc834c512011-11-29 18:15:50 +0000360template <class _Fp, class ..._Args,
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000361 class
362 >
Howard Hinnantc834c512011-11-29 18:15:50 +0000363thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000364{
Eric Fiselierfd996062016-04-20 02:21:33 +0000365 typedef unique_ptr<__thread_struct> _TSPtr;
366 _TSPtr __tsp(new __thread_struct);
367 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
368 _VSTD::unique_ptr<_Gp> __p(
369 new _Gp(std::move(__tsp),
370 __decay_copy(_VSTD::forward<_Fp>(__f)),
371 __decay_copy(_VSTD::forward<_Args>(__args))...));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000372 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000373 if (__ec == 0)
374 __p.release();
375 else
376 __throw_system_error(__ec, "thread constructor failed");
377}
378
379#else // _LIBCPP_HAS_NO_VARIADICS
380
Howard Hinnantc834c512011-11-29 18:15:50 +0000381template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000382struct __thread_invoke_pair {
383 // This type is used to pass memory for thread local storage and a functor
384 // to a newly created thread because std::pair doesn't work with
385 // std::unique_ptr in C++03.
386 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
387 unique_ptr<__thread_struct> __tsp_;
388 _Fp __fn_;
389};
390
391template <class _Fp>
392void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393{
Howard Hinnantc834c512011-11-29 18:15:50 +0000394 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselierfd996062016-04-20 02:21:33 +0000395 __thread_local_data().reset(__p->__tsp_.release());
396 (__p->__fn_)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397 return nullptr;
398}
399
Howard Hinnantc834c512011-11-29 18:15:50 +0000400template <class _Fp>
401thread::thread(_Fp __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402{
Eric Fiselierfd996062016-04-20 02:21:33 +0000403
404 typedef __thread_invoke_pair<_Fp> _InvokePair;
405 typedef std::unique_ptr<_InvokePair> _PairPtr;
406 _PairPtr __pp(new _InvokePair(__f));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000407 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408 if (__ec == 0)
Eric Fiselierfd996062016-04-20 02:21:33 +0000409 __pp.release();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410 else
411 __throw_system_error(__ec, "thread constructor failed");
412}
413
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000414#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415
Howard Hinnant74279a52010-09-04 23:28:19 +0000416#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000418inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419thread&
Howard Hinnant4bd34702012-07-21 16:50:47 +0000420thread::operator=(thread&& __t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421{
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000422 if (__t_ != 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423 terminate();
424 __t_ = __t.__t_;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000425 __t.__t_ = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426 return *this;
427}
428
Howard Hinnant74279a52010-09-04 23:28:19 +0000429#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430
Howard Hinnant186dca82010-09-23 17:31:07 +0000431inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000432void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434namespace this_thread
435{
436
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000437_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438
439template <class _Rep, class _Period>
440void
441sleep_for(const chrono::duration<_Rep, _Period>& __d)
442{
443 using namespace chrono;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000444 if (__d > duration<_Rep, _Period>::zero())
445 {
446 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
447 nanoseconds __ns;
448 if (__d < _Max)
449 {
450 __ns = duration_cast<nanoseconds>(__d);
451 if (__ns < __d)
452 ++__ns;
453 }
454 else
455 __ns = nanoseconds::max();
456 sleep_for(__ns);
457 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000458}
459
460template <class _Clock, class _Duration>
461void
462sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
463{
464 using namespace chrono;
465 mutex __mut;
466 condition_variable __cv;
467 unique_lock<mutex> __lk(__mut);
468 while (_Clock::now() < __t)
469 __cv.wait_until(__lk, __t);
470}
471
472template <class _Duration>
Howard Hinnant186dca82010-09-23 17:31:07 +0000473inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474void
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000475sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000476{
477 using namespace chrono;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000478 sleep_for(__t - steady_clock::now());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479}
480
Howard Hinnant186dca82010-09-23 17:31:07 +0000481inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000482void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000483
484} // this_thread
485
486_LIBCPP_END_NAMESPACE_STD
487
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000488#endif // !_LIBCPP_HAS_NO_THREADS
489
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490#endif // _LIBCPP_THREAD