blob: 400459ae7f32c4d7cd24b2d85c49d789500e432d [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 Hinnant29aba232010-08-21 21:01:59 +000017#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantc51e1022010-05-11 19:42:16 +000018
19namespace std
20{
21
22class thread
23{
24public:
25 class id;
26 typedef pthread_t native_handle_type;
27
Howard Hinnant4bd34702012-07-21 16:50:47 +000028 thread() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000029 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
30 ~thread();
31
32 thread(const thread&) = delete;
Howard Hinnant4bd34702012-07-21 16:50:47 +000033 thread(thread&& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000034
35 thread& operator=(const thread&) = delete;
Howard Hinnant4bd34702012-07-21 16:50:47 +000036 thread& operator=(thread&& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000037
Howard Hinnant4bd34702012-07-21 16:50:47 +000038 void swap(thread& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000039
Howard Hinnant4bd34702012-07-21 16:50:47 +000040 bool joinable() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 void join();
42 void detach();
Howard Hinnant4bd34702012-07-21 16:50:47 +000043 id get_id() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000044 native_handle_type native_handle();
45
Howard Hinnant4bd34702012-07-21 16:50:47 +000046 static unsigned hardware_concurrency() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000047};
48
Howard Hinnant4bd34702012-07-21 16:50:47 +000049void swap(thread& x, thread& y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000050
51class thread::id
52{
53public:
Howard Hinnant4bd34702012-07-21 16:50:47 +000054 id() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055};
56
Howard Hinnant4bd34702012-07-21 16:50:47 +000057bool 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;
61bool operator> (thread::id x, thread::id y) noexcept;
62bool operator>=(thread::id x, thread::id y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
64template<class charT, class traits>
65basic_ostream<charT, traits>&
66operator<<(basic_ostream<charT, traits>& out, thread::id id);
67
68namespace this_thread
69{
70
Howard Hinnant4bd34702012-07-21 16:50:47 +000071thread::id get_id() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Howard Hinnant4bd34702012-07-21 16:50:47 +000073void yield() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074
75template <class Clock, class Duration>
76void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
77
78template <class Rep, class Period>
79void sleep_for(const chrono::duration<Rep, Period>& rel_time);
80
81} // this_thread
82
83} // std
84
85*/
86
87#include <__config>
88#include <iosfwd>
89#include <__functional_base>
90#include <type_traits>
91#include <cstddef>
92#include <functional>
93#include <memory>
94#include <system_error>
95#include <chrono>
96#include <__mutex_base>
Eric Fiselierb6159752017-04-18 23:05:08 +000097#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +000098#include <tuple>
99#endif
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000100#include <__threading_support>
Eric Fiselier99245c52016-09-03 08:07:40 +0000101#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000103#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000105#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106
Eric Fiselierf4433a32017-05-31 22:07:49 +0000107_LIBCPP_PUSH_MACROS
108#include <__undef_macros>
109
Howard Hinnant29aba232010-08-21 21:01:59 +0000110#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000112#ifdef _LIBCPP_HAS_NO_THREADS
113#error <thread> is not supported on this single threaded system
114#else // !_LIBCPP_HAS_NO_THREADS
115
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116_LIBCPP_BEGIN_NAMESPACE_STD
117
Eric Fiselier80b0e152015-08-18 19:40:38 +0000118template <class _Tp> class __thread_specific_ptr;
119class _LIBCPP_TYPE_VIS __thread_struct;
120class _LIBCPP_HIDDEN __thread_struct_imp;
121class __assoc_sub_state;
122
123_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
124
125class _LIBCPP_TYPE_VIS __thread_struct
126{
127 __thread_struct_imp* __p_;
128
129 __thread_struct(const __thread_struct&);
130 __thread_struct& operator=(const __thread_struct&);
131public:
132 __thread_struct();
133 ~__thread_struct();
134
135 void notify_all_at_thread_exit(condition_variable*, mutex*);
136 void __make_ready_at_thread_exit(__assoc_sub_state*);
137};
138
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000139template <class _Tp>
140class __thread_specific_ptr
141{
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000142 __libcpp_tls_key __key_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000143
Eric Fiselier80b0e152015-08-18 19:40:38 +0000144 // Only __thread_local_data() may construct a __thread_specific_ptr
145 // and only with _Tp == __thread_struct.
Eric Fiselierf34995b2015-08-19 03:38:41 +0000146 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier80b0e152015-08-18 19:40:38 +0000147 __thread_specific_ptr();
148 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
149
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000150 __thread_specific_ptr(const __thread_specific_ptr&);
151 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
152
Louis Dionne5254b372018-10-25 12:13:43 +0000153 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000154
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000155public:
156 typedef _Tp* pointer;
157
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000158 ~__thread_specific_ptr();
159
Howard Hinnant186dca82010-09-23 17:31:07 +0000160 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000161 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
Howard Hinnant186dca82010-09-23 17:31:07 +0000162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000163 pointer operator*() const {return *get();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000165 pointer operator->() const {return get();}
Eric Fiselier99245c52016-09-03 08:07:40 +0000166 void set_pointer(pointer __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000167};
168
169template <class _Tp>
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000170void _LIBCPP_TLS_DESTRUCTOR_CC
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000171__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
172{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000173 delete static_cast<pointer>(__p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000174}
175
176template <class _Tp>
177__thread_specific_ptr<_Tp>::__thread_specific_ptr()
178{
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000179 int __ec =
180 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
181 if (__ec)
182 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000183}
184
185template <class _Tp>
186__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
187{
Eric Fiselier80b0e152015-08-18 19:40:38 +0000188 // __thread_specific_ptr is only created with a static storage duration
189 // so this destructor is only invoked during program termination. Invoking
190 // pthread_key_delete(__key_) may prevent other threads from deleting their
191 // thread local data. For this reason we leak the key.
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000192}
193
194template <class _Tp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000195void
Eric Fiselier99245c52016-09-03 08:07:40 +0000196__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000197{
Eric Fiselier99245c52016-09-03 08:07:40 +0000198 _LIBCPP_ASSERT(get() == nullptr,
199 "Attempting to overwrite thread local data");
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000200 __libcpp_tls_set(__key_, __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000201}
202
Howard Hinnant8331b762013-03-06 23:30:19 +0000203class _LIBCPP_TYPE_VIS thread;
204class _LIBCPP_TYPE_VIS __thread_id;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205
206namespace this_thread
207{
208
Howard Hinnanta54386e2012-09-14 00:39:16 +0000209_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210
211} // this_thread
212
Eric Fiselier6585c752016-04-21 22:54:21 +0000213template<> struct hash<__thread_id>;
Howard Hinnantad71c232011-12-05 00:08:45 +0000214
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000215class _LIBCPP_TEMPLATE_VIS __thread_id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216{
Howard Hinnant155c2af2010-05-24 17:49:41 +0000217 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
218 // NULL is the no-thread value on Darwin. Someone needs to check
219 // on other platforms. We assume 0 works everywhere for now.
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000220 __libcpp_thread_id __id_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221
222public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000224 __thread_id() _NOEXCEPT : __id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225
Howard Hinnant186dca82010-09-23 17:31:07 +0000226 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000227 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000228 {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000229 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000230 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 {return !(__x == __y);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000232 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000233 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000234 {return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000235 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000236 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 {return !(__y < __x);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000238 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000239 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240 {return __y < __x ;}
Howard Hinnant186dca82010-09-23 17:31:07 +0000241 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000242 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243 {return !(__x < __y);}
244
245 template<class _CharT, class _Traits>
246 friend
Howard Hinnant186dca82010-09-23 17:31:07 +0000247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248 basic_ostream<_CharT, _Traits>&
249 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
250 {return __os << __id.__id_;}
251
252private:
Howard Hinnant186dca82010-09-23 17:31:07 +0000253 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000254 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255
Howard Hinnantd48ef422012-08-19 15:13:16 +0000256 friend __thread_id this_thread::get_id() _NOEXCEPT;
Howard Hinnant8331b762013-03-06 23:30:19 +0000257 friend class _LIBCPP_TYPE_VIS thread;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000258 friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259};
260
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261template<>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000262struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263 : public unary_function<__thread_id, size_t>
264{
Howard Hinnant186dca82010-09-23 17:31:07 +0000265 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +0000266 size_t operator()(__thread_id __v) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000268 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269 }
270};
271
272namespace this_thread
273{
274
Howard Hinnant186dca82010-09-23 17:31:07 +0000275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276__thread_id
Howard Hinnant4bd34702012-07-21 16:50:47 +0000277get_id() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000279 return __libcpp_thread_get_current_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280}
281
282} // this_thread
283
Howard Hinnant8331b762013-03-06 23:30:19 +0000284class _LIBCPP_TYPE_VIS thread
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000286 __libcpp_thread_t __t_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287
Howard Hinnantaa87c0f2010-08-10 20:48:29 +0000288 thread(const thread&);
289 thread& operator=(const thread&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290public:
291 typedef __thread_id id;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000292 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293
Howard Hinnant186dca82010-09-23 17:31:07 +0000294 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000295 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
Eric Fiselierb6159752017-04-18 23:05:08 +0000296#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +0000297 template <class _Fp, class ..._Args,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298 class = typename enable_if
299 <
Marshall Clowaaf031b2018-03-20 22:37:37 +0000300 !is_same<typename __uncvref<_Fp>::type, thread>::value
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301 >::type
302 >
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000303 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc834c512011-11-29 18:15:50 +0000304 explicit thread(_Fp&& __f, _Args&&... __args);
Eric Fiselierb6159752017-04-18 23:05:08 +0000305#else // _LIBCPP_CXX03_LANG
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000306 template <class _Fp>
307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
308 explicit thread(_Fp __f);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309#endif
310 ~thread();
311
Eric Fiselierb6159752017-04-18 23:05:08 +0000312#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000313 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000314 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000316 thread& operator=(thread&& __t) _NOEXCEPT;
Eric Fiselierb6159752017-04-18 23:05:08 +0000317#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318
Howard Hinnant186dca82010-09-23 17:31:07 +0000319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000320 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321
Howard Hinnant186dca82010-09-23 17:31:07 +0000322 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000323 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 void join();
325 void detach();
Howard Hinnant186dca82010-09-23 17:31:07 +0000326 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000327 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000329 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330
Howard Hinnant4bd34702012-07-21 16:50:47 +0000331 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332};
333
Eric Fiselierb6159752017-04-18 23:05:08 +0000334#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000335
Eric Fiselierfd996062016-04-20 02:21:33 +0000336template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000337inline _LIBCPP_INLINE_VISIBILITY
338void
Eric Fiselierfd996062016-04-20 02:21:33 +0000339__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000340{
Eric Fiselierfd996062016-04-20 02:21:33 +0000341 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000342}
343
Howard Hinnantc834c512011-11-29 18:15:50 +0000344template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000345void* __thread_proxy(void* __vp)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000346{
Eric Fiselierfd996062016-04-20 02:21:33 +0000347 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
Howard Hinnantc834c512011-11-29 18:15:50 +0000348 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000349 __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000350 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Howard Hinnant3462e1e2013-04-03 20:29:45 +0000351 __thread_execute(*__p, _Index());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000352 return nullptr;
353}
354
Howard Hinnantc834c512011-11-29 18:15:50 +0000355template <class _Fp, class ..._Args,
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000356 class
357 >
Howard Hinnantc834c512011-11-29 18:15:50 +0000358thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000359{
Eric Fiselierfd996062016-04-20 02:21:33 +0000360 typedef unique_ptr<__thread_struct> _TSPtr;
361 _TSPtr __tsp(new __thread_struct);
362 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
363 _VSTD::unique_ptr<_Gp> __p(
364 new _Gp(std::move(__tsp),
365 __decay_copy(_VSTD::forward<_Fp>(__f)),
366 __decay_copy(_VSTD::forward<_Args>(__args))...));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000367 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000368 if (__ec == 0)
369 __p.release();
370 else
371 __throw_system_error(__ec, "thread constructor failed");
372}
373
Eric Fiselierb6159752017-04-18 23:05:08 +0000374inline
375thread&
376thread::operator=(thread&& __t) _NOEXCEPT
377{
378 if (!__libcpp_thread_isnull(&__t_))
379 terminate();
380 __t_ = __t.__t_;
381 __t.__t_ = _LIBCPP_NULL_THREAD;
382 return *this;
383}
384
385#else // _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000386
Howard Hinnantc834c512011-11-29 18:15:50 +0000387template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000388struct __thread_invoke_pair {
389 // This type is used to pass memory for thread local storage and a functor
390 // to a newly created thread because std::pair doesn't work with
391 // std::unique_ptr in C++03.
392 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
393 unique_ptr<__thread_struct> __tsp_;
394 _Fp __fn_;
395};
396
397template <class _Fp>
398void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399{
Howard Hinnantc834c512011-11-29 18:15:50 +0000400 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000401 __thread_local_data().set_pointer(__p->__tsp_.release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000402 (__p->__fn_)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 return nullptr;
404}
405
Howard Hinnantc834c512011-11-29 18:15:50 +0000406template <class _Fp>
407thread::thread(_Fp __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408{
Eric Fiselierfd996062016-04-20 02:21:33 +0000409
410 typedef __thread_invoke_pair<_Fp> _InvokePair;
411 typedef std::unique_ptr<_InvokePair> _PairPtr;
412 _PairPtr __pp(new _InvokePair(__f));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000413 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414 if (__ec == 0)
Eric Fiselierfd996062016-04-20 02:21:33 +0000415 __pp.release();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416 else
417 __throw_system_error(__ec, "thread constructor failed");
418}
419
Eric Fiselierb6159752017-04-18 23:05:08 +0000420#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
Howard Hinnant186dca82010-09-23 17:31:07 +0000422inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000423void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425namespace this_thread
426{
427
Joerg Sonnenberger917a30a2017-02-09 14:12:29 +0000428_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template <class _Rep, class _Period>
431void
432sleep_for(const chrono::duration<_Rep, _Period>& __d)
433{
434 using namespace chrono;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000435 if (__d > duration<_Rep, _Period>::zero())
436 {
Marshall Clow4aef5c32019-04-02 14:46:36 +0000437#if defined(_LIBCPP_COMPILER_GCC) && (__powerpc__ || __POWERPC__)
438 // GCC's long double const folding is incomplete for IBM128 long doubles.
439 _LIBCPP_CONSTEXPR duration<long double> _Max = duration<long double>(ULLONG_MAX/1000000000ULL) ;
440#else
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000441 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
Marshall Clow4aef5c32019-04-02 14:46:36 +0000442#endif
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000443 nanoseconds __ns;
444 if (__d < _Max)
445 {
446 __ns = duration_cast<nanoseconds>(__d);
447 if (__ns < __d)
448 ++__ns;
449 }
450 else
451 __ns = nanoseconds::max();
452 sleep_for(__ns);
453 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454}
455
456template <class _Clock, class _Duration>
457void
458sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
459{
460 using namespace chrono;
461 mutex __mut;
462 condition_variable __cv;
463 unique_lock<mutex> __lk(__mut);
464 while (_Clock::now() < __t)
465 __cv.wait_until(__lk, __t);
466}
467
468template <class _Duration>
Howard Hinnant186dca82010-09-23 17:31:07 +0000469inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470void
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000471sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000472{
473 using namespace chrono;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000474 sleep_for(__t - steady_clock::now());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475}
476
Howard Hinnant186dca82010-09-23 17:31:07 +0000477inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000478void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479
480} // this_thread
481
482_LIBCPP_END_NAMESPACE_STD
483
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000484#endif // !_LIBCPP_HAS_NO_THREADS
485
Eric Fiselierf4433a32017-05-31 22:07:49 +0000486_LIBCPP_POP_MACROS
487
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488#endif // _LIBCPP_THREAD