2 //===--------------------------- thread -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_THREAD
12 #define _LIBCPP_THREAD
18 #define __STDCPP_THREADS__ __cplusplus
27 typedef pthread_t native_handle_type;
30 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
33 thread(const thread&) = delete;
34 thread(thread&& t) noexcept;
36 thread& operator=(const thread&) = delete;
37 thread& operator=(thread&& t) noexcept;
39 void swap(thread& t) noexcept;
41 bool joinable() const noexcept;
44 id get_id() const noexcept;
45 native_handle_type native_handle();
47 static unsigned hardware_concurrency() noexcept;
50 void swap(thread& x, thread& 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;
63 bool operator>=(thread::id x, thread::id y) noexcept;
65 template<class charT, class traits>
66 basic_ostream<charT, traits>&
67 operator<<(basic_ostream<charT, traits>& out, thread::id id);
72 thread::id get_id() noexcept;
74 void yield() noexcept;
76 template <class Clock, class Duration>
77 void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
79 template <class Rep, class Period>
80 void sleep_for(const chrono::duration<Rep, Period>& rel_time);
90 #include <__functional_base>
91 #include <type_traits>
95 #include <system_error>
97 #include <__mutex_base>
98 #ifndef _LIBCPP_CXX03_LANG
101 #include <__threading_support>
104 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
105 #pragma GCC system_header
109 #include <__undef_macros>
111 #define __STDCPP_THREADS__ __cplusplus
113 #ifdef _LIBCPP_HAS_NO_THREADS
114 #error <thread> is not supported on this single threaded system
115 #else // !_LIBCPP_HAS_NO_THREADS
117 _LIBCPP_BEGIN_NAMESPACE_STD
119 template <class _Tp> class __thread_specific_ptr;
120 class _LIBCPP_TYPE_VIS __thread_struct;
121 class _LIBCPP_HIDDEN __thread_struct_imp;
122 class __assoc_sub_state;
124 _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
126 class _LIBCPP_TYPE_VIS __thread_struct
128 __thread_struct_imp* __p_;
130 __thread_struct(const __thread_struct&);
131 __thread_struct& operator=(const __thread_struct&);
136 void notify_all_at_thread_exit(condition_variable*, mutex*);
137 void __make_ready_at_thread_exit(__assoc_sub_state*);
141 class __thread_specific_ptr
143 __libcpp_tls_key __key_;
145 // Only __thread_local_data() may construct a __thread_specific_ptr
146 // and only with _Tp == __thread_struct.
147 static_assert((is_same<_Tp, __thread_struct>::value), "");
148 __thread_specific_ptr();
149 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
151 __thread_specific_ptr(const __thread_specific_ptr&);
152 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
154 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
157 typedef _Tp* pointer;
159 ~__thread_specific_ptr();
161 _LIBCPP_INLINE_VISIBILITY
162 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
163 _LIBCPP_INLINE_VISIBILITY
164 pointer operator*() const {return *get();}
165 _LIBCPP_INLINE_VISIBILITY
166 pointer operator->() const {return get();}
167 void set_pointer(pointer __p);
171 void _LIBCPP_TLS_DESTRUCTOR_CC
172 __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
174 delete static_cast<pointer>(__p);
178 __thread_specific_ptr<_Tp>::__thread_specific_ptr()
181 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
183 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
187 __thread_specific_ptr<_Tp>::~__thread_specific_ptr()
189 // __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.
197 __thread_specific_ptr<_Tp>::set_pointer(pointer __p)
199 _LIBCPP_ASSERT(get() == nullptr,
200 "Attempting to overwrite thread local data");
201 __libcpp_tls_set(__key_, __p);
205 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
206 : public unary_function<__thread_id, size_t>
208 _LIBCPP_INLINE_VISIBILITY
209 size_t operator()(__thread_id __v) const _NOEXCEPT
211 return hash<__libcpp_thread_id>()(__v.__id_);
215 template<class _CharT, class _Traits>
216 _LIBCPP_INLINE_VISIBILITY
217 basic_ostream<_CharT, _Traits>&
218 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
219 {return __os << __id.__id_;}
221 class _LIBCPP_TYPE_VIS thread
223 __libcpp_thread_t __t_;
225 thread(const thread&);
226 thread& operator=(const thread&);
228 typedef __thread_id id;
229 typedef __libcpp_thread_t native_handle_type;
231 _LIBCPP_INLINE_VISIBILITY
232 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
233 #ifndef _LIBCPP_CXX03_LANG
234 template <class _Fp, class ..._Args,
235 class = typename enable_if
237 !is_same<typename __uncvref<_Fp>::type, thread>::value
240 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
241 explicit thread(_Fp&& __f, _Args&&... __args);
242 #else // _LIBCPP_CXX03_LANG
244 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
245 explicit thread(_Fp __f);
249 #ifndef _LIBCPP_CXX03_LANG
250 _LIBCPP_INLINE_VISIBILITY
251 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
252 _LIBCPP_INLINE_VISIBILITY
253 thread& operator=(thread&& __t) _NOEXCEPT;
254 #endif // _LIBCPP_CXX03_LANG
256 _LIBCPP_INLINE_VISIBILITY
257 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
259 _LIBCPP_INLINE_VISIBILITY
260 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
263 _LIBCPP_INLINE_VISIBILITY
264 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
265 _LIBCPP_INLINE_VISIBILITY
266 native_handle_type native_handle() _NOEXCEPT {return __t_;}
268 static unsigned hardware_concurrency() _NOEXCEPT;
271 #ifndef _LIBCPP_CXX03_LANG
273 template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
274 inline _LIBCPP_INLINE_VISIBILITY
276 __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
278 __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
282 void* __thread_proxy(void* __vp)
284 // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
285 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
286 __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
287 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
288 __thread_execute(*__p, _Index());
292 template <class _Fp, class ..._Args,
295 thread::thread(_Fp&& __f, _Args&&... __args)
297 typedef unique_ptr<__thread_struct> _TSPtr;
298 _TSPtr __tsp(new __thread_struct);
299 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
300 _VSTD::unique_ptr<_Gp> __p(
301 new _Gp(std::move(__tsp),
302 __decay_copy(_VSTD::forward<_Fp>(__f)),
303 __decay_copy(_VSTD::forward<_Args>(__args))...));
304 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
308 __throw_system_error(__ec, "thread constructor failed");
313 thread::operator=(thread&& __t) _NOEXCEPT
315 if (!__libcpp_thread_isnull(&__t_))
318 __t.__t_ = _LIBCPP_NULL_THREAD;
322 #else // _LIBCPP_CXX03_LANG
325 struct __thread_invoke_pair {
326 // This type is used to pass memory for thread local storage and a functor
327 // to a newly created thread because std::pair doesn't work with
328 // std::unique_ptr in C++03.
329 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
330 unique_ptr<__thread_struct> __tsp_;
335 void* __thread_proxy_cxx03(void* __vp)
337 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
338 __thread_local_data().set_pointer(__p->__tsp_.release());
344 thread::thread(_Fp __f)
347 typedef __thread_invoke_pair<_Fp> _InvokePair;
348 typedef std::unique_ptr<_InvokePair> _PairPtr;
349 _PairPtr __pp(new _InvokePair(__f));
350 int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
354 __throw_system_error(__ec, "thread constructor failed");
357 #endif // _LIBCPP_CXX03_LANG
359 inline _LIBCPP_INLINE_VISIBILITY
360 void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
362 namespace this_thread
365 _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
367 template <class _Rep, class _Period>
369 sleep_for(const chrono::duration<_Rep, _Period>& __d)
371 using namespace chrono;
372 if (__d > duration<_Rep, _Period>::zero())
374 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
378 __ns = duration_cast<nanoseconds>(__d);
383 __ns = nanoseconds::max();
388 template <class _Clock, class _Duration>
390 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
392 using namespace chrono;
394 condition_variable __cv;
395 unique_lock<mutex> __lk(__mut);
396 while (_Clock::now() < __t)
397 __cv.wait_until(__lk, __t);
400 template <class _Duration>
401 inline _LIBCPP_INLINE_VISIBILITY
403 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
405 using namespace chrono;
406 sleep_for(__t - steady_clock::now());
409 inline _LIBCPP_INLINE_VISIBILITY
410 void yield() _NOEXCEPT {__libcpp_thread_yield();}
414 _LIBCPP_END_NAMESPACE_STD
416 #endif // !_LIBCPP_HAS_NO_THREADS
420 #endif // _LIBCPP_THREAD