2 //===--------------------------- thread -----------------------------------===//
4 // 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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_THREAD
11 #define _LIBCPP_THREAD
24 typedef pthread_t native_handle_type;
27 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
30 thread(const thread&) = delete;
31 thread(thread&& t) noexcept;
33 thread& operator=(const thread&) = delete;
34 thread& operator=(thread&& t) noexcept;
36 void swap(thread& t) noexcept;
38 bool joinable() const noexcept;
41 id get_id() const noexcept;
42 native_handle_type native_handle();
44 static unsigned hardware_concurrency() noexcept;
47 void swap(thread& x, thread& y) noexcept;
55 bool operator==(thread::id x, thread::id y) noexcept;
56 bool operator!=(thread::id x, thread::id y) noexcept;
57 bool operator< (thread::id x, thread::id y) noexcept;
58 bool operator<=(thread::id x, thread::id y) noexcept;
59 bool operator> (thread::id x, thread::id y) noexcept;
60 bool operator>=(thread::id x, thread::id y) noexcept;
62 template<class charT, class traits>
63 basic_ostream<charT, traits>&
64 operator<<(basic_ostream<charT, traits>& out, thread::id id);
69 thread::id get_id() noexcept;
71 void yield() noexcept;
73 template <class Clock, class Duration>
74 void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
76 template <class Rep, class Period>
77 void sleep_for(const chrono::duration<Rep, Period>& rel_time);
87 #include <__functional_base>
88 #include <type_traits>
92 #include <system_error>
94 #include <__mutex_base>
95 #ifndef _LIBCPP_CXX03_LANG
98 #include <__threading_support>
101 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
102 #pragma GCC system_header
106 #include <__undef_macros>
108 #ifdef _LIBCPP_HAS_NO_THREADS
109 #error <thread> is not supported on this single threaded system
110 #else // !_LIBCPP_HAS_NO_THREADS
112 _LIBCPP_BEGIN_NAMESPACE_STD
114 template <class _Tp> class __thread_specific_ptr;
115 class _LIBCPP_TYPE_VIS __thread_struct;
116 class _LIBCPP_HIDDEN __thread_struct_imp;
117 class __assoc_sub_state;
119 _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
121 class _LIBCPP_TYPE_VIS __thread_struct
123 __thread_struct_imp* __p_;
125 __thread_struct(const __thread_struct&);
126 __thread_struct& operator=(const __thread_struct&);
131 void notify_all_at_thread_exit(condition_variable*, mutex*);
132 void __make_ready_at_thread_exit(__assoc_sub_state*);
136 class __thread_specific_ptr
138 __libcpp_tls_key __key_;
140 // Only __thread_local_data() may construct a __thread_specific_ptr
141 // and only with _Tp == __thread_struct.
142 static_assert((is_same<_Tp, __thread_struct>::value), "");
143 __thread_specific_ptr();
144 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
146 __thread_specific_ptr(const __thread_specific_ptr&);
147 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
149 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
152 typedef _Tp* pointer;
154 ~__thread_specific_ptr();
156 _LIBCPP_INLINE_VISIBILITY
157 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
158 _LIBCPP_INLINE_VISIBILITY
159 pointer operator*() const {return *get();}
160 _LIBCPP_INLINE_VISIBILITY
161 pointer operator->() const {return get();}
162 void set_pointer(pointer __p);
166 void _LIBCPP_TLS_DESTRUCTOR_CC
167 __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
169 delete static_cast<pointer>(__p);
173 __thread_specific_ptr<_Tp>::__thread_specific_ptr()
176 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
178 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
182 __thread_specific_ptr<_Tp>::~__thread_specific_ptr()
184 // __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.
192 __thread_specific_ptr<_Tp>::set_pointer(pointer __p)
194 _LIBCPP_ASSERT(get() == nullptr,
195 "Attempting to overwrite thread local data");
196 __libcpp_tls_set(__key_, __p);
200 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
201 : public unary_function<__thread_id, size_t>
203 _LIBCPP_INLINE_VISIBILITY
204 size_t operator()(__thread_id __v) const _NOEXCEPT
206 return hash<__libcpp_thread_id>()(__v.__id_);
210 template<class _CharT, class _Traits>
211 _LIBCPP_INLINE_VISIBILITY
212 basic_ostream<_CharT, _Traits>&
213 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
214 {return __os << __id.__id_;}
216 class _LIBCPP_TYPE_VIS thread
218 __libcpp_thread_t __t_;
220 thread(const thread&);
221 thread& operator=(const thread&);
223 typedef __thread_id id;
224 typedef __libcpp_thread_t native_handle_type;
226 _LIBCPP_INLINE_VISIBILITY
227 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
228 #ifndef _LIBCPP_CXX03_LANG
229 template <class _Fp, class ..._Args,
230 class = typename enable_if
232 !is_same<typename __uncvref<_Fp>::type, thread>::value
235 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
236 explicit thread(_Fp&& __f, _Args&&... __args);
237 #else // _LIBCPP_CXX03_LANG
239 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
240 explicit thread(_Fp __f);
244 #ifndef _LIBCPP_CXX03_LANG
245 _LIBCPP_INLINE_VISIBILITY
246 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
247 _LIBCPP_INLINE_VISIBILITY
248 thread& operator=(thread&& __t) _NOEXCEPT;
249 #endif // _LIBCPP_CXX03_LANG
251 _LIBCPP_INLINE_VISIBILITY
252 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
254 _LIBCPP_INLINE_VISIBILITY
255 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
258 _LIBCPP_INLINE_VISIBILITY
259 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
260 _LIBCPP_INLINE_VISIBILITY
261 native_handle_type native_handle() _NOEXCEPT {return __t_;}
263 static unsigned hardware_concurrency() _NOEXCEPT;
266 #ifndef _LIBCPP_CXX03_LANG
268 template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
269 inline _LIBCPP_INLINE_VISIBILITY
271 __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
273 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
277 _LIBCPP_INLINE_VISIBILITY
278 void* __thread_proxy(void* __vp)
280 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
281 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
282 __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
283 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
284 __thread_execute(*__p, _Index());
288 template <class _Fp, class ..._Args,
291 thread::thread(_Fp&& __f, _Args&&... __args)
293 typedef unique_ptr<__thread_struct> _TSPtr;
294 _TSPtr __tsp(new __thread_struct);
295 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
296 _VSTD::unique_ptr<_Gp> __p(
297 new _Gp(std::move(__tsp),
298 __decay_copy(_VSTD::forward<_Fp>(__f)),
299 __decay_copy(_VSTD::forward<_Args>(__args))...));
300 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
304 __throw_system_error(__ec, "thread constructor failed");
309 thread::operator=(thread&& __t) _NOEXCEPT
311 if (!__libcpp_thread_isnull(&__t_))
314 __t.__t_ = _LIBCPP_NULL_THREAD;
318 #else // _LIBCPP_CXX03_LANG
321 struct __thread_invoke_pair {
322 // This type is used to pass memory for thread local storage and a functor
323 // to a newly created thread because std::pair doesn't work with
324 // std::unique_ptr in C++03.
325 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
326 unique_ptr<__thread_struct> __tsp_;
331 void* __thread_proxy_cxx03(void* __vp)
333 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
334 __thread_local_data().set_pointer(__p->__tsp_.release());
340 thread::thread(_Fp __f)
343 typedef __thread_invoke_pair<_Fp> _InvokePair;
344 typedef std::unique_ptr<_InvokePair> _PairPtr;
345 _PairPtr __pp(new _InvokePair(__f));
346 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
350 __throw_system_error(__ec, "thread constructor failed");
353 #endif // _LIBCPP_CXX03_LANG
355 inline _LIBCPP_INLINE_VISIBILITY
356 void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
358 namespace this_thread
361 _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
363 template <class _Rep, class _Period>
365 sleep_for(const chrono::duration<_Rep, _Period>& __d)
367 using namespace chrono;
368 if (__d > duration<_Rep, _Period>::zero())
370 #if defined(_LIBCPP_COMPILER_GCC) && (__powerpc__ || __POWERPC__)
371 // GCC's long double const folding is incomplete for IBM128 long doubles.
372 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
374 _LIBCPP_CONSTEXPR duration<long double> _Max = duration<long double>(ULLONG_MAX/1000000000ULL) ;
379 __ns = duration_cast<nanoseconds>(__d);
384 __ns = nanoseconds::max();
389 template <class _Clock, class _Duration>
391 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
393 using namespace chrono;
395 condition_variable __cv;
396 unique_lock<mutex> __lk(__mut);
397 while (_Clock::now() < __t)
398 __cv.wait_until(__lk, __t);
401 template <class _Duration>
402 inline _LIBCPP_INLINE_VISIBILITY
404 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
406 using namespace chrono;
407 sleep_for(__t - steady_clock::now());
410 inline _LIBCPP_INLINE_VISIBILITY
411 void yield() _NOEXCEPT {__libcpp_thread_yield();}
415 _LIBCPP_END_NAMESPACE_STD
417 #endif // !_LIBCPP_HAS_NO_THREADS
421 #endif // _LIBCPP_THREAD