blob: 3dde390261c719bf4599abc307a2069634803ec3 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- thread -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// 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:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_THREAD
12#define _LIBCPP_THREAD
13
14/*
15
16 thread synopsis
17
Howard Hinnanta785e4e2010-08-21 21:01:5918#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:1619
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:4729 thread() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1630 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:4734 thread(thread&& t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1635
36 thread& operator=(const thread&) = delete;
Howard Hinnant6e1d8512012-07-21 16:50:4737 thread& operator=(thread&& t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1638
Howard Hinnant6e1d8512012-07-21 16:50:4739 void swap(thread& t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1640
Howard Hinnant6e1d8512012-07-21 16:50:4741 bool joinable() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1642 void join();
43 void detach();
Howard Hinnant6e1d8512012-07-21 16:50:4744 id get_id() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1645 native_handle_type native_handle();
46
Howard Hinnant6e1d8512012-07-21 16:50:4747 static unsigned hardware_concurrency() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1648};
49
Howard Hinnant6e1d8512012-07-21 16:50:4750void swap(thread& x, thread& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1651
52class thread::id
53{
54public:
Howard Hinnant6e1d8512012-07-21 16:50:4755 id() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1656};
57
Howard Hinnant6e1d8512012-07-21 16:50:4758bool 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:1664
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:4772thread::id get_id() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673
Howard Hinnant6e1d8512012-07-21 16:50:4774void yield() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1675
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:3598#ifndef _LIBCPP_HAS_NO_VARIADICS
99#include <tuple>
100#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16101#include <pthread.h>
Sergey Dmitroukfff544e2014-12-08 14:50:21102#include <sched.h>
Howard Hinnantbc8d3f92010-05-11 19:42:16103
Howard Hinnant08e17472011-10-17 20:05:10104#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16105#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10106#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16107
Howard Hinnanta785e4e2010-08-21 21:01:59108#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:16109
Jonathan Roelofs8d86b2e2014-09-05 19:45:05110#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:16114_LIBCPP_BEGIN_NAMESPACE_STD
115
Eric Fiselier0376dfa2015-08-18 19:40:38116template <class _Tp> class __thread_specific_ptr;
117class _LIBCPP_TYPE_VIS __thread_struct;
118class _LIBCPP_HIDDEN __thread_struct_imp;
119class __assoc_sub_state;
120
121_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
122
123class _LIBCPP_TYPE_VIS __thread_struct
124{
125 __thread_struct_imp* __p_;
126
127 __thread_struct(const __thread_struct&);
128 __thread_struct& operator=(const __thread_struct&);
129public:
130 __thread_struct();
131 ~__thread_struct();
132
133 void notify_all_at_thread_exit(condition_variable*, mutex*);
134 void __make_ready_at_thread_exit(__assoc_sub_state*);
135};
136
Howard Hinnant47499b12010-08-27 20:10:19137template <class _Tp>
138class __thread_specific_ptr
139{
140 pthread_key_t __key_;
141
Eric Fiselier0376dfa2015-08-18 19:40:38142 // Only __thread_local_data() may construct a __thread_specific_ptr
143 // and only with _Tp == __thread_struct.
Eric Fiselierf99b59c2015-08-19 03:38:41144 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier0376dfa2015-08-18 19:40:38145 __thread_specific_ptr();
146 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
147
Howard Hinnant47499b12010-08-27 20:10:19148 __thread_specific_ptr(const __thread_specific_ptr&);
149 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
150
151 static void __at_thread_exit(void*);
152public:
153 typedef _Tp* pointer;
154
Howard Hinnant47499b12010-08-27 20:10:19155 ~__thread_specific_ptr();
156
Howard Hinnant8d7a9552010-09-23 17:31:07157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19158 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19160 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19162 pointer operator->() const {return get();}
163 pointer release();
164 void reset(pointer __p = nullptr);
165};
166
167template <class _Tp>
168void
169__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
170{
Howard Hinnant8db4aca2011-10-17 20:08:59171 delete static_cast<pointer>(__p);
Howard Hinnant47499b12010-08-27 20:10:19172}
173
174template <class _Tp>
175__thread_specific_ptr<_Tp>::__thread_specific_ptr()
176{
177 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
Howard Hinnant2c0d3ed2013-03-28 15:00:04178#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8db4aca2011-10-17 20:08:59179 if (__ec)
180 throw system_error(error_code(__ec, system_category()),
181 "__thread_specific_ptr construction failed");
Howard Hinnant2c0d3ed2013-03-28 15:00:04182#endif
Howard Hinnant47499b12010-08-27 20:10:19183}
184
185template <class _Tp>
186__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
187{
Eric Fiselier0376dfa2015-08-18 19:40:38188 // __thread_specific_ptr is only created with a static storage duration
189 // so this destructor is only invoked during program termination. Invoking
190 // pthread_key_delete(__key_) may prevent other threads from deleting their
191 // thread local data. For this reason we leak the key.
Howard Hinnant47499b12010-08-27 20:10:19192}
193
194template <class _Tp>
195typename __thread_specific_ptr<_Tp>::pointer
196__thread_specific_ptr<_Tp>::release()
197{
Howard Hinnant8db4aca2011-10-17 20:08:59198 pointer __p = get();
199 pthread_setspecific(__key_, 0);
200 return __p;
Howard Hinnant47499b12010-08-27 20:10:19201}
202
203template <class _Tp>
204void
205__thread_specific_ptr<_Tp>::reset(pointer __p)
206{
Howard Hinnant8db4aca2011-10-17 20:08:59207 pointer __p_old = get();
208 pthread_setspecific(__key_, __p);
209 delete __p_old;
Howard Hinnant47499b12010-08-27 20:10:19210}
211
Howard Hinnant83eade62013-03-06 23:30:19212class _LIBCPP_TYPE_VIS thread;
213class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantbc8d3f92010-05-11 19:42:16214
215namespace this_thread
216{
217
Howard Hinnant33be35e2012-09-14 00:39:16218_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16219
220} // this_thread
221
Howard Hinnant0f678bd2013-08-12 18:38:34222template<> struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnant40c13d32011-12-05 00:08:45223
Howard Hinnant0f678bd2013-08-12 18:38:34224class _LIBCPP_TYPE_VIS_ONLY __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16225{
Howard Hinnantadff4892010-05-24 17:49:41226 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
227 // NULL is the no-thread value on Darwin. Someone needs to check
228 // on other platforms. We assume 0 works everywhere for now.
Howard Hinnantbc8d3f92010-05-11 19:42:16229 pthread_t __id_;
230
231public:
Howard Hinnant8d7a9552010-09-23 17:31:07232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47233 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16234
Howard Hinnant8d7a9552010-09-23 17:31:07235 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47236 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16237 {return __x.__id_ == __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07238 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47239 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16240 {return !(__x == __y);}
Howard Hinnant8d7a9552010-09-23 17:31:07241 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47242 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16243 {return __x.__id_ < __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07244 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47245 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16246 {return !(__y < __x);}
Howard Hinnant8d7a9552010-09-23 17:31:07247 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47248 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16249 {return __y < __x ;}
Howard Hinnant8d7a9552010-09-23 17:31:07250 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47251 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16252 {return !(__x < __y);}
253
254 template<class _CharT, class _Traits>
255 friend
Howard Hinnant8d7a9552010-09-23 17:31:07256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16257 basic_ostream<_CharT, _Traits>&
258 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
259 {return __os << __id.__id_;}
260
261private:
Howard Hinnant8d7a9552010-09-23 17:31:07262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16263 __thread_id(pthread_t __id) : __id_(__id) {}
264
Howard Hinnant96c60b42012-08-19 15:13:16265 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant83eade62013-03-06 23:30:19266 friend class _LIBCPP_TYPE_VIS thread;
Howard Hinnant0f678bd2013-08-12 18:38:34267 friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnantbc8d3f92010-05-11 19:42:16268};
269
Howard Hinnantbc8d3f92010-05-11 19:42:16270template<>
Howard Hinnant0f678bd2013-08-12 18:38:34271struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16272 : public unary_function<__thread_id, size_t>
273{
Howard Hinnant8d7a9552010-09-23 17:31:07274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16275 size_t operator()(__thread_id __v) const
276 {
Howard Hinnant40c13d32011-12-05 00:08:45277 return hash<pthread_t>()(__v.__id_);
Howard Hinnantbc8d3f92010-05-11 19:42:16278 }
279};
280
281namespace this_thread
282{
283
Howard Hinnant8d7a9552010-09-23 17:31:07284inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16285__thread_id
Howard Hinnant6e1d8512012-07-21 16:50:47286get_id() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16287{
288 return pthread_self();
289}
290
291} // this_thread
292
Howard Hinnant83eade62013-03-06 23:30:19293class _LIBCPP_TYPE_VIS thread
Howard Hinnantbc8d3f92010-05-11 19:42:16294{
295 pthread_t __t_;
296
Howard Hinnant60a0a8e2010-08-10 20:48:29297 thread(const thread&);
298 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16299public:
300 typedef __thread_id id;
301 typedef pthread_t native_handle_type;
302
Howard Hinnant8d7a9552010-09-23 17:31:07303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47304 thread() _NOEXCEPT : __t_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16305#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50306 template <class _Fp, class ..._Args,
Howard Hinnantbc8d3f92010-05-11 19:42:16307 class = typename enable_if
308 <
Howard Hinnant99968442011-11-29 18:15:50309 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantbc8d3f92010-05-11 19:42:16310 >::type
311 >
Howard Hinnant99968442011-11-29 18:15:50312 explicit thread(_Fp&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43313#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50314 template <class _Fp> explicit thread(_Fp __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16315#endif
316 ~thread();
317
Howard Hinnant73d21a42010-09-04 23:28:19318#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47320 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
321 thread& operator=(thread&& __t) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19322#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16323
Howard Hinnant8d7a9552010-09-23 17:31:07324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47325 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16326
Howard Hinnant8d7a9552010-09-23 17:31:07327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47328 bool joinable() const _NOEXCEPT {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16329 void join();
330 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47332 id get_id() const _NOEXCEPT {return __t_;}
Howard Hinnant8d7a9552010-09-23 17:31:07333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47334 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16335
Howard Hinnant6e1d8512012-07-21 16:50:47336 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16337};
338
Howard Hinnant656bdc32011-05-16 18:40:35339#ifndef _LIBCPP_HAS_NO_VARIADICS
340
Howard Hinnant99968442011-11-29 18:15:50341template <class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant656bdc32011-05-16 18:40:35342inline _LIBCPP_INLINE_VISIBILITY
343void
Howard Hinnantc5e89612013-04-03 20:29:45344__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant656bdc32011-05-16 18:40:35345{
Howard Hinnant0949eed2011-06-30 21:18:19346 __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35347}
348
Howard Hinnant99968442011-11-29 18:15:50349template <class _Fp>
Howard Hinnant656bdc32011-05-16 18:40:35350void*
351__thread_proxy(void* __vp)
352{
353 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50354 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
355 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnantc5e89612013-04-03 20:29:45356 __thread_execute(*__p, _Index());
Howard Hinnant656bdc32011-05-16 18:40:35357 return nullptr;
358}
359
Howard Hinnant99968442011-11-29 18:15:50360template <class _Fp, class ..._Args,
Howard Hinnant656bdc32011-05-16 18:40:35361 class
362 >
Howard Hinnant99968442011-11-29 18:15:50363thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant656bdc32011-05-16 18:40:35364{
Howard Hinnant99968442011-11-29 18:15:50365 typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
366 _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19367 __decay_copy(_VSTD::forward<_Args>(__args))...));
Howard Hinnant99968442011-11-29 18:15:50368 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant656bdc32011-05-16 18:40:35369 if (__ec == 0)
370 __p.release();
371 else
372 __throw_system_error(__ec, "thread constructor failed");
373}
374
375#else // _LIBCPP_HAS_NO_VARIADICS
376
Howard Hinnant99968442011-11-29 18:15:50377template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16378void*
379__thread_proxy(void* __vp)
380{
Howard Hinnant5306d682010-10-14 19:18:04381 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50382 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Howard Hinnantbc8d3f92010-05-11 19:42:16383 (*__p)();
384 return nullptr;
385}
386
Howard Hinnant99968442011-11-29 18:15:50387template <class _Fp>
388thread::thread(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16389{
Howard Hinnant99968442011-11-29 18:15:50390 std::unique_ptr<_Fp> __p(new _Fp(__f));
391 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16392 if (__ec == 0)
393 __p.release();
394 else
395 __throw_system_error(__ec, "thread constructor failed");
396}
397
Howard Hinnant324bb032010-08-22 00:02:43398#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16399
Howard Hinnant73d21a42010-09-04 23:28:19400#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16401
Howard Hinnant8d7a9552010-09-23 17:31:07402inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16403thread&
Howard Hinnant6e1d8512012-07-21 16:50:47404thread::operator=(thread&& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16405{
Howard Hinnanta6a062d2010-06-02 18:20:39406 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16407 terminate();
408 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39409 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16410 return *this;
411}
412
Howard Hinnant73d21a42010-09-04 23:28:19413#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16414
Howard Hinnant8d7a9552010-09-23 17:31:07415inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47416void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16417
Howard Hinnantbc8d3f92010-05-11 19:42:16418namespace this_thread
419{
420
Howard Hinnant0f678bd2013-08-12 18:38:34421_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16422
423template <class _Rep, class _Period>
424void
425sleep_for(const chrono::duration<_Rep, _Period>& __d)
426{
427 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33428 if (__d > duration<_Rep, _Period>::zero())
429 {
430 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
431 nanoseconds __ns;
432 if (__d < _Max)
433 {
434 __ns = duration_cast<nanoseconds>(__d);
435 if (__ns < __d)
436 ++__ns;
437 }
438 else
439 __ns = nanoseconds::max();
440 sleep_for(__ns);
441 }
Howard Hinnantbc8d3f92010-05-11 19:42:16442}
443
444template <class _Clock, class _Duration>
445void
446sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
447{
448 using namespace chrono;
449 mutex __mut;
450 condition_variable __cv;
451 unique_lock<mutex> __lk(__mut);
452 while (_Clock::now() < __t)
453 __cv.wait_until(__lk, __t);
454}
455
456template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07457inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16458void
Howard Hinnantf8f85212010-11-20 19:16:30459sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16460{
461 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30462 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16463}
464
Howard Hinnant8d7a9552010-09-23 17:31:07465inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47466void yield() _NOEXCEPT {sched_yield();}
Howard Hinnantbc8d3f92010-05-11 19:42:16467
468} // this_thread
469
470_LIBCPP_END_NAMESPACE_STD
471
Jonathan Roelofs8d86b2e2014-09-05 19:45:05472#endif // !_LIBCPP_HAS_NO_THREADS
473
Howard Hinnantbc8d3f92010-05-11 19:42:16474#endif // _LIBCPP_THREAD