blob: 3ce32fc28b1ce7c1732267b6f787d641a571e0b1 [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
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000101#include <__threading_support>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000102
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
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000115template <class _Tp> class __thread_specific_ptr;
116class _LIBCPP_TYPE_VIS __thread_struct;
117class _LIBCPP_HIDDEN __thread_struct_imp;
118class __assoc_sub_state;
119
120_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
121
122class _LIBCPP_TYPE_VIS __thread_struct
123{
124 __thread_struct_imp* __p_;
125
126 __thread_struct(const __thread_struct&);
127 __thread_struct& operator=(const __thread_struct&);
128public:
129 __thread_struct();
130 ~__thread_struct();
131
132 void notify_all_at_thread_exit(condition_variable*, mutex*);
133 void __make_ready_at_thread_exit(__assoc_sub_state*);
134};
135
Howard Hinnant47499b12010-08-27 20:10:19 +0000136template <class _Tp>
137class __thread_specific_ptr
138{
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000139 __libcpp_tl_key __key_;
Howard Hinnant47499b12010-08-27 20:10:19 +0000140
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000141 // Only __thread_local_data() may construct a __thread_specific_ptr
142 // and only with _Tp == __thread_struct.
Eric Fiselierf99b59c2015-08-19 03:38:41 +0000143 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000144 __thread_specific_ptr();
145 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
146
Howard Hinnant47499b12010-08-27 20:10:19 +0000147 __thread_specific_ptr(const __thread_specific_ptr&);
148 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
149
150 static void __at_thread_exit(void*);
151public:
152 typedef _Tp* pointer;
153
Howard Hinnant47499b12010-08-27 20:10:19 +0000154 ~__thread_specific_ptr();
155
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000156 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000157 pointer get() const {return static_cast<_Tp*>(__libcpp_tl_get(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000159 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000161 pointer operator->() const {return get();}
162 pointer release();
163 void reset(pointer __p = nullptr);
164};
165
166template <class _Tp>
167void
168__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
169{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000170 delete static_cast<pointer>(__p);
Howard Hinnant47499b12010-08-27 20:10:19 +0000171}
172
173template <class _Tp>
174__thread_specific_ptr<_Tp>::__thread_specific_ptr()
175{
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000176 int __ec = __libcpp_tl_create(
177 &__key_,
178 &__thread_specific_ptr::__at_thread_exit);
Howard Hinnant2c0d3ed2013-03-28 15:00:04 +0000179#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000180 if (__ec)
181 throw system_error(error_code(__ec, system_category()),
182 "__thread_specific_ptr construction failed");
Howard Hinnant2c0d3ed2013-03-28 15:00:04 +0000183#endif
Howard Hinnant47499b12010-08-27 20:10:19 +0000184}
185
186template <class _Tp>
187__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
188{
Eric Fiselier0376dfa2015-08-18 19:40:38 +0000189 // __thread_specific_ptr is only created with a static storage duration
190 // so this destructor is only invoked during program termination. Invoking
191 // pthread_key_delete(__key_) may prevent other threads from deleting their
192 // thread local data. For this reason we leak the key.
Howard Hinnant47499b12010-08-27 20:10:19 +0000193}
194
195template <class _Tp>
196typename __thread_specific_ptr<_Tp>::pointer
197__thread_specific_ptr<_Tp>::release()
198{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000199 pointer __p = get();
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000200 __libcpp_tl_set(__key_, nullptr);
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000201 return __p;
Howard Hinnant47499b12010-08-27 20:10:19 +0000202}
203
204template <class _Tp>
205void
206__thread_specific_ptr<_Tp>::reset(pointer __p)
207{
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000208 pointer __p_old = get();
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000209 __libcpp_tl_set(__key_, __p);
Howard Hinnant8db4aca2011-10-17 20:08:59 +0000210 delete __p_old;
Howard Hinnant47499b12010-08-27 20:10:19 +0000211}
212
Howard Hinnant83eade62013-03-06 23:30:19 +0000213class _LIBCPP_TYPE_VIS thread;
214class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000215
216namespace this_thread
217{
218
Howard Hinnant33be35e2012-09-14 00:39:16 +0000219_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220
221} // this_thread
222
Eric Fiselier566bcb42016-04-21 22:54:21 +0000223template<> struct hash<__thread_id>;
Howard Hinnant40c13d32011-12-05 00:08:45 +0000224
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000225class _LIBCPP_TYPE_VIS_ONLY __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000226{
Howard Hinnantadff4892010-05-24 17:49:41 +0000227 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
228 // NULL is the no-thread value on Darwin. Someone needs to check
229 // on other platforms. We assume 0 works everywhere for now.
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000230 __libcpp_thread_id __id_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231
232public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000234 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000236 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000237 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000238 {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000239 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000240 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241 {return !(__x == __y);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000242 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000243 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000244 {return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000245 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000246 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247 {return !(__y < __x);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000248 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000249 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250 {return __y < __x ;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000251 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000252 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253 {return !(__x < __y);}
254
255 template<class _CharT, class _Traits>
256 friend
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258 basic_ostream<_CharT, _Traits>&
259 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
260 {return __os << __id.__id_;}
261
262private:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000263 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000264 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265
Howard Hinnant96c60b42012-08-19 15:13:16 +0000266 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant83eade62013-03-06 23:30:19 +0000267 friend class _LIBCPP_TYPE_VIS thread;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000268 friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269};
270
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271template<>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000272struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273 : public unary_function<__thread_id, size_t>
274{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276 size_t operator()(__thread_id __v) const
277 {
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000278 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279 }
280};
281
282namespace this_thread
283{
284
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000285inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286__thread_id
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000287get_id() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288{
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000289 return __libcpp_thread_get_current_id();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290}
291
292} // this_thread
293
Howard Hinnant83eade62013-03-06 23:30:19 +0000294class _LIBCPP_TYPE_VIS thread
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295{
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000296 __libcpp_thread_t __t_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000297
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000298 thread(const thread&);
299 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300public:
301 typedef __thread_id id;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000302 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000305 thread() _NOEXCEPT : __t_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50 +0000307 template <class _Fp, class ..._Args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308 class = typename enable_if
309 <
Howard Hinnant99968442011-11-29 18:15:50 +0000310 !is_same<typename decay<_Fp>::type, thread>::value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 >::type
312 >
Howard Hinnant99968442011-11-29 18:15:50 +0000313 explicit thread(_Fp&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000314#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant99968442011-11-29 18:15:50 +0000315 template <class _Fp> explicit thread(_Fp __f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316#endif
317 ~thread();
318
Howard Hinnant73d21a42010-09-04 23:28:19 +0000319#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000321 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000323 thread& operator=(thread&& __t) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000324#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000327 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000330 bool joinable() const _NOEXCEPT {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331 void join();
332 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000333 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000334 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000336 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000338 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339};
340
Howard Hinnant656bdc32011-05-16 18:40:35 +0000341#ifndef _LIBCPP_HAS_NO_VARIADICS
342
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000343template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant656bdc32011-05-16 18:40:35 +0000344inline _LIBCPP_INLINE_VISIBILITY
345void
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000346__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000347{
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000348 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35 +0000349}
350
Howard Hinnant99968442011-11-29 18:15:50 +0000351template <class _Fp>
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000352void* __thread_proxy(void* __vp)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000353{
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000354 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
Howard Hinnant99968442011-11-29 18:15:50 +0000355 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000356 __thread_local_data().reset(_VSTD::get<0>(*__p).release());
357 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Howard Hinnantc5e89612013-04-03 20:29:45 +0000358 __thread_execute(*__p, _Index());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000359 return nullptr;
360}
361
Howard Hinnant99968442011-11-29 18:15:50 +0000362template <class _Fp, class ..._Args,
Howard Hinnant656bdc32011-05-16 18:40:35 +0000363 class
364 >
Howard Hinnant99968442011-11-29 18:15:50 +0000365thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant656bdc32011-05-16 18:40:35 +0000366{
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000367 typedef unique_ptr<__thread_struct> _TSPtr;
368 _TSPtr __tsp(new __thread_struct);
369 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
370 _VSTD::unique_ptr<_Gp> __p(
371 new _Gp(std::move(__tsp),
372 __decay_copy(_VSTD::forward<_Fp>(__f)),
373 __decay_copy(_VSTD::forward<_Args>(__args))...));
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000374 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant656bdc32011-05-16 18:40:35 +0000375 if (__ec == 0)
376 __p.release();
377 else
378 __throw_system_error(__ec, "thread constructor failed");
379}
380
381#else // _LIBCPP_HAS_NO_VARIADICS
382
Howard Hinnant99968442011-11-29 18:15:50 +0000383template <class _Fp>
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000384struct __thread_invoke_pair {
385 // This type is used to pass memory for thread local storage and a functor
386 // to a newly created thread because std::pair doesn't work with
387 // std::unique_ptr in C++03.
388 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
389 unique_ptr<__thread_struct> __tsp_;
390 _Fp __fn_;
391};
392
393template <class _Fp>
394void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395{
Howard Hinnant99968442011-11-29 18:15:50 +0000396 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000397 __thread_local_data().reset(__p->__tsp_.release());
398 (__p->__fn_)();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399 return nullptr;
400}
401
Howard Hinnant99968442011-11-29 18:15:50 +0000402template <class _Fp>
403thread::thread(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404{
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000405
406 typedef __thread_invoke_pair<_Fp> _InvokePair;
407 typedef std::unique_ptr<_InvokePair> _PairPtr;
408 _PairPtr __pp(new _InvokePair(__f));
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000409 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 if (__ec == 0)
Eric Fiseliere94c1ae2016-04-20 02:21:33 +0000411 __pp.release();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412 else
413 __throw_system_error(__ec, "thread constructor failed");
414}
415
Howard Hinnant324bb032010-08-22 00:02:43 +0000416#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417
Howard Hinnant73d21a42010-09-04 23:28:19 +0000418#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000420inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421thread&
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000422thread::operator=(thread&& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423{
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000424 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425 terminate();
426 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000427 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428 return *this;
429}
430
Howard Hinnant73d21a42010-09-04 23:28:19 +0000431#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000433inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6e1d8512012-07-21 16:50:47 +0000434void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436namespace this_thread
437{
438
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000439_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440
441template <class _Rep, class _Period>
442void
443sleep_for(const chrono::duration<_Rep, _Period>& __d)
444{
445 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000446 if (__d > duration<_Rep, _Period>::zero())
447 {
448 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
449 nanoseconds __ns;
450 if (__d < _Max)
451 {
452 __ns = duration_cast<nanoseconds>(__d);
453 if (__ns < __d)
454 ++__ns;
455 }
456 else
457 __ns = nanoseconds::max();
458 sleep_for(__ns);
459 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000460}
461
462template <class _Clock, class _Duration>
463void
464sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
465{
466 using namespace chrono;
467 mutex __mut;
468 condition_variable __cv;
469 unique_lock<mutex> __lk(__mut);
470 while (_Clock::now() < __t)
471 __cv.wait_until(__lk, __t);
472}
473
474template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000475inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476void
Howard Hinnantf8f85212010-11-20 19:16:30 +0000477sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478{
479 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30 +0000480 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481}
482
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000483inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000484void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485
486} // this_thread
487
488_LIBCPP_END_NAMESPACE_STD
489
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000490#endif // !_LIBCPP_HAS_NO_THREADS
491
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492#endif // _LIBCPP_THREAD