blob: 8a30102f427e2b2e8a81328907535af16a82c8b4 [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>
Howard Hinnant656bdc32011-05-16 18:40:35 +000098#ifndef _LIBCPP_HAS_NO_VARIADICS
99#include <tuple>
100#endif
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700101#include <pthread.h>
102#include <sched.h>
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
Howard Hinnanta785e4e2010-08-21 21:01:59 +0000108#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109
Jonathan Roelofs8d86b2e2014-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000114_LIBCPP_BEGIN_NAMESPACE_STD
115
Howard Hinnant47499b12010-08-27 20:10:19 +0000116template <class _Tp>
117class __thread_specific_ptr
118{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700119 pthread_key_t __key_;
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000120
Howard Hinnant47499b12010-08-27 20:10:19 +0000121 __thread_specific_ptr(const __thread_specific_ptr&);
122 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
123
124 static void __at_thread_exit(void*);
125public:
126 typedef _Tp* pointer;
127
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700128 __thread_specific_ptr();
Howard Hinnant47499b12010-08-27 20:10:19 +0000129 ~__thread_specific_ptr();
130
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000131 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700132 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000134 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000136 pointer operator->() const {return get();}
137 pointer release();
138 void reset(pointer __p = nullptr);
139};
140
141template <class _Tp>
142void
143__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
144{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000145 delete static_cast<pointer>(__p);
Howard Hinnant47499b12010-08-27 20:10:19 +0000146}
147
148template <class _Tp>
149__thread_specific_ptr<_Tp>::__thread_specific_ptr()
150{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700151 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
Howard Hinnant2c0d3ed2013-03-28 15:00:04 +0000152#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000153 if (__ec)
154 throw system_error(error_code(__ec, system_category()),
155 "__thread_specific_ptr construction failed");
Howard Hinnant2c0d3ed2013-03-28 15:00:04 +0000156#endif
Howard Hinnant47499b12010-08-27 20:10:19 +0000157}
158
159template <class _Tp>
160__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
161{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700162 pthread_key_delete(__key_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000163}
164
165template <class _Tp>
166typename __thread_specific_ptr<_Tp>::pointer
167__thread_specific_ptr<_Tp>::release()
168{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000169 pointer __p = get();
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700170 pthread_setspecific(__key_, 0);
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000171 return __p;
Howard Hinnant47499b12010-08-27 20:10:19 +0000172}
173
174template <class _Tp>
175void
176__thread_specific_ptr<_Tp>::reset(pointer __p)
177{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000178 pointer __p_old = get();
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700179 pthread_setspecific(__key_, __p);
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000180 delete __p_old;
Howard Hinnant47499b12010-08-27 20:10:19 +0000181}
182
Howard Hinnant83eade62013-03-06 23:30:19 +0000183class _LIBCPP_TYPE_VIS thread;
184class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185
186namespace this_thread
187{
188
Howard Hinnant33be35e2012-09-14 00:39:16 +0000189_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190
191} // this_thread
192
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700193template<> struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnant40c13d32011-12-05 00:08:45 +0000194
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000195class _LIBCPP_TYPE_VIS_ONLY __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000196{
Howard Hinnantadff4892010-05-24 17:49:41 +0000197 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
198 // NULL is the no-thread value on Darwin. Someone needs to check
199 // on other platforms. We assume 0 works everywhere for now.
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700200 pthread_t __id_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201
202public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000204 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000206 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000207 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700208 {return __x.__id_ == __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000209 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000210 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000211 {return !(__x == __y);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000212 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000213 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700214 {return __x.__id_ < __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000215 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000216 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 {return !(__y < __x);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000218 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000219 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220 {return __y < __x ;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000221 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000222 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223 {return !(__x < __y);}
224
225 template<class _CharT, class _Traits>
226 friend
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228 basic_ostream<_CharT, _Traits>&
229 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
230 {return __os << __id.__id_;}
231
232private:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000233 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700234 __thread_id(pthread_t __id) : __id_(__id) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
Howard Hinnant96c60b42012-08-19 15:13:16 +0000236 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant83eade62013-03-06 23:30:19 +0000237 friend class _LIBCPP_TYPE_VIS thread;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000238 friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000239};
240
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241template<>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000242struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 : public unary_function<__thread_id, size_t>
244{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246 size_t operator()(__thread_id __v) const
247 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700248 return hash<pthread_t>()(__v.__id_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 }
250};
251
252namespace this_thread
253{
254
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000255inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256__thread_id
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000257get_id() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700259 return pthread_self();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260}
261
262} // this_thread
263
Howard Hinnant83eade62013-03-06 23:30:19 +0000264class _LIBCPP_TYPE_VIS thread
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700266 pthread_t __t_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000268 thread(const thread&);
269 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270public:
271 typedef __thread_id id;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700272 typedef pthread_t native_handle_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000275 thread() _NOEXCEPT : __t_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50 +0000277 template <class _Fp, class ..._Args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278 class = typename enable_if
279 <
Howard Hinnant99968442011-11-29 18:15:50 +0000280 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000281 >::type
282 >
Howard Hinnant99968442011-11-29 18:15:50 +0000283 explicit thread(_Fp&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000284#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50 +0000285 template <class _Fp> explicit thread(_Fp __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286#endif
287 ~thread();
288
Howard Hinnant73d21a42010-09-04 23:28:19 +0000289#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000291 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
292 thread& operator=(thread&& __t) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000293#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000296 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000297
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000299 bool joinable() const _NOEXCEPT {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300 void join();
301 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000302 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700303 id get_id() const _NOEXCEPT {return __t_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000305 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000307 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308};
309
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700310class __assoc_sub_state;
311
312class _LIBCPP_HIDDEN __thread_struct_imp;
313
314class _LIBCPP_TYPE_VIS __thread_struct
315{
316 __thread_struct_imp* __p_;
317
318 __thread_struct(const __thread_struct&);
319 __thread_struct& operator=(const __thread_struct&);
320public:
321 __thread_struct();
322 ~__thread_struct();
323
324 void notify_all_at_thread_exit(condition_variable*, mutex*);
325 void __make_ready_at_thread_exit(__assoc_sub_state*);
326};
327
328_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
329
Howard Hinnant656bdc32011-05-16 18:40:35 +0000330#ifndef _LIBCPP_HAS_NO_VARIADICS
331
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700332template <class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant656bdc32011-05-16 18:40:35 +0000333inline _LIBCPP_INLINE_VISIBILITY
334void
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700335__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000336{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700337 __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35 +0000338}
339
Howard Hinnant99968442011-11-29 18:15:50 +0000340template <class _Fp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700341void*
342__thread_proxy(void* __vp)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000343{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700344 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50 +0000345 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700346 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnantc5e89612013-04-03 20:29:45 +0000347 __thread_execute(*__p, _Index());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000348 return nullptr;
349}
350
Howard Hinnant99968442011-11-29 18:15:50 +0000351template <class _Fp, class ..._Args,
Howard Hinnant656bdc32011-05-16 18:40:35 +0000352 class
353 >
Howard Hinnant99968442011-11-29 18:15:50 +0000354thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000355{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700356 typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
357 _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
358 __decay_copy(_VSTD::forward<_Args>(__args))...));
359 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000360 if (__ec == 0)
361 __p.release();
362 else
363 __throw_system_error(__ec, "thread constructor failed");
364}
365
366#else // _LIBCPP_HAS_NO_VARIADICS
367
Howard Hinnant99968442011-11-29 18:15:50 +0000368template <class _Fp>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700369void*
370__thread_proxy(void* __vp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700372 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50 +0000373 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700374 (*__p)();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375 return nullptr;
376}
377
Howard Hinnant99968442011-11-29 18:15:50 +0000378template <class _Fp>
379thread::thread(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000380{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700381 std::unique_ptr<_Fp> __p(new _Fp(__f));
382 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 if (__ec == 0)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700384 __p.release();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 else
386 __throw_system_error(__ec, "thread constructor failed");
387}
388
Howard Hinnant324bb032010-08-22 00:02:43 +0000389#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390
Howard Hinnant73d21a42010-09-04 23:28:19 +0000391#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700393inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394thread&
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000395thread::operator=(thread&& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396{
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000397 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398 terminate();
399 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000400 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401 return *this;
402}
403
Howard Hinnant73d21a42010-09-04 23:28:19 +0000404#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000406inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000407void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409namespace this_thread
410{
411
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000412_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413
414template <class _Rep, class _Period>
415void
416sleep_for(const chrono::duration<_Rep, _Period>& __d)
417{
418 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000419 if (__d > duration<_Rep, _Period>::zero())
420 {
421 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
422 nanoseconds __ns;
423 if (__d < _Max)
424 {
425 __ns = duration_cast<nanoseconds>(__d);
426 if (__ns < __d)
427 ++__ns;
428 }
429 else
430 __ns = nanoseconds::max();
431 sleep_for(__ns);
432 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433}
434
435template <class _Clock, class _Duration>
436void
437sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
438{
439 using namespace chrono;
440 mutex __mut;
441 condition_variable __cv;
442 unique_lock<mutex> __lk(__mut);
443 while (_Clock::now() < __t)
444 __cv.wait_until(__lk, __t);
445}
446
447template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000448inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449void
Howard Hinnantf8f85212010-11-20 19:16:30 +0000450sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451{
452 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30 +0000453 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454}
455
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000456inline _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700457void yield() _NOEXCEPT {sched_yield();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458
459} // this_thread
460
461_LIBCPP_END_NAMESPACE_STD
462
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000463#endif // !_LIBCPP_HAS_NO_THREADS
464
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465#endif // _LIBCPP_THREAD