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
17 #define __STDCPP_THREADS__ __cplusplus
26 typedef pthread_t native_handle_type;
29 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
32 thread(const thread&) = delete;
33 thread(thread&& t) noexcept;
35 thread& operator=(const thread&) = delete;
36 thread& operator=(thread&& t) noexcept;
38 void swap(thread& t) noexcept;
40 bool joinable() const noexcept;
43 id get_id() const noexcept;
44 native_handle_type native_handle();
46 static unsigned hardware_concurrency() noexcept;
49 void swap(thread& x, thread& 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;
61 bool operator> (thread::id x, thread::id y) noexcept;
62 bool operator>=(thread::id x, thread::id y) noexcept;
64 template<class charT, class traits>
65 basic_ostream<charT, traits>&
66 operator<<(basic_ostream<charT, traits>& out, thread::id id);
71 thread::id get_id() noexcept;
73 void yield() noexcept;
75 template <class Clock, class Duration>
76 void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
78 template <class Rep, class Period>
79 void sleep_for(const chrono::duration<Rep, Period>& rel_time);
89 #include <__functional_base>
90 #include <type_traits>
94 #include <system_error>
96 #include <__mutex_base>
97 #ifndef _LIBCPP_CXX03_LANG
100 #include <__threading_support>
103 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
104 #pragma GCC system_header
108 #include <__undef_macros>
110 #define __STDCPP_THREADS__ __cplusplus
112 #ifdef _LIBCPP_HAS_NO_THREADS
113 #error <thread> is not supported on this single threaded system
114 #else // !_LIBCPP_HAS_NO_THREADS
116 _LIBCPP_BEGIN_NAMESPACE_STD
118 template <class _Tp> class __thread_specific_ptr;
119 class _LIBCPP_TYPE_VIS __thread_struct;
120 class _LIBCPP_HIDDEN __thread_struct_imp;
121 class __assoc_sub_state;
123 _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
125 class _LIBCPP_TYPE_VIS __thread_struct
127 __thread_struct_imp* __p_;
129 __thread_struct(const __thread_struct&);
130 __thread_struct& operator=(const __thread_struct&);
135 void notify_all_at_thread_exit(condition_variable*, mutex*);
136 void __make_ready_at_thread_exit(__assoc_sub_state*);
140 class __thread_specific_ptr
142 __libcpp_tls_key __key_;
144 // Only __thread_local_data() may construct a __thread_specific_ptr
145 // and only with _Tp == __thread_struct.
146 static_assert((is_same<_Tp, __thread_struct>::value), "");
147 __thread_specific_ptr();
148 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
150 __thread_specific_ptr(const __thread_specific_ptr&);
151 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
153 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
156 typedef _Tp* pointer;
158 ~__thread_specific_ptr();
160 _LIBCPP_INLINE_VISIBILITY
161 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
162 _LIBCPP_INLINE_VISIBILITY
163 pointer operator*() const {return *get();}
164 _LIBCPP_INLINE_VISIBILITY
165 pointer operator->() const {return get();}
166 void set_pointer(pointer __p);
170 void _LIBCPP_TLS_DESTRUCTOR_CC
171 __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
173 delete static_cast<pointer>(__p);
177 __thread_specific_ptr<_Tp>::__thread_specific_ptr()
180 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
182 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
186 __thread_specific_ptr<_Tp>::~__thread_specific_ptr()
188 // __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.
196 __thread_specific_ptr<_Tp>::set_pointer(pointer __p)
198 _LIBCPP_ASSERT(get() == nullptr,
199 "Attempting to overwrite thread local data");
200 __libcpp_tls_set(__key_, __p);
204 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
205 : public unary_function<__thread_id, size_t>
207 _LIBCPP_INLINE_VISIBILITY
208 size_t operator()(__thread_id __v) const _NOEXCEPT
210 return hash<__libcpp_thread_id>()(__v.__id_);
214 template<class _CharT, class _Traits>
215 _LIBCPP_INLINE_VISIBILITY
216 basic_ostream<_CharT, _Traits>&
217 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
218 {return __os << __id.__id_;}
220 class _LIBCPP_TYPE_VIS thread
222 __libcpp_thread_t __t_;
224 thread(const thread&);
225 thread& operator=(const thread&);
227 typedef __thread_id id;
228 typedef __libcpp_thread_t native_handle_type;
230 _LIBCPP_INLINE_VISIBILITY
231 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
232 #ifndef _LIBCPP_CXX03_LANG
233 template <class _Fp, class ..._Args,
234 class = typename enable_if
236 !is_same<typename __uncvref<_Fp>::type, thread>::value
239 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
240 explicit thread(_Fp&& __f, _Args&&... __args);
241 #else // _LIBCPP_CXX03_LANG
243 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
244 explicit thread(_Fp __f);
248 #ifndef _LIBCPP_CXX03_LANG
249 _LIBCPP_INLINE_VISIBILITY
250 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
251 _LIBCPP_INLINE_VISIBILITY
252 thread& operator=(thread&& __t) _NOEXCEPT;
253 #endif // _LIBCPP_CXX03_LANG
255 _LIBCPP_INLINE_VISIBILITY
256 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
258 _LIBCPP_INLINE_VISIBILITY
259 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
262 _LIBCPP_INLINE_VISIBILITY
263 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
264 _LIBCPP_INLINE_VISIBILITY
265 native_handle_type native_handle() _NOEXCEPT {return __t_;}
267 static unsigned hardware_concurrency() _NOEXCEPT;
270 #ifndef _LIBCPP_CXX03_LANG
272 template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
273 inline _LIBCPP_INLINE_VISIBILITY
275 __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
277 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
281 void* __thread_proxy(void* __vp)
283 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
284 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
285 __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
286 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
287 __thread_execute(*__p, _Index());
291 template <class _Fp, class ..._Args,
294 thread::thread(_Fp&& __f, _Args&&... __args)
296 typedef unique_ptr<__thread_struct> _TSPtr;
297 _TSPtr __tsp(new __thread_struct);
298 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
299 _VSTD::unique_ptr<_Gp> __p(
300 new _Gp(std::move(__tsp),
301 __decay_copy(_VSTD::forward<_Fp>(__f)),
302 __decay_copy(_VSTD::forward<_Args>(__args))...));
303 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
307 __throw_system_error(__ec, "thread constructor failed");
312 thread::operator=(thread&& __t) _NOEXCEPT
314 if (!__libcpp_thread_isnull(&__t_))
317 __t.__t_ = _LIBCPP_NULL_THREAD;
321 #else // _LIBCPP_CXX03_LANG
324 struct __thread_invoke_pair {
325 // This type is used to pass memory for thread local storage and a functor
326 // to a newly created thread because std::pair doesn't work with
327 // std::unique_ptr in C++03.
328 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
329 unique_ptr<__thread_struct> __tsp_;
334 void* __thread_proxy_cxx03(void* __vp)
336 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
337 __thread_local_data().set_pointer(__p->__tsp_.release());
343 thread::thread(_Fp __f)
346 typedef __thread_invoke_pair<_Fp> _InvokePair;
347 typedef std::unique_ptr<_InvokePair> _PairPtr;
348 _PairPtr __pp(new _InvokePair(__f));
349 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
353 __throw_system_error(__ec, "thread constructor failed");
356 #endif // _LIBCPP_CXX03_LANG
358 inline _LIBCPP_INLINE_VISIBILITY
359 void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
361 namespace this_thread
364 _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
366 template <class _Rep, class _Period>
368 sleep_for(const chrono::duration<_Rep, _Period>& __d)
370 using namespace chrono;
371 if (__d > duration<_Rep, _Period>::zero())
373 #if defined(_LIBCPP_COMPILER_GCC) && (__powerpc__ || __POWERPC__)
374 // GCC's long double const folding is incomplete for IBM128 long doubles.
375 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
377 _LIBCPP_CONSTEXPR duration<long double> _Max = duration<long double>(ULLONG_MAX/1000000000ULL) ;
382 __ns = duration_cast<nanoseconds>(__d);
387 __ns = nanoseconds::max();
392 template <class _Clock, class _Duration>
394 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
396 using namespace chrono;
398 condition_variable __cv;
399 unique_lock<mutex> __lk(__mut);
400 while (_Clock::now() < __t)
401 __cv.wait_until(__lk, __t);
404 template <class _Duration>
405 inline _LIBCPP_INLINE_VISIBILITY
407 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
409 using namespace chrono;
410 sleep_for(__t - steady_clock::now());
413 inline _LIBCPP_INLINE_VISIBILITY
414 void yield() _NOEXCEPT {__libcpp_thread_yield();}
418 _LIBCPP_END_NAMESPACE_STD
420 #endif // !_LIBCPP_HAS_NO_THREADS
424 #endif // _LIBCPP_THREAD