blob: 1f1e4a2b9459cc2b2fe742934b78fafd6e33870a [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>
102
Howard Hinnant08e17472011-10-17 20:05:10103#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16104#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10105#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16106
Howard Hinnanta785e4e2010-08-21 21:01:59107#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:16108
109_LIBCPP_BEGIN_NAMESPACE_STD
110
Howard Hinnant47499b12010-08-27 20:10:19111template <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:07126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19127 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19129 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19131 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:59140 delete static_cast<pointer>(__p);
Howard Hinnant47499b12010-08-27 20:10:19141}
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 Hinnant2c0d3ed2013-03-28 15:00:04147#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8db4aca2011-10-17 20:08:59148 if (__ec)
149 throw system_error(error_code(__ec, system_category()),
150 "__thread_specific_ptr construction failed");
Howard Hinnant2c0d3ed2013-03-28 15:00:04151#endif
Howard Hinnant47499b12010-08-27 20:10:19152}
153
154template <class _Tp>
155__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
156{
Howard Hinnant8db4aca2011-10-17 20:08:59157 pthread_key_delete(__key_);
Howard Hinnant47499b12010-08-27 20:10:19158}
159
160template <class _Tp>
161typename __thread_specific_ptr<_Tp>::pointer
162__thread_specific_ptr<_Tp>::release()
163{
Howard Hinnant8db4aca2011-10-17 20:08:59164 pointer __p = get();
165 pthread_setspecific(__key_, 0);
166 return __p;
Howard Hinnant47499b12010-08-27 20:10:19167}
168
169template <class _Tp>
170void
171__thread_specific_ptr<_Tp>::reset(pointer __p)
172{
Howard Hinnant8db4aca2011-10-17 20:08:59173 pointer __p_old = get();
174 pthread_setspecific(__key_, __p);
175 delete __p_old;
Howard Hinnant47499b12010-08-27 20:10:19176}
177
Howard Hinnant83eade62013-03-06 23:30:19178class _LIBCPP_TYPE_VIS thread;
179class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantbc8d3f92010-05-11 19:42:16180
181namespace this_thread
182{
183
Howard Hinnant33be35e2012-09-14 00:39:16184_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16185
186} // this_thread
187
Howard Hinnant0f678bd2013-08-12 18:38:34188template<> struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnant40c13d32011-12-05 00:08:45189
Howard Hinnant0f678bd2013-08-12 18:38:34190class _LIBCPP_TYPE_VIS_ONLY __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16191{
Howard Hinnantadff4892010-05-24 17:49:41192 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
193 // NULL is the no-thread value on Darwin. Someone needs to check
194 // on other platforms. We assume 0 works everywhere for now.
Howard Hinnantbc8d3f92010-05-11 19:42:16195 pthread_t __id_;
196
197public:
Howard Hinnant8d7a9552010-09-23 17:31:07198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47199 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16200
Howard Hinnant8d7a9552010-09-23 17:31:07201 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47202 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16203 {return __x.__id_ == __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07204 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47205 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16206 {return !(__x == __y);}
Howard Hinnant8d7a9552010-09-23 17:31:07207 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47208 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16209 {return __x.__id_ < __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07210 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47211 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16212 {return !(__y < __x);}
Howard Hinnant8d7a9552010-09-23 17:31:07213 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47214 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16215 {return __y < __x ;}
Howard Hinnant8d7a9552010-09-23 17:31:07216 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47217 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16218 {return !(__x < __y);}
219
220 template<class _CharT, class _Traits>
221 friend
Howard Hinnant8d7a9552010-09-23 17:31:07222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16223 basic_ostream<_CharT, _Traits>&
224 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
225 {return __os << __id.__id_;}
226
227private:
Howard Hinnant8d7a9552010-09-23 17:31:07228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16229 __thread_id(pthread_t __id) : __id_(__id) {}
230
Howard Hinnant96c60b42012-08-19 15:13:16231 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant83eade62013-03-06 23:30:19232 friend class _LIBCPP_TYPE_VIS thread;
Howard Hinnant0f678bd2013-08-12 18:38:34233 friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnantbc8d3f92010-05-11 19:42:16234};
235
Howard Hinnantbc8d3f92010-05-11 19:42:16236template<>
Howard Hinnant0f678bd2013-08-12 18:38:34237struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16238 : public unary_function<__thread_id, size_t>
239{
Howard Hinnant8d7a9552010-09-23 17:31:07240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16241 size_t operator()(__thread_id __v) const
242 {
Howard Hinnant40c13d32011-12-05 00:08:45243 return hash<pthread_t>()(__v.__id_);
Howard Hinnantbc8d3f92010-05-11 19:42:16244 }
245};
246
247namespace this_thread
248{
249
Howard Hinnant8d7a9552010-09-23 17:31:07250inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16251__thread_id
Howard Hinnant6e1d8512012-07-21 16:50:47252get_id() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16253{
254 return pthread_self();
255}
256
257} // this_thread
258
Howard Hinnant83eade62013-03-06 23:30:19259class _LIBCPP_TYPE_VIS thread
Howard Hinnantbc8d3f92010-05-11 19:42:16260{
261 pthread_t __t_;
262
Howard Hinnant60a0a8e2010-08-10 20:48:29263 thread(const thread&);
264 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16265public:
266 typedef __thread_id id;
267 typedef pthread_t native_handle_type;
268
Howard Hinnant8d7a9552010-09-23 17:31:07269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47270 thread() _NOEXCEPT : __t_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16271#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50272 template <class _Fp, class ..._Args,
Howard Hinnantbc8d3f92010-05-11 19:42:16273 class = typename enable_if
274 <
Howard Hinnant99968442011-11-29 18:15:50275 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantbc8d3f92010-05-11 19:42:16276 >::type
277 >
Howard Hinnant99968442011-11-29 18:15:50278 explicit thread(_Fp&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43279#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50280 template <class _Fp> explicit thread(_Fp __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16281#endif
282 ~thread();
283
Howard Hinnant73d21a42010-09-04 23:28:19284#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47286 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
287 thread& operator=(thread&& __t) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19288#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16289
Howard Hinnant8d7a9552010-09-23 17:31:07290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47291 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16292
Howard Hinnant8d7a9552010-09-23 17:31:07293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47294 bool joinable() const _NOEXCEPT {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16295 void join();
296 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47298 id get_id() const _NOEXCEPT {return __t_;}
Howard Hinnant8d7a9552010-09-23 17:31:07299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47300 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16301
Howard Hinnant6e1d8512012-07-21 16:50:47302 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16303};
304
Howard Hinnant47499b12010-08-27 20:10:19305class __assoc_sub_state;
306
Howard Hinnant2d72b1e2010-12-17 14:46:43307class _LIBCPP_HIDDEN __thread_struct_imp;
Howard Hinnant47499b12010-08-27 20:10:19308
Howard Hinnant0f678bd2013-08-12 18:38:34309class _LIBCPP_TYPE_VIS __thread_struct
Howard Hinnant47499b12010-08-27 20:10:19310{
311 __thread_struct_imp* __p_;
312
313 __thread_struct(const __thread_struct&);
314 __thread_struct& operator=(const __thread_struct&);
315public:
316 __thread_struct();
317 ~__thread_struct();
318
Howard Hinnante6e4d012010-09-03 21:46:37319 void notify_all_at_thread_exit(condition_variable*, mutex*);
Howard Hinnant47499b12010-08-27 20:10:19320 void __make_ready_at_thread_exit(__assoc_sub_state*);
321};
322
Howard Hinnant0f678bd2013-08-12 18:38:34323_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
Howard Hinnant47499b12010-08-27 20:10:19324
Howard Hinnant656bdc32011-05-16 18:40:35325#ifndef _LIBCPP_HAS_NO_VARIADICS
326
Howard Hinnant99968442011-11-29 18:15:50327template <class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant656bdc32011-05-16 18:40:35328inline _LIBCPP_INLINE_VISIBILITY
329void
Howard Hinnantc5e89612013-04-03 20:29:45330__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant656bdc32011-05-16 18:40:35331{
Howard Hinnant0949eed2011-06-30 21:18:19332 __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35333}
334
Howard Hinnant99968442011-11-29 18:15:50335template <class _Fp>
Howard Hinnant656bdc32011-05-16 18:40:35336void*
337__thread_proxy(void* __vp)
338{
339 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50340 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
341 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnantc5e89612013-04-03 20:29:45342 __thread_execute(*__p, _Index());
Howard Hinnant656bdc32011-05-16 18:40:35343 return nullptr;
344}
345
Howard Hinnant99968442011-11-29 18:15:50346template <class _Fp, class ..._Args,
Howard Hinnant656bdc32011-05-16 18:40:35347 class
348 >
Howard Hinnant99968442011-11-29 18:15:50349thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant656bdc32011-05-16 18:40:35350{
Howard Hinnant99968442011-11-29 18:15:50351 typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
352 _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19353 __decay_copy(_VSTD::forward<_Args>(__args))...));
Howard Hinnant99968442011-11-29 18:15:50354 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant656bdc32011-05-16 18:40:35355 if (__ec == 0)
356 __p.release();
357 else
358 __throw_system_error(__ec, "thread constructor failed");
359}
360
361#else // _LIBCPP_HAS_NO_VARIADICS
362
Howard Hinnant99968442011-11-29 18:15:50363template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16364void*
365__thread_proxy(void* __vp)
366{
Howard Hinnant5306d682010-10-14 19:18:04367 __thread_local_data().reset(new __thread_struct);
Howard Hinnant99968442011-11-29 18:15:50368 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Howard Hinnantbc8d3f92010-05-11 19:42:16369 (*__p)();
370 return nullptr;
371}
372
Howard Hinnant99968442011-11-29 18:15:50373template <class _Fp>
374thread::thread(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16375{
Howard Hinnant99968442011-11-29 18:15:50376 std::unique_ptr<_Fp> __p(new _Fp(__f));
377 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16378 if (__ec == 0)
379 __p.release();
380 else
381 __throw_system_error(__ec, "thread constructor failed");
382}
383
Howard Hinnant324bb032010-08-22 00:02:43384#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16385
Howard Hinnant73d21a42010-09-04 23:28:19386#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16387
Howard Hinnant8d7a9552010-09-23 17:31:07388inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16389thread&
Howard Hinnant6e1d8512012-07-21 16:50:47390thread::operator=(thread&& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16391{
Howard Hinnanta6a062d2010-06-02 18:20:39392 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16393 terminate();
394 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39395 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16396 return *this;
397}
398
Howard Hinnant73d21a42010-09-04 23:28:19399#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16400
Howard Hinnant8d7a9552010-09-23 17:31:07401inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47402void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16403
Howard Hinnantbc8d3f92010-05-11 19:42:16404namespace this_thread
405{
406
Howard Hinnant0f678bd2013-08-12 18:38:34407_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16408
409template <class _Rep, class _Period>
410void
411sleep_for(const chrono::duration<_Rep, _Period>& __d)
412{
413 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33414 if (__d > duration<_Rep, _Period>::zero())
415 {
416 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
417 nanoseconds __ns;
418 if (__d < _Max)
419 {
420 __ns = duration_cast<nanoseconds>(__d);
421 if (__ns < __d)
422 ++__ns;
423 }
424 else
425 __ns = nanoseconds::max();
426 sleep_for(__ns);
427 }
Howard Hinnantbc8d3f92010-05-11 19:42:16428}
429
430template <class _Clock, class _Duration>
431void
432sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
433{
434 using namespace chrono;
435 mutex __mut;
436 condition_variable __cv;
437 unique_lock<mutex> __lk(__mut);
438 while (_Clock::now() < __t)
439 __cv.wait_until(__lk, __t);
440}
441
442template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07443inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16444void
Howard Hinnantf8f85212010-11-20 19:16:30445sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16446{
447 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30448 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16449}
450
Howard Hinnant8d7a9552010-09-23 17:31:07451inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47452void yield() _NOEXCEPT {sched_yield();}
Howard Hinnantbc8d3f92010-05-11 19:42:16453
454} // this_thread
455
456_LIBCPP_END_NAMESPACE_STD
457
458#endif // _LIBCPP_THREAD