blob: 94e7ab6c54ca4f76c0650c26b994353b09b3b96e [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
109_LIBCPP_BEGIN_NAMESPACE_STD
110
Howard Hinnant47499b12010-08-27 20:10:19 +0000111template <class _Tp>
112class __thread_specific_ptr
113{
114 pthread_key_t __key_;
115
116 __thread_specific_ptr(const __thread_specific_ptr&);
117 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
118
119 static void __at_thread_exit(void*);
120public:
121 typedef _Tp* pointer;
122
123 __thread_specific_ptr();
124 ~__thread_specific_ptr();
125
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000127 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000129 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000131 pointer operator->() const {return get();}
132 pointer release();
133 void reset(pointer __p = nullptr);
134};
135
136template <class _Tp>
137void
138__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
139{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000140 delete static_cast<pointer>(__p);
Howard Hinnant47499b12010-08-27 20:10:19 +0000141}
142
143template <class _Tp>
144__thread_specific_ptr<_Tp>::__thread_specific_ptr()
145{
146 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000147 if (__ec)
148 throw system_error(error_code(__ec, system_category()),
149 "__thread_specific_ptr construction failed");
Howard Hinnant47499b12010-08-27 20:10:19 +0000150}
151
152template <class _Tp>
153__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
154{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000155 pthread_key_delete(__key_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000156}
157
158template <class _Tp>
159typename __thread_specific_ptr<_Tp>::pointer
160__thread_specific_ptr<_Tp>::release()
161{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000162 pointer __p = get();
163 pthread_setspecific(__key_, 0);
164 return __p;
Howard Hinnant47499b12010-08-27 20:10:19 +0000165}
166
167template <class _Tp>
168void
169__thread_specific_ptr<_Tp>::reset(pointer __p)
170{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000171 pointer __p_old = get();
172 pthread_setspecific(__key_, __p);
173 delete __p_old;
Howard Hinnant47499b12010-08-27 20:10:19 +0000174}
175
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176class thread;
177class __thread_id;
178
179namespace this_thread
180{
181
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000182__thread_id get_id() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000183
184} // this_thread
185
Howard Hinnant40c13d32011-12-05 00:08:45 +0000186class _LIBCPP_VISIBLE __thread_id;
187template<> struct _LIBCPP_VISIBLE hash<__thread_id>;
188
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000189class _LIBCPP_VISIBLE __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190{
Howard Hinnantadff4892010-05-24 17:49:41 +0000191 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
192 // NULL is the no-thread value on Darwin. Someone needs to check
193 // on other platforms. We assume 0 works everywhere for now.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194 pthread_t __id_;
195
196public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000198 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000200 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000201 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 {return __x.__id_ == __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000203 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000204 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205 {return !(__x == __y);}
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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208 {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 !(__y < __x);}
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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214 {return __y < __x ;}
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 !(__x < __y);}
218
219 template<class _CharT, class _Traits>
220 friend
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 basic_ostream<_CharT, _Traits>&
223 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
224 {return __os << __id.__id_;}
225
226private:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228 __thread_id(pthread_t __id) : __id_(__id) {}
229
Howard Hinnant96c60b42012-08-19 15:13:16 +0000230 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000231 friend class _LIBCPP_VISIBLE thread;
Howard Hinnant40c13d32011-12-05 00:08:45 +0000232 friend struct _LIBCPP_VISIBLE hash<__thread_id>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233};
234
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235template<>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000236struct _LIBCPP_VISIBLE hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237 : public unary_function<__thread_id, size_t>
238{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240 size_t operator()(__thread_id __v) const
241 {
Howard Hinnant40c13d32011-12-05 00:08:45 +0000242 return hash<pthread_t>()(__v.__id_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 }
244};
245
246namespace this_thread
247{
248
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000249inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250__thread_id
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000251get_id() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252{
253 return pthread_self();
254}
255
256} // this_thread
257
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000258class _LIBCPP_VISIBLE thread
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259{
260 pthread_t __t_;
261
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000262 thread(const thread&);
263 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264public:
265 typedef __thread_id id;
266 typedef pthread_t native_handle_type;
267
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000269 thread() _NOEXCEPT : __t_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50 +0000271 template <class _Fp, class ..._Args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272 class = typename enable_if
273 <
Howard Hinnant99968442011-11-29 18:15:50 +0000274 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275 >::type
276 >
Howard Hinnant99968442011-11-29 18:15:50 +0000277 explicit thread(_Fp&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000278#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50 +0000279 template <class _Fp> explicit thread(_Fp __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280#endif
281 ~thread();
282
Howard Hinnant73d21a42010-09-04 23:28:19 +0000283#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000285 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
286 thread& operator=(thread&& __t) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000287#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000290 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000293 bool joinable() const _NOEXCEPT {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294 void join();
295 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000297 id get_id() const _NOEXCEPT {return __t_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000299 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000301 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302};
303
Howard Hinnant47499b12010-08-27 20:10:19 +0000304class __assoc_sub_state;
305
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000306class _LIBCPP_HIDDEN __thread_struct_imp;
Howard Hinnant47499b12010-08-27 20:10:19 +0000307
308class __thread_struct
309{
310 __thread_struct_imp* __p_;
311
312 __thread_struct(const __thread_struct&);
313 __thread_struct& operator=(const __thread_struct&);
314public:
315 __thread_struct();
316 ~__thread_struct();
317
Howard Hinnante6e4d012010-09-03 21:46:37 +0000318 void notify_all_at_thread_exit(condition_variable*, mutex*);
Howard Hinnant47499b12010-08-27 20:10:19 +0000319 void __make_ready_at_thread_exit(__assoc_sub_state*);
320};
321
Howard Hinnant5306d682010-10-14 19:18:04 +0000322__thread_specific_ptr<__thread_struct>& __thread_local_data();
Howard Hinnant47499b12010-08-27 20:10:19 +0000323
Howard Hinnant656bdc32011-05-16 18:40:35 +0000324#ifndef _LIBCPP_HAS_NO_VARIADICS
325
Howard Hinnant99968442011-11-29 18:15:50 +0000326template <class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant656bdc32011-05-16 18:40:35 +0000327inline _LIBCPP_INLINE_VISIBILITY
328void
Howard Hinnant99968442011-11-29 18:15:50 +0000329__threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000330{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000331 __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35 +0000332}
333
Howard Hinnant99968442011-11-29 18:15:50 +0000334template <class _Fp>
Howard Hinnant656bdc32011-05-16 18:40:35 +0000335void*
336__thread_proxy(void* __vp)
337{
338 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50 +0000339 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
340 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnant656bdc32011-05-16 18:40:35 +0000341 __threaad_execute(*__p, _Index());
342 return nullptr;
343}
344
Howard Hinnant99968442011-11-29 18:15:50 +0000345template <class _Fp, class ..._Args,
Howard Hinnant656bdc32011-05-16 18:40:35 +0000346 class
347 >
Howard Hinnant99968442011-11-29 18:15:50 +0000348thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000349{
Howard Hinnant99968442011-11-29 18:15:50 +0000350 typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
351 _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000352 __decay_copy(_VSTD::forward<_Args>(__args))...));
Howard Hinnant99968442011-11-29 18:15:50 +0000353 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000354 if (__ec == 0)
355 __p.release();
356 else
357 __throw_system_error(__ec, "thread constructor failed");
358}
359
360#else // _LIBCPP_HAS_NO_VARIADICS
361
Howard Hinnant99968442011-11-29 18:15:50 +0000362template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000363void*
364__thread_proxy(void* __vp)
365{
Howard Hinnant5306d682010-10-14 19:18:04 +0000366 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50 +0000367 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368 (*__p)();
369 return nullptr;
370}
371
Howard Hinnant99968442011-11-29 18:15:50 +0000372template <class _Fp>
373thread::thread(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374{
Howard Hinnant99968442011-11-29 18:15:50 +0000375 std::unique_ptr<_Fp> __p(new _Fp(__f));
376 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 if (__ec == 0)
378 __p.release();
379 else
380 __throw_system_error(__ec, "thread constructor failed");
381}
382
Howard Hinnant324bb032010-08-22 00:02:43 +0000383#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
Howard Hinnant73d21a42010-09-04 23:28:19 +0000385#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000387inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388thread&
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000389thread::operator=(thread&& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390{
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000391 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392 terminate();
393 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000394 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395 return *this;
396}
397
Howard Hinnant73d21a42010-09-04 23:28:19 +0000398#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000400inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000401void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403namespace this_thread
404{
405
406void sleep_for(const chrono::nanoseconds& ns);
407
408template <class _Rep, class _Period>
409void
410sleep_for(const chrono::duration<_Rep, _Period>& __d)
411{
412 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000413 if (__d > duration<_Rep, _Period>::zero())
414 {
415 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
416 nanoseconds __ns;
417 if (__d < _Max)
418 {
419 __ns = duration_cast<nanoseconds>(__d);
420 if (__ns < __d)
421 ++__ns;
422 }
423 else
424 __ns = nanoseconds::max();
425 sleep_for(__ns);
426 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427}
428
429template <class _Clock, class _Duration>
430void
431sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
432{
433 using namespace chrono;
434 mutex __mut;
435 condition_variable __cv;
436 unique_lock<mutex> __lk(__mut);
437 while (_Clock::now() < __t)
438 __cv.wait_until(__lk, __t);
439}
440
441template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000442inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443void
Howard Hinnantf8f85212010-11-20 19:16:30 +0000444sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445{
446 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30 +0000447 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448}
449
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000451void yield() _NOEXCEPT {sched_yield();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000452
453} // this_thread
454
455_LIBCPP_END_NAMESPACE_STD
456
457#endif // _LIBCPP_THREAD