blob: 4277bc90b36aae89587779d4312c43ebe5d530dc [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- thread -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_THREAD
11#define _LIBCPP_THREAD
12
13/*
14
15 thread synopsis
16
Howard Hinnantc51e1022010-05-11 19:42:16 +000017namespace std
18{
19
20class thread
21{
22public:
23 class id;
24 typedef pthread_t native_handle_type;
25
Howard Hinnant4bd34702012-07-21 16:50:47 +000026 thread() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000027 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
28 ~thread();
29
30 thread(const thread&) = delete;
Howard Hinnant4bd34702012-07-21 16:50:47 +000031 thread(thread&& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000032
33 thread& operator=(const thread&) = delete;
Howard Hinnant4bd34702012-07-21 16:50:47 +000034 thread& operator=(thread&& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000035
Howard Hinnant4bd34702012-07-21 16:50:47 +000036 void swap(thread& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000037
Howard Hinnant4bd34702012-07-21 16:50:47 +000038 bool joinable() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000039 void join();
40 void detach();
Howard Hinnant4bd34702012-07-21 16:50:47 +000041 id get_id() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 native_handle_type native_handle();
43
Howard Hinnant4bd34702012-07-21 16:50:47 +000044 static unsigned hardware_concurrency() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000045};
46
Howard Hinnant4bd34702012-07-21 16:50:47 +000047void swap(thread& x, thread& y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000048
49class thread::id
50{
51public:
Howard Hinnant4bd34702012-07-21 16:50:47 +000052 id() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000053};
54
Howard Hinnant4bd34702012-07-21 16:50:47 +000055bool operator==(thread::id x, thread::id y) noexcept;
56bool operator!=(thread::id x, thread::id y) noexcept;
57bool operator< (thread::id x, thread::id y) noexcept;
58bool 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;
Howard Hinnantc51e1022010-05-11 19:42:16 +000061
62template<class charT, class traits>
63basic_ostream<charT, traits>&
64operator<<(basic_ostream<charT, traits>& out, thread::id id);
65
66namespace this_thread
67{
68
Howard Hinnant4bd34702012-07-21 16:50:47 +000069thread::id get_id() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000070
Howard Hinnant4bd34702012-07-21 16:50:47 +000071void yield() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
73template <class Clock, class Duration>
74void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
75
76template <class Rep, class Period>
77void sleep_for(const chrono::duration<Rep, Period>& rel_time);
78
79} // this_thread
80
81} // std
82
83*/
84
85#include <__config>
86#include <iosfwd>
87#include <__functional_base>
88#include <type_traits>
89#include <cstddef>
90#include <functional>
91#include <memory>
92#include <system_error>
93#include <chrono>
94#include <__mutex_base>
Eric Fiselierb6159752017-04-18 23:05:08 +000095#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +000096#include <tuple>
97#endif
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000098#include <__threading_support>
Eric Fiselier99245c52016-09-03 08:07:40 +000099#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000101#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000103#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104
Eric Fiselierf4433a32017-05-31 22:07:49 +0000105_LIBCPP_PUSH_MACROS
106#include <__undef_macros>
107
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000108#ifdef _LIBCPP_HAS_NO_THREADS
109#error <thread> is not supported on this single threaded system
110#else // !_LIBCPP_HAS_NO_THREADS
111
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112_LIBCPP_BEGIN_NAMESPACE_STD
113
Eric Fiselier80b0e152015-08-18 19:40:38 +0000114template <class _Tp> class __thread_specific_ptr;
115class _LIBCPP_TYPE_VIS __thread_struct;
116class _LIBCPP_HIDDEN __thread_struct_imp;
117class __assoc_sub_state;
118
119_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
120
121class _LIBCPP_TYPE_VIS __thread_struct
122{
123 __thread_struct_imp* __p_;
124
125 __thread_struct(const __thread_struct&);
126 __thread_struct& operator=(const __thread_struct&);
127public:
128 __thread_struct();
129 ~__thread_struct();
130
131 void notify_all_at_thread_exit(condition_variable*, mutex*);
132 void __make_ready_at_thread_exit(__assoc_sub_state*);
133};
134
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000135template <class _Tp>
136class __thread_specific_ptr
137{
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000138 __libcpp_tls_key __key_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000139
Eric Fiselier80b0e152015-08-18 19:40:38 +0000140 // Only __thread_local_data() may construct a __thread_specific_ptr
141 // and only with _Tp == __thread_struct.
Eric Fiselierf34995b2015-08-19 03:38:41 +0000142 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier80b0e152015-08-18 19:40:38 +0000143 __thread_specific_ptr();
144 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
145
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000146 __thread_specific_ptr(const __thread_specific_ptr&);
147 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
148
Louis Dionne5254b372018-10-25 12:13:43 +0000149 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000150
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000151public:
152 typedef _Tp* pointer;
153
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000154 ~__thread_specific_ptr();
155
Howard Hinnant186dca82010-09-23 17:31:07 +0000156 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000157 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
Howard Hinnant186dca82010-09-23 17:31:07 +0000158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000159 pointer operator*() const {return *get();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000161 pointer operator->() const {return get();}
Eric Fiselier99245c52016-09-03 08:07:40 +0000162 void set_pointer(pointer __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000163};
164
165template <class _Tp>
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000166void _LIBCPP_TLS_DESTRUCTOR_CC
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000167__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
168{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000169 delete static_cast<pointer>(__p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000170}
171
172template <class _Tp>
173__thread_specific_ptr<_Tp>::__thread_specific_ptr()
174{
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000175 int __ec =
176 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
177 if (__ec)
178 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000179}
180
181template <class _Tp>
182__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
183{
Eric Fiselier80b0e152015-08-18 19:40:38 +0000184 // __thread_specific_ptr is only created with a static storage duration
185 // so this destructor is only invoked during program termination. Invoking
186 // pthread_key_delete(__key_) may prevent other threads from deleting their
187 // thread local data. For this reason we leak the key.
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000188}
189
190template <class _Tp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000191void
Eric Fiselier99245c52016-09-03 08:07:40 +0000192__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000193{
Eric Fiselier99245c52016-09-03 08:07:40 +0000194 _LIBCPP_ASSERT(get() == nullptr,
195 "Attempting to overwrite thread local data");
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000196 __libcpp_tls_set(__key_, __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000197}
198
Howard Hinnant8331b762013-03-06 23:30:19 +0000199class _LIBCPP_TYPE_VIS thread;
200class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201
202namespace this_thread
203{
204
Howard Hinnanta54386e2012-09-14 00:39:16 +0000205_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206
207} // this_thread
208
Eric Fiselier6585c752016-04-21 22:54:21 +0000209template<> struct hash<__thread_id>;
Howard Hinnantad71c232011-12-05 00:08:45 +0000210
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000211class _LIBCPP_TEMPLATE_VIS __thread_id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212{
Howard Hinnant155c2af2010-05-24 17:49:41 +0000213 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
214 // NULL is the no-thread value on Darwin. Someone needs to check
215 // on other platforms. We assume 0 works everywhere for now.
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000216 __libcpp_thread_id __id_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217
218public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000220 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221
Howard Hinnant186dca82010-09-23 17:31:07 +0000222 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000223 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000224 {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000225 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000226 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227 {return !(__x == __y);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000228 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000229 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000230 {return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000231 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000232 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233 {return !(__y < __x);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000234 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000235 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236 {return __y < __x ;}
Howard Hinnant186dca82010-09-23 17:31:07 +0000237 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000238 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 {return !(__x < __y);}
240
241 template<class _CharT, class _Traits>
242 friend
Howard Hinnant186dca82010-09-23 17:31:07 +0000243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 basic_ostream<_CharT, _Traits>&
245 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
246 {return __os << __id.__id_;}
247
248private:
Howard Hinnant186dca82010-09-23 17:31:07 +0000249 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000250 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251
Howard Hinnantd48ef422012-08-19 15:13:16 +0000252 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant8331b762013-03-06 23:30:19 +0000253 friend class _LIBCPP_TYPE_VIS thread;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000254 friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255};
256
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257template<>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000258struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 : public unary_function<__thread_id, size_t>
260{
Howard Hinnant186dca82010-09-23 17:31:07 +0000261 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +0000262 size_t operator()(__thread_id __v) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000264 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265 }
266};
267
268namespace this_thread
269{
270
Howard Hinnant186dca82010-09-23 17:31:07 +0000271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272__thread_id
Howard Hinnant4bd34702012-07-21 16:50:47 +0000273get_id() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000275 return __libcpp_thread_get_current_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276}
277
278} // this_thread
279
Howard Hinnant8331b762013-03-06 23:30:19 +0000280class _LIBCPP_TYPE_VIS thread
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000282 __libcpp_thread_t __t_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Howard Hinnantaa87c0f2010-08-10 20:48:29 +0000284 thread(const thread&);
285 thread& operator=(const thread&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286public:
287 typedef __thread_id id;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000288 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Howard Hinnant186dca82010-09-23 17:31:07 +0000290 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000291 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
Eric Fiselierb6159752017-04-18 23:05:08 +0000292#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +0000293 template <class _Fp, class ..._Args,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294 class = typename enable_if
295 <
Marshall Clowaaf031b2018-03-20 22:37:37 +0000296 !is_same<typename __uncvref<_Fp>::type, thread>::value
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297 >::type
298 >
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000299 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc834c512011-11-29 18:15:50 +0000300 explicit thread(_Fp&& __f, _Args&&... __args);
Eric Fiselierb6159752017-04-18 23:05:08 +0000301#else // _LIBCPP_CXX03_LANG
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000302 template <class _Fp>
303 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
304 explicit thread(_Fp __f);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305#endif
306 ~thread();
307
Eric Fiselierb6159752017-04-18 23:05:08 +0000308#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000309 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000310 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000312 thread& operator=(thread&& __t) _NOEXCEPT;
Eric Fiselierb6159752017-04-18 23:05:08 +0000313#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314
Howard Hinnant186dca82010-09-23 17:31:07 +0000315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000316 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317
Howard Hinnant186dca82010-09-23 17:31:07 +0000318 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000319 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320 void join();
321 void detach();
Howard Hinnant186dca82010-09-23 17:31:07 +0000322 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000323 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000325 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326
Howard Hinnant4bd34702012-07-21 16:50:47 +0000327 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328};
329
Eric Fiselierb6159752017-04-18 23:05:08 +0000330#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000331
Eric Fiselierfd996062016-04-20 02:21:33 +0000332template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000333inline _LIBCPP_INLINE_VISIBILITY
334void
Eric Fiselierfd996062016-04-20 02:21:33 +0000335__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000336{
Eric Fiselierfd996062016-04-20 02:21:33 +0000337 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000338}
339
Howard Hinnantc834c512011-11-29 18:15:50 +0000340template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000341void* __thread_proxy(void* __vp)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000342{
Eric Fiselierfd996062016-04-20 02:21:33 +0000343 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
Howard Hinnantc834c512011-11-29 18:15:50 +0000344 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000345 __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000346 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Howard Hinnant3462e1e2013-04-03 20:29:45 +0000347 __thread_execute(*__p, _Index());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000348 return nullptr;
349}
350
Howard Hinnantc834c512011-11-29 18:15:50 +0000351template <class _Fp, class ..._Args,
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000352 class
353 >
Howard Hinnantc834c512011-11-29 18:15:50 +0000354thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000355{
Eric Fiselierfd996062016-04-20 02:21:33 +0000356 typedef unique_ptr<__thread_struct> _TSPtr;
357 _TSPtr __tsp(new __thread_struct);
358 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
359 _VSTD::unique_ptr<_Gp> __p(
360 new _Gp(std::move(__tsp),
361 __decay_copy(_VSTD::forward<_Fp>(__f)),
362 __decay_copy(_VSTD::forward<_Args>(__args))...));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000363 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000364 if (__ec == 0)
365 __p.release();
366 else
367 __throw_system_error(__ec, "thread constructor failed");
368}
369
Eric Fiselierb6159752017-04-18 23:05:08 +0000370inline
371thread&
372thread::operator=(thread&& __t) _NOEXCEPT
373{
374 if (!__libcpp_thread_isnull(&__t_))
375 terminate();
376 __t_ = __t.__t_;
377 __t.__t_ = _LIBCPP_NULL_THREAD;
378 return *this;
379}
380
381#else // _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000382
Howard Hinnantc834c512011-11-29 18:15:50 +0000383template <class _Fp>
Eric Fiselierfd996062016-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 Hinnantc51e1022010-05-11 19:42:16 +0000395{
Howard Hinnantc834c512011-11-29 18:15:50 +0000396 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000397 __thread_local_data().set_pointer(__p->__tsp_.release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000398 (__p->__fn_)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 return nullptr;
400}
401
Howard Hinnantc834c512011-11-29 18:15:50 +0000402template <class _Fp>
403thread::thread(_Fp __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404{
Eric Fiselierfd996062016-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 Rathnayakefa2e2032016-05-06 14:06:29 +0000409 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410 if (__ec == 0)
Eric Fiselierfd996062016-04-20 02:21:33 +0000411 __pp.release();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 else
413 __throw_system_error(__ec, "thread constructor failed");
414}
415
Eric Fiselierb6159752017-04-18 23:05:08 +0000416#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417
Howard Hinnant186dca82010-09-23 17:31:07 +0000418inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000419void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421namespace this_thread
422{
423
Joerg Sonnenberger917a30a2017-02-09 14:12:29 +0000424_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425
426template <class _Rep, class _Period>
427void
428sleep_for(const chrono::duration<_Rep, _Period>& __d)
429{
430 using namespace chrono;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000431 if (__d > duration<_Rep, _Period>::zero())
432 {
Marshall Clow4aef5c32019-04-02 14:46:36 +0000433#if defined(_LIBCPP_COMPILER_GCC) && (__powerpc__ || __POWERPC__)
434 // GCC's long double const folding is incomplete for IBM128 long doubles.
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000435 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
Marshall Clow9b68f892019-04-03 00:01:03 +0000436#else
437 _LIBCPP_CONSTEXPR duration<long double> _Max = duration<long double>(ULLONG_MAX/1000000000ULL) ;
Marshall Clow4aef5c32019-04-02 14:46:36 +0000438#endif
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000439 nanoseconds __ns;
440 if (__d < _Max)
441 {
442 __ns = duration_cast<nanoseconds>(__d);
443 if (__ns < __d)
444 ++__ns;
445 }
446 else
447 __ns = nanoseconds::max();
448 sleep_for(__ns);
449 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000450}
451
452template <class _Clock, class _Duration>
453void
454sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
455{
456 using namespace chrono;
457 mutex __mut;
458 condition_variable __cv;
459 unique_lock<mutex> __lk(__mut);
460 while (_Clock::now() < __t)
461 __cv.wait_until(__lk, __t);
462}
463
464template <class _Duration>
Howard Hinnant186dca82010-09-23 17:31:07 +0000465inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000466void
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000467sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468{
469 using namespace chrono;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000470 sleep_for(__t - steady_clock::now());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471}
472
Howard Hinnant186dca82010-09-23 17:31:07 +0000473inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000474void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475
476} // this_thread
477
478_LIBCPP_END_NAMESPACE_STD
479
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000480#endif // !_LIBCPP_HAS_NO_THREADS
481
Eric Fiselierf4433a32017-05-31 22:07:49 +0000482_LIBCPP_POP_MACROS
483
Howard Hinnantc51e1022010-05-11 19:42:16 +0000484#endif // _LIBCPP_THREAD