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;
36 thread& operator=(const thread&) = delete;
37 thread& operator=(thread&& t);
41 bool joinable() const;
45 native_handle_type native_handle();
47 static unsigned hardware_concurrency();
50 void swap(thread& x, thread& y);
58 bool operator==(thread::id x, thread::id y);
59 bool operator!=(thread::id x, thread::id y);
60 bool operator< (thread::id x, thread::id y);
61 bool operator<=(thread::id x, thread::id y);
62 bool operator> (thread::id x, thread::id y);
63 bool operator>=(thread::id x, thread::id y);
65 template<class charT, class traits>
66 basic_ostream<charT, traits>&
67 operator<<(basic_ostream<charT, traits>& out, thread::id id);
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);
148 throw system_error(error_code(__ec, system_category()),
149 "__thread_specific_ptr construction failed");
153 __thread_specific_ptr<_Tp>::~__thread_specific_ptr()
155 pthread_key_delete(__key_);
159 typename __thread_specific_ptr<_Tp>::pointer
160 __thread_specific_ptr<_Tp>::release()
163 pthread_setspecific(__key_, 0);
169 __thread_specific_ptr<_Tp>::reset(pointer __p)
171 pointer __p_old = get();
172 pthread_setspecific(__key_, __p);
179 namespace this_thread
182 __thread_id get_id();
186 class _LIBCPP_VISIBLE __thread_id;
187 template<> struct _LIBCPP_VISIBLE hash<__thread_id>;
189 class _LIBCPP_VISIBLE __thread_id
191 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
192 // NULL is the no-thread value on Darwin. Someone needs to check
193 // on other platforms. We assume 0 works everywhere for now.
197 _LIBCPP_INLINE_VISIBILITY
198 __thread_id() : __id_(0) {}
200 friend _LIBCPP_INLINE_VISIBILITY
201 bool operator==(__thread_id __x, __thread_id __y)
202 {return __x.__id_ == __y.__id_;}
203 friend _LIBCPP_INLINE_VISIBILITY
204 bool operator!=(__thread_id __x, __thread_id __y)
205 {return !(__x == __y);}
206 friend _LIBCPP_INLINE_VISIBILITY
207 bool operator< (__thread_id __x, __thread_id __y)
208 {return __x.__id_ < __y.__id_;}
209 friend _LIBCPP_INLINE_VISIBILITY
210 bool operator<=(__thread_id __x, __thread_id __y)
211 {return !(__y < __x);}
212 friend _LIBCPP_INLINE_VISIBILITY
213 bool operator> (__thread_id __x, __thread_id __y)
215 friend _LIBCPP_INLINE_VISIBILITY
216 bool operator>=(__thread_id __x, __thread_id __y)
217 {return !(__x < __y);}
219 template<class _CharT, class _Traits>
221 _LIBCPP_INLINE_VISIBILITY
222 basic_ostream<_CharT, _Traits>&
223 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
224 {return __os << __id.__id_;}
227 _LIBCPP_INLINE_VISIBILITY
228 __thread_id(pthread_t __id) : __id_(__id) {}
230 friend __thread_id this_thread::get_id();
231 friend class _LIBCPP_VISIBLE thread;
232 friend struct _LIBCPP_VISIBLE hash<__thread_id>;
236 struct _LIBCPP_VISIBLE hash<__thread_id>
237 : public unary_function<__thread_id, size_t>
239 _LIBCPP_INLINE_VISIBILITY
240 size_t operator()(__thread_id __v) const
242 return hash<pthread_t>()(__v.__id_);
246 namespace this_thread
249 inline _LIBCPP_INLINE_VISIBILITY
253 return pthread_self();
258 class _LIBCPP_VISIBLE thread
262 thread(const thread&);
263 thread& operator=(const thread&);
265 typedef __thread_id id;
266 typedef pthread_t native_handle_type;
268 _LIBCPP_INLINE_VISIBILITY
269 thread() : __t_(0) {}
270 #ifndef _LIBCPP_HAS_NO_VARIADICS
271 template <class _Fp, class ..._Args,
272 class = typename enable_if
274 !is_same<typename decay<_Fp>::type, thread>::value
277 explicit thread(_Fp&& __f, _Args&&... __args);
278 #else // _LIBCPP_HAS_NO_VARIADICS
279 template <class _Fp> explicit thread(_Fp __f);
283 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
284 _LIBCPP_INLINE_VISIBILITY
285 thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;}
286 thread& operator=(thread&& __t);
287 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
289 _LIBCPP_INLINE_VISIBILITY
290 void swap(thread& __t) {_VSTD::swap(__t_, __t.__t_);}
292 _LIBCPP_INLINE_VISIBILITY
293 bool joinable() const {return __t_ != 0;}
296 _LIBCPP_INLINE_VISIBILITY
297 id get_id() const {return __t_;}
298 _LIBCPP_INLINE_VISIBILITY
299 native_handle_type native_handle() {return __t_;}
301 static unsigned hardware_concurrency();
304 class __assoc_sub_state;
306 class _LIBCPP_HIDDEN __thread_struct_imp;
308 class __thread_struct
310 __thread_struct_imp* __p_;
312 __thread_struct(const __thread_struct&);
313 __thread_struct& operator=(const __thread_struct&);
318 void notify_all_at_thread_exit(condition_variable*, mutex*);
319 void __make_ready_at_thread_exit(__assoc_sub_state*);
322 __thread_specific_ptr<__thread_struct>& __thread_local_data();
324 #ifndef _LIBCPP_HAS_NO_VARIADICS
326 template <class _Fp, class ..._Args, size_t ..._Indices>
327 inline _LIBCPP_INLINE_VISIBILITY
329 __threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
331 __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
336 __thread_proxy(void* __vp)
338 __thread_local_data().reset(new __thread_struct);
339 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
340 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
341 __threaad_execute(*__p, _Index());
345 template <class _Fp, class ..._Args,
348 thread::thread(_Fp&& __f, _Args&&... __args)
350 typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
351 _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
352 __decay_copy(_VSTD::forward<_Args>(__args))...));
353 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
357 __throw_system_error(__ec, "thread constructor failed");
360 #else // _LIBCPP_HAS_NO_VARIADICS
364 __thread_proxy(void* __vp)
366 __thread_local_data().reset(new __thread_struct);
367 std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
373 thread::thread(_Fp __f)
375 std::unique_ptr<_Fp> __p(new _Fp(__f));
376 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
380 __throw_system_error(__ec, "thread constructor failed");
383 #endif // _LIBCPP_HAS_NO_VARIADICS
385 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
387 inline _LIBCPP_INLINE_VISIBILITY
389 thread::operator=(thread&& __t)
398 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
400 inline _LIBCPP_INLINE_VISIBILITY
401 void swap(thread& __x, thread& __y) {__x.swap(__y);}
403 namespace this_thread
406 void sleep_for(const chrono::nanoseconds& ns);
408 template <class _Rep, class _Period>
410 sleep_for(const chrono::duration<_Rep, _Period>& __d)
412 using namespace chrono;
413 nanoseconds __ns = duration_cast<nanoseconds>(__d);
419 template <class _Clock, class _Duration>
421 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
423 using namespace chrono;
425 condition_variable __cv;
426 unique_lock<mutex> __lk(__mut);
427 while (_Clock::now() < __t)
428 __cv.wait_until(__lk, __t);
431 template <class _Duration>
432 inline _LIBCPP_INLINE_VISIBILITY
434 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
436 using namespace chrono;
437 sleep_for(__t - steady_clock::now());
440 inline _LIBCPP_INLINE_VISIBILITY
441 void yield() {sched_yield();}
445 _LIBCPP_END_NAMESPACE_STD
447 #endif // _LIBCPP_THREAD