blob: 808d76b82a09394b37929d486f7ded0e0dd2040e [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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101#include <pthread.h>
102
Howard Hinnant08e17472011-10-17 20:05:10 +0000103#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000105#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106
Howard Hinnanta785e4e2010-08-21 21:01:59 +0000107#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000108
Jonathan Roelofs8d86b2e2014-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000113_LIBCPP_BEGIN_NAMESPACE_STD
114
Howard Hinnant47499b12010-08-27 20:10:19 +0000115template <class _Tp>
116class __thread_specific_ptr
117{
118 pthread_key_t __key_;
119
120 __thread_specific_ptr(const __thread_specific_ptr&);
121 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
122
123 static void __at_thread_exit(void*);
124public:
125 typedef _Tp* pointer;
126
127 __thread_specific_ptr();
128 ~__thread_specific_ptr();
129
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000131 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000133 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000135 pointer operator->() const {return get();}
136 pointer release();
137 void reset(pointer __p = nullptr);
138};
139
140template <class _Tp>
141void
142__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
143{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000144 delete static_cast<pointer>(__p);
Howard Hinnant47499b12010-08-27 20:10:19 +0000145}
146
147template <class _Tp>
148__thread_specific_ptr<_Tp>::__thread_specific_ptr()
149{
150 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
Howard Hinnant2c0d3ed2013-03-28 15:00:04 +0000151#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000152 if (__ec)
153 throw system_error(error_code(__ec, system_category()),
154 "__thread_specific_ptr construction failed");
Howard Hinnant2c0d3ed2013-03-28 15:00:04 +0000155#endif
Howard Hinnant47499b12010-08-27 20:10:19 +0000156}
157
158template <class _Tp>
159__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
160{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000161 pthread_key_delete(__key_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000162}
163
164template <class _Tp>
165typename __thread_specific_ptr<_Tp>::pointer
166__thread_specific_ptr<_Tp>::release()
167{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000168 pointer __p = get();
169 pthread_setspecific(__key_, 0);
170 return __p;
Howard Hinnant47499b12010-08-27 20:10:19 +0000171}
172
173template <class _Tp>
174void
175__thread_specific_ptr<_Tp>::reset(pointer __p)
176{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000177 pointer __p_old = get();
178 pthread_setspecific(__key_, __p);
179 delete __p_old;
Howard Hinnant47499b12010-08-27 20:10:19 +0000180}
181
Howard Hinnant83eade62013-03-06 23:30:19 +0000182class _LIBCPP_TYPE_VIS thread;
183class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184
185namespace this_thread
186{
187
Howard Hinnant33be35e2012-09-14 00:39:16 +0000188_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189
190} // this_thread
191
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000192template<> struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnant40c13d32011-12-05 00:08:45 +0000193
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000194class _LIBCPP_TYPE_VIS_ONLY __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195{
Howard Hinnantadff4892010-05-24 17:49:41 +0000196 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
197 // NULL is the no-thread value on Darwin. Someone needs to check
198 // on other platforms. We assume 0 works everywhere for now.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 pthread_t __id_;
200
201public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000203 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000205 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000206 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207 {return __x.__id_ == __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000208 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000209 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210 {return !(__x == __y);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000211 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000212 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213 {return __x.__id_ < __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000214 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000215 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216 {return !(__y < __x);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000217 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000218 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219 {return __y < __x ;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000220 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000221 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 {return !(__x < __y);}
223
224 template<class _CharT, class _Traits>
225 friend
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227 basic_ostream<_CharT, _Traits>&
228 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
229 {return __os << __id.__id_;}
230
231private:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233 __thread_id(pthread_t __id) : __id_(__id) {}
234
Howard Hinnant96c60b42012-08-19 15:13:16 +0000235 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant83eade62013-03-06 23:30:19 +0000236 friend class _LIBCPP_TYPE_VIS thread;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000237 friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238};
239
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240template<>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000241struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242 : public unary_function<__thread_id, size_t>
243{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245 size_t operator()(__thread_id __v) const
246 {
Howard Hinnant40c13d32011-12-05 00:08:45 +0000247 return hash<pthread_t>()(__v.__id_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248 }
249};
250
251namespace this_thread
252{
253
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000254inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255__thread_id
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000256get_id() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257{
258 return pthread_self();
259}
260
261} // this_thread
262
Howard Hinnant83eade62013-03-06 23:30:19 +0000263class _LIBCPP_TYPE_VIS thread
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264{
265 pthread_t __t_;
266
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000267 thread(const thread&);
268 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269public:
270 typedef __thread_id id;
271 typedef pthread_t native_handle_type;
272
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000274 thread() _NOEXCEPT : __t_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50 +0000276 template <class _Fp, class ..._Args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277 class = typename enable_if
278 <
Howard Hinnant99968442011-11-29 18:15:50 +0000279 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280 >::type
281 >
Howard Hinnant99968442011-11-29 18:15:50 +0000282 explicit thread(_Fp&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000283#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50 +0000284 template <class _Fp> explicit thread(_Fp __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285#endif
286 ~thread();
287
Howard Hinnant73d21a42010-09-04 23:28:19 +0000288#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000290 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
291 thread& operator=(thread&& __t) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000292#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000295 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000298 bool joinable() const _NOEXCEPT {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000299 void join();
300 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000302 id get_id() const _NOEXCEPT {return __t_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000304 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000305
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000306 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000307};
308
Howard Hinnant47499b12010-08-27 20:10:19 +0000309class __assoc_sub_state;
310
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000311class _LIBCPP_HIDDEN __thread_struct_imp;
Howard Hinnant47499b12010-08-27 20:10:19 +0000312
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000313class _LIBCPP_TYPE_VIS __thread_struct
Howard Hinnant47499b12010-08-27 20:10:19 +0000314{
315 __thread_struct_imp* __p_;
316
317 __thread_struct(const __thread_struct&);
318 __thread_struct& operator=(const __thread_struct&);
319public:
320 __thread_struct();
321 ~__thread_struct();
322
Howard Hinnante6e4d012010-09-03 21:46:37 +0000323 void notify_all_at_thread_exit(condition_variable*, mutex*);
Howard Hinnant47499b12010-08-27 20:10:19 +0000324 void __make_ready_at_thread_exit(__assoc_sub_state*);
325};
326
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000327_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
Howard Hinnant47499b12010-08-27 20:10:19 +0000328
Howard Hinnant656bdc32011-05-16 18:40:35 +0000329#ifndef _LIBCPP_HAS_NO_VARIADICS
330
Howard Hinnant99968442011-11-29 18:15:50 +0000331template <class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant656bdc32011-05-16 18:40:35 +0000332inline _LIBCPP_INLINE_VISIBILITY
333void
Howard Hinnantc5e89612013-04-03 20:29:45 +0000334__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000335{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000336 __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35 +0000337}
338
Howard Hinnant99968442011-11-29 18:15:50 +0000339template <class _Fp>
Howard Hinnant656bdc32011-05-16 18:40:35 +0000340void*
341__thread_proxy(void* __vp)
342{
343 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50 +0000344 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
345 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnantc5e89612013-04-03 20:29:45 +0000346 __thread_execute(*__p, _Index());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000347 return nullptr;
348}
349
Howard Hinnant99968442011-11-29 18:15:50 +0000350template <class _Fp, class ..._Args,
Howard Hinnant656bdc32011-05-16 18:40:35 +0000351 class
352 >
Howard Hinnant99968442011-11-29 18:15:50 +0000353thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000354{
Howard Hinnant99968442011-11-29 18:15:50 +0000355 typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
356 _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000357 __decay_copy(_VSTD::forward<_Args>(__args))...));
Howard Hinnant99968442011-11-29 18:15:50 +0000358 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000359 if (__ec == 0)
360 __p.release();
361 else
362 __throw_system_error(__ec, "thread constructor failed");
363}
364
365#else // _LIBCPP_HAS_NO_VARIADICS
366
Howard Hinnant99968442011-11-29 18:15:50 +0000367template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368void*
369__thread_proxy(void* __vp)
370{
Howard Hinnant5306d682010-10-14 19:18:04 +0000371 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50 +0000372 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373 (*__p)();
374 return nullptr;
375}
376
Howard Hinnant99968442011-11-29 18:15:50 +0000377template <class _Fp>
378thread::thread(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379{
Howard Hinnant99968442011-11-29 18:15:50 +0000380 std::unique_ptr<_Fp> __p(new _Fp(__f));
381 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000382 if (__ec == 0)
383 __p.release();
384 else
385 __throw_system_error(__ec, "thread constructor failed");
386}
387
Howard Hinnant324bb032010-08-22 00:02:43 +0000388#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389
Howard Hinnant73d21a42010-09-04 23:28:19 +0000390#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000392inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393thread&
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000394thread::operator=(thread&& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395{
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000396 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397 terminate();
398 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000399 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400 return *this;
401}
402
Howard Hinnant73d21a42010-09-04 23:28:19 +0000403#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000405inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000406void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408namespace this_thread
409{
410
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000411_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412
413template <class _Rep, class _Period>
414void
415sleep_for(const chrono::duration<_Rep, _Period>& __d)
416{
417 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000418 if (__d > duration<_Rep, _Period>::zero())
419 {
420 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
421 nanoseconds __ns;
422 if (__d < _Max)
423 {
424 __ns = duration_cast<nanoseconds>(__d);
425 if (__ns < __d)
426 ++__ns;
427 }
428 else
429 __ns = nanoseconds::max();
430 sleep_for(__ns);
431 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432}
433
434template <class _Clock, class _Duration>
435void
436sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
437{
438 using namespace chrono;
439 mutex __mut;
440 condition_variable __cv;
441 unique_lock<mutex> __lk(__mut);
442 while (_Clock::now() < __t)
443 __cv.wait_until(__lk, __t);
444}
445
446template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000447inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448void
Howard Hinnantf8f85212010-11-20 19:16:30 +0000449sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450{
451 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30 +0000452 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453}
454
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000455inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000456void yield() _NOEXCEPT {sched_yield();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457
458} // this_thread
459
460_LIBCPP_END_NAMESPACE_STD
461
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000462#endif // !_LIBCPP_HAS_NO_THREADS
463
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000464#endif // _LIBCPP_THREAD