blob: c9d1dfaa44999810bdf04bbce63a65f611c20280 [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
Asiri Rathnayake35ff03b2016-05-06 14:06:29101#include <__threading_support>
Eric Fiselier7f735c32016-09-03 08:07:40102#include <__debug>
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{
Asiri Rathnayake040945b2016-09-11 21:46:40140 __libcpp_tls_key __key_;
Howard Hinnant47499b12010-08-27 20:10:19141
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
Saleem Abdulrasool678eadd2017-01-07 03:07:45151 static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
152
Howard Hinnant47499b12010-08-27 20:10:19153public:
154 typedef _Tp* pointer;
155
Howard Hinnant47499b12010-08-27 20:10:19156 ~__thread_specific_ptr();
157
Howard Hinnant8d7a9552010-09-23 17:31:07158 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake040945b2016-09-11 21:46:40159 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19161 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19163 pointer operator->() const {return get();}
Eric Fiselier7f735c32016-09-03 08:07:40164 void set_pointer(pointer __p);
Howard Hinnant47499b12010-08-27 20:10:19165};
166
167template <class _Tp>
Saleem Abdulrasool678eadd2017-01-07 03:07:45168void _LIBCPP_TLS_DESTRUCTOR_CC
Howard Hinnant47499b12010-08-27 20:10:19169__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{
Saleem Abdulrasool678eadd2017-01-07 03:07:45177 int __ec =
178 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
179 if (__ec)
180 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
Howard Hinnant47499b12010-08-27 20:10:19181}
182
183template <class _Tp>
184__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
185{
Eric Fiselier0376dfa2015-08-18 19:40:38186 // __thread_specific_ptr is only created with a static storage duration
187 // so this destructor is only invoked during program termination. Invoking
188 // pthread_key_delete(__key_) may prevent other threads from deleting their
189 // thread local data. For this reason we leak the key.
Howard Hinnant47499b12010-08-27 20:10:19190}
191
192template <class _Tp>
Howard Hinnant47499b12010-08-27 20:10:19193void
Eric Fiselier7f735c32016-09-03 08:07:40194__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
Howard Hinnant47499b12010-08-27 20:10:19195{
Eric Fiselier7f735c32016-09-03 08:07:40196 _LIBCPP_ASSERT(get() == nullptr,
197 "Attempting to overwrite thread local data");
Asiri Rathnayake040945b2016-09-11 21:46:40198 __libcpp_tls_set(__key_, __p);
Howard Hinnant47499b12010-08-27 20:10:19199}
200
Howard Hinnant83eade62013-03-06 23:30:19201class _LIBCPP_TYPE_VIS thread;
202class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantbc8d3f92010-05-11 19:42:16203
204namespace this_thread
205{
206
Howard Hinnant33be35e2012-09-14 00:39:16207_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16208
209} // this_thread
210
Eric Fiselier566bcb42016-04-21 22:54:21211template<> struct hash<__thread_id>;
Howard Hinnant40c13d32011-12-05 00:08:45212
Eric Fiselierc3589a82017-01-04 23:56:00213class _LIBCPP_TEMPLATE_VIS __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16214{
Howard Hinnantadff4892010-05-24 17:49:41215 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
216 // NULL is the no-thread value on Darwin. Someone needs to check
217 // on other platforms. We assume 0 works everywhere for now.
Asiri Rathnayake35ff03b2016-05-06 14:06:29218 __libcpp_thread_id __id_;
Howard Hinnantbc8d3f92010-05-11 19:42:16219
220public:
Howard Hinnant8d7a9552010-09-23 17:31:07221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47222 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16223
Howard Hinnant8d7a9552010-09-23 17:31:07224 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47225 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayake35ff03b2016-05-06 14:06:29226 {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
Howard Hinnant8d7a9552010-09-23 17:31:07227 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47228 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16229 {return !(__x == __y);}
Howard Hinnant8d7a9552010-09-23 17:31:07230 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47231 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayake35ff03b2016-05-06 14:06:29232 {return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
Howard Hinnant8d7a9552010-09-23 17:31:07233 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47234 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16235 {return !(__y < __x);}
Howard Hinnant8d7a9552010-09-23 17:31:07236 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47237 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16238 {return __y < __x ;}
Howard Hinnant8d7a9552010-09-23 17:31:07239 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47240 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16241 {return !(__x < __y);}
242
243 template<class _CharT, class _Traits>
244 friend
Howard Hinnant8d7a9552010-09-23 17:31:07245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16246 basic_ostream<_CharT, _Traits>&
247 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
248 {return __os << __id.__id_;}
249
250private:
Howard Hinnant8d7a9552010-09-23 17:31:07251 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29252 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16253
Howard Hinnant96c60b42012-08-19 15:13:16254 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant83eade62013-03-06 23:30:19255 friend class _LIBCPP_TYPE_VIS thread;
Eric Fiselierc3589a82017-01-04 23:56:00256 friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
Howard Hinnantbc8d3f92010-05-11 19:42:16257};
258
Howard Hinnantbc8d3f92010-05-11 19:42:16259template<>
Eric Fiselierc3589a82017-01-04 23:56:00260struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16261 : public unary_function<__thread_id, size_t>
262{
Howard Hinnant8d7a9552010-09-23 17:31:07263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16264 size_t operator()(__thread_id __v) const
265 {
Asiri Rathnayake35ff03b2016-05-06 14:06:29266 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantbc8d3f92010-05-11 19:42:16267 }
268};
269
270namespace this_thread
271{
272
Howard Hinnant8d7a9552010-09-23 17:31:07273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16274__thread_id
Howard Hinnant6e1d8512012-07-21 16:50:47275get_id() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16276{
Asiri Rathnayake35ff03b2016-05-06 14:06:29277 return __libcpp_thread_get_current_id();
Howard Hinnantbc8d3f92010-05-11 19:42:16278}
279
280} // this_thread
281
Howard Hinnant83eade62013-03-06 23:30:19282class _LIBCPP_TYPE_VIS thread
Howard Hinnantbc8d3f92010-05-11 19:42:16283{
Asiri Rathnayake35ff03b2016-05-06 14:06:29284 __libcpp_thread_t __t_;
Howard Hinnantbc8d3f92010-05-11 19:42:16285
Howard Hinnant60a0a8e2010-08-10 20:48:29286 thread(const thread&);
287 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16288public:
289 typedef __thread_id id;
Asiri Rathnayake35ff03b2016-05-06 14:06:29290 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16291
Howard Hinnant8d7a9552010-09-23 17:31:07292 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54293 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16294#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50295 template <class _Fp, class ..._Args,
Howard Hinnantbc8d3f92010-05-11 19:42:16296 class = typename enable_if
297 <
Howard Hinnant99968442011-11-29 18:15:50298 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantbc8d3f92010-05-11 19:42:16299 >::type
300 >
Howard Hinnant99968442011-11-29 18:15:50301 explicit thread(_Fp&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43302#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50303 template <class _Fp> explicit thread(_Fp __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16304#endif
305 ~thread();
306
Howard Hinnant73d21a42010-09-04 23:28:19307#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07308 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54309 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
Evgeniy Stepanova3b25f82015-11-07 01:22:13310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47311 thread& operator=(thread&& __t) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19312#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16313
Howard Hinnant8d7a9552010-09-23 17:31:07314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47315 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16316
Howard Hinnant8d7a9552010-09-23 17:31:07317 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54318 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16319 void join();
320 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07321 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29322 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant8d7a9552010-09-23 17:31:07323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47324 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16325
Howard Hinnant6e1d8512012-07-21 16:50:47326 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16327};
328
Howard Hinnant656bdc32011-05-16 18:40:35329#ifndef _LIBCPP_HAS_NO_VARIADICS
330
Eric Fiseliere94c1ae2016-04-20 02:21:33331template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant656bdc32011-05-16 18:40:35332inline _LIBCPP_INLINE_VISIBILITY
333void
Eric Fiseliere94c1ae2016-04-20 02:21:33334__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant656bdc32011-05-16 18:40:35335{
Eric Fiseliere94c1ae2016-04-20 02:21:33336 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35337}
338
Howard Hinnant99968442011-11-29 18:15:50339template <class _Fp>
Eric Fiseliere94c1ae2016-04-20 02:21:33340void* __thread_proxy(void* __vp)
Howard Hinnant656bdc32011-05-16 18:40:35341{
Eric Fiseliere94c1ae2016-04-20 02:21:33342 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
Howard Hinnant99968442011-11-29 18:15:50343 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier7f735c32016-09-03 08:07:40344 __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
Eric Fiseliere94c1ae2016-04-20 02:21:33345 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Howard Hinnantc5e89612013-04-03 20:29:45346 __thread_execute(*__p, _Index());
Howard Hinnant656bdc32011-05-16 18:40:35347 return nullptr;
348}
349
Howard Hinnant99968442011-11-29 18:15:50350template <class _Fp, class ..._Args,
Howard Hinnant656bdc32011-05-16 18:40:35351 class
352 >
Howard Hinnant99968442011-11-29 18:15:50353thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant656bdc32011-05-16 18:40:35354{
Eric Fiseliere94c1ae2016-04-20 02:21:33355 typedef unique_ptr<__thread_struct> _TSPtr;
356 _TSPtr __tsp(new __thread_struct);
357 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
358 _VSTD::unique_ptr<_Gp> __p(
359 new _Gp(std::move(__tsp),
360 __decay_copy(_VSTD::forward<_Fp>(__f)),
361 __decay_copy(_VSTD::forward<_Args>(__args))...));
Asiri Rathnayake35ff03b2016-05-06 14:06:29362 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant656bdc32011-05-16 18:40:35363 if (__ec == 0)
364 __p.release();
365 else
366 __throw_system_error(__ec, "thread constructor failed");
367}
368
369#else // _LIBCPP_HAS_NO_VARIADICS
370
Howard Hinnant99968442011-11-29 18:15:50371template <class _Fp>
Eric Fiseliere94c1ae2016-04-20 02:21:33372struct __thread_invoke_pair {
373 // This type is used to pass memory for thread local storage and a functor
374 // to a newly created thread because std::pair doesn't work with
375 // std::unique_ptr in C++03.
376 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
377 unique_ptr<__thread_struct> __tsp_;
378 _Fp __fn_;
379};
380
381template <class _Fp>
382void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantbc8d3f92010-05-11 19:42:16383{
Howard Hinnant99968442011-11-29 18:15:50384 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier7f735c32016-09-03 08:07:40385 __thread_local_data().set_pointer(__p->__tsp_.release());
Eric Fiseliere94c1ae2016-04-20 02:21:33386 (__p->__fn_)();
Howard Hinnantbc8d3f92010-05-11 19:42:16387 return nullptr;
388}
389
Howard Hinnant99968442011-11-29 18:15:50390template <class _Fp>
391thread::thread(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16392{
Eric Fiseliere94c1ae2016-04-20 02:21:33393
394 typedef __thread_invoke_pair<_Fp> _InvokePair;
395 typedef std::unique_ptr<_InvokePair> _PairPtr;
396 _PairPtr __pp(new _InvokePair(__f));
Asiri Rathnayake35ff03b2016-05-06 14:06:29397 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16398 if (__ec == 0)
Eric Fiseliere94c1ae2016-04-20 02:21:33399 __pp.release();
Howard Hinnantbc8d3f92010-05-11 19:42:16400 else
401 __throw_system_error(__ec, "thread constructor failed");
402}
403
Howard Hinnant324bb032010-08-22 00:02:43404#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16405
Howard Hinnant73d21a42010-09-04 23:28:19406#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16407
Evgeniy Stepanova3b25f82015-11-07 01:22:13408inline
Howard Hinnantbc8d3f92010-05-11 19:42:16409thread&
Howard Hinnant6e1d8512012-07-21 16:50:47410thread::operator=(thread&& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16411{
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54412 if (!__libcpp_thread_isnull(&__t_))
Howard Hinnantbc8d3f92010-05-11 19:42:16413 terminate();
414 __t_ = __t.__t_;
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54415 __t.__t_ = _LIBCPP_NULL_THREAD;
Howard Hinnantbc8d3f92010-05-11 19:42:16416 return *this;
417}
418
Howard Hinnant73d21a42010-09-04 23:28:19419#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16420
Howard Hinnant8d7a9552010-09-23 17:31:07421inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47422void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16423
Howard Hinnantbc8d3f92010-05-11 19:42:16424namespace this_thread
425{
426
Joerg Sonnenberger5a0c9c32017-02-09 14:12:29427_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16428
429template <class _Rep, class _Period>
430void
431sleep_for(const chrono::duration<_Rep, _Period>& __d)
432{
433 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33434 if (__d > duration<_Rep, _Period>::zero())
435 {
436 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
437 nanoseconds __ns;
438 if (__d < _Max)
439 {
440 __ns = duration_cast<nanoseconds>(__d);
441 if (__ns < __d)
442 ++__ns;
443 }
444 else
445 __ns = nanoseconds::max();
446 sleep_for(__ns);
447 }
Howard Hinnantbc8d3f92010-05-11 19:42:16448}
449
450template <class _Clock, class _Duration>
451void
452sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
453{
454 using namespace chrono;
455 mutex __mut;
456 condition_variable __cv;
457 unique_lock<mutex> __lk(__mut);
458 while (_Clock::now() < __t)
459 __cv.wait_until(__lk, __t);
460}
461
462template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07463inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16464void
Howard Hinnantf8f85212010-11-20 19:16:30465sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16466{
467 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30468 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16469}
470
Howard Hinnant8d7a9552010-09-23 17:31:07471inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29472void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantbc8d3f92010-05-11 19:42:16473
474} // this_thread
475
476_LIBCPP_END_NAMESPACE_STD
477
Jonathan Roelofs8d86b2e2014-09-05 19:45:05478#endif // !_LIBCPP_HAS_NO_THREADS
479
Howard Hinnantbc8d3f92010-05-11 19:42:16480#endif // _LIBCPP_THREAD