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_HAS_NO_VARIADICS
103 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
104 #pragma GCC system_header
107 #define __STDCPP_THREADS__ __cplusplus
109 _LIBCPP_BEGIN_NAMESPACE_STD
112 class __thread_specific_ptr
114 pthread_key_t __key_;
116 __thread_specific_ptr(const __thread_specific_ptr&);
117 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
119 static void __at_thread_exit(void*);
121 typedef _Tp* pointer;
123 __thread_specific_ptr();
124 ~__thread_specific_ptr();
126 _LIBCPP_INLINE_VISIBILITY
127 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
128 _LIBCPP_INLINE_VISIBILITY
129 pointer operator*() const {return *get();}
130 _LIBCPP_INLINE_VISIBILITY
131 pointer operator->() const {return get();}
133 void reset(pointer __p = nullptr);
138 __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
140 delete static_cast<pointer>(__p);
144 __thread_specific_ptr<_Tp>::__thread_specific_ptr()
146 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
147 #ifndef _LIBCPP_NO_EXCEPTIONS
149 throw system_error(error_code(__ec, system_category()),
150 "__thread_specific_ptr construction failed");
155 __thread_specific_ptr<_Tp>::~__thread_specific_ptr()
157 pthread_key_delete(__key_);
161 typename __thread_specific_ptr<_Tp>::pointer
162 __thread_specific_ptr<_Tp>::release()
165 pthread_setspecific(__key_, 0);
171 __thread_specific_ptr<_Tp>::reset(pointer __p)
173 pointer __p_old = get();
174 pthread_setspecific(__key_, __p);
178 class _LIBCPP_TYPE_VIS thread;
179 class _LIBCPP_TYPE_VIS __thread_id;
181 namespace this_thread
184 _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
188 class _LIBCPP_TYPE_VIS __thread_id;
189 template<> struct _LIBCPP_TYPE_VIS hash<__thread_id>;
191 class _LIBCPP_TYPE_VIS __thread_id
193 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
194 // NULL is the no-thread value on Darwin. Someone needs to check
195 // on other platforms. We assume 0 works everywhere for now.
199 _LIBCPP_INLINE_VISIBILITY
200 __thread_id() _NOEXCEPT : __id_(0) {}
202 friend _LIBCPP_INLINE_VISIBILITY
203 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
204 {return __x.__id_ == __y.__id_;}
205 friend _LIBCPP_INLINE_VISIBILITY
206 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
207 {return !(__x == __y);}
208 friend _LIBCPP_INLINE_VISIBILITY
209 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
210 {return __x.__id_ < __y.__id_;}
211 friend _LIBCPP_INLINE_VISIBILITY
212 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
213 {return !(__y < __x);}
214 friend _LIBCPP_INLINE_VISIBILITY
215 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
217 friend _LIBCPP_INLINE_VISIBILITY
218 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
219 {return !(__x < __y);}
221 template<class _CharT, class _Traits>
223 _LIBCPP_INLINE_VISIBILITY
224 basic_ostream<_CharT, _Traits>&
225 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
226 {return __os << __id.__id_;}
229 _LIBCPP_INLINE_VISIBILITY
230 __thread_id(pthread_t __id) : __id_(__id) {}
232 friend __thread_id this_thread::get_id() _NOEXCEPT;
233 friend class _LIBCPP_TYPE_VIS thread;
234 friend struct _LIBCPP_TYPE_VIS hash<__thread_id>;
238 struct _LIBCPP_TYPE_VIS hash<__thread_id>
239 : public unary_function<__thread_id, size_t>
241 _LIBCPP_INLINE_VISIBILITY
242 size_t operator()(__thread_id __v) const
244 return hash<pthread_t>()(__v.__id_);
248 namespace this_thread
251 inline _LIBCPP_INLINE_VISIBILITY
255 return pthread_self();
260 class _LIBCPP_TYPE_VIS thread
264 thread(const thread&);
265 thread& operator=(const thread&);
267 typedef __thread_id id;
268 typedef pthread_t native_handle_type;
270 _LIBCPP_INLINE_VISIBILITY
271 thread() _NOEXCEPT : __t_(0) {}
272 #ifndef _LIBCPP_HAS_NO_VARIADICS
273 template <class _Fp, class ..._Args,
274 class = typename enable_if
276 !is_same<typename decay<_Fp>::type, thread>::value
279 explicit thread(_Fp&& __f, _Args&&... __args);
280 #else // _LIBCPP_HAS_NO_VARIADICS
281 template <class _Fp> explicit thread(_Fp __f);
285 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
286 _LIBCPP_INLINE_VISIBILITY
287 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
288 thread& operator=(thread&& __t) _NOEXCEPT;
289 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
291 _LIBCPP_INLINE_VISIBILITY
292 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
294 _LIBCPP_INLINE_VISIBILITY
295 bool joinable() const _NOEXCEPT {return __t_ != 0;}
298 _LIBCPP_INLINE_VISIBILITY
299 id get_id() const _NOEXCEPT {return __t_;}
300 _LIBCPP_INLINE_VISIBILITY
301 native_handle_type native_handle() _NOEXCEPT {return __t_;}
303 static unsigned hardware_concurrency() _NOEXCEPT;
306 class __assoc_sub_state;
308 class _LIBCPP_HIDDEN __thread_struct_imp;
310 class __thread_struct
312 __thread_struct_imp* __p_;
314 __thread_struct(const __thread_struct&);
315 __thread_struct& operator=(const __thread_struct&);
320 void notify_all_at_thread_exit(condition_variable*, mutex*);
321 void __make_ready_at_thread_exit(__assoc_sub_state*);
324 __thread_specific_ptr<__thread_struct>& __thread_local_data();
326 #ifndef _LIBCPP_HAS_NO_VARIADICS
328 template <class _Fp, class ..._Args, size_t ..._Indices>
329 inline _LIBCPP_INLINE_VISIBILITY
331 __thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
333 __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
338 __thread_proxy(void* __vp)
340 __thread_local_data().reset(new __thread_struct);
341 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
342 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
343 __thread_execute(*__p, _Index());
347 template <class _Fp, class ..._Args,
350 thread::thread(_Fp&& __f, _Args&&... __args)
352 typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
353 _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
354 __decay_copy(_VSTD::forward<_Args>(__args))...));
355 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
359 __throw_system_error(__ec, "thread constructor failed");
362 #else // _LIBCPP_HAS_NO_VARIADICS
366 __thread_proxy(void* __vp)
368 __thread_local_data().reset(new __thread_struct);
369 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
375 thread::thread(_Fp __f)
377 std::unique_ptr<_Fp> __p(new _Fp(__f));
378 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
382 __throw_system_error(__ec, "thread constructor failed");
385 #endif // _LIBCPP_HAS_NO_VARIADICS
387 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
389 inline _LIBCPP_INLINE_VISIBILITY
391 thread::operator=(thread&& __t) _NOEXCEPT
400 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
402 inline _LIBCPP_INLINE_VISIBILITY
403 void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
405 namespace this_thread
408 void sleep_for(const chrono::nanoseconds& ns);
410 template <class _Rep, class _Period>
412 sleep_for(const chrono::duration<_Rep, _Period>& __d)
414 using namespace chrono;
415 if (__d > duration<_Rep, _Period>::zero())
417 _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
421 __ns = duration_cast<nanoseconds>(__d);
426 __ns = nanoseconds::max();
431 template <class _Clock, class _Duration>
433 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
435 using namespace chrono;
437 condition_variable __cv;
438 unique_lock<mutex> __lk(__mut);
439 while (_Clock::now() < __t)
440 __cv.wait_until(__lk, __t);
443 template <class _Duration>
444 inline _LIBCPP_INLINE_VISIBILITY
446 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
448 using namespace chrono;
449 sleep_for(__t - steady_clock::now());
452 inline _LIBCPP_INLINE_VISIBILITY
453 void yield() _NOEXCEPT {sched_yield();}
457 _LIBCPP_END_NAMESPACE_STD
459 #endif // _LIBCPP_THREAD