blob: a04572f6be28bc4df53f48ada695d47f37d5d051 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- thread -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_THREAD
12#define _LIBCPP_THREAD
13
14/*
15
16 thread synopsis
17
Howard Hinnanta785e4e2010-08-21 21:01:59 +000018#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
20namespace std
21{
22
23class thread
24{
25public:
26 class id;
27 typedef pthread_t native_handle_type;
28
29 thread();
30 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
31 ~thread();
32
33 thread(const thread&) = delete;
34 thread(thread&& t);
35
36 thread& operator=(const thread&) = delete;
37 thread& operator=(thread&& t);
38
39 void swap(thread& t);
40
41 bool joinable() const;
42 void join();
43 void detach();
44 id get_id() const;
45 native_handle_type native_handle();
46
47 static unsigned hardware_concurrency();
48};
49
50void swap(thread& x, thread& y);
51
52class thread::id
53{
54public:
55 id();
56};
57
58bool operator==(thread::id x, thread::id y);
59bool operator!=(thread::id x, thread::id y);
60bool operator< (thread::id x, thread::id y);
61bool operator<=(thread::id x, thread::id y);
62bool operator> (thread::id x, thread::id y);
63bool operator>=(thread::id x, thread::id y);
64
65template<class charT, class traits>
66basic_ostream<charT, traits>&
67operator<<(basic_ostream<charT, traits>& out, thread::id id);
68
69namespace this_thread
70{
71
72thread::id get_id();
73
74void yield();
75
76template <class Clock, class Duration>
77void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
78
79template <class Rep, class Period>
80void sleep_for(const chrono::duration<Rep, Period>& rel_time);
81
82} // this_thread
83
84} // std
85
86*/
87
88#include <__config>
89#include <iosfwd>
90#include <__functional_base>
91#include <type_traits>
92#include <cstddef>
93#include <functional>
94#include <memory>
95#include <system_error>
96#include <chrono>
97#include <__mutex_base>
Howard Hinnant656bdc32011-05-16 18:40:35 +000098#ifndef _LIBCPP_HAS_NO_VARIADICS
99#include <tuple>
100#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101#include <pthread.h>
102
103#pragma GCC system_header
104
Howard Hinnanta785e4e2010-08-21 21:01:59 +0000105#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106
107_LIBCPP_BEGIN_NAMESPACE_STD
108
Howard Hinnant47499b12010-08-27 20:10:19 +0000109template <class _Tp>
110class __thread_specific_ptr
111{
112 pthread_key_t __key_;
113
114 __thread_specific_ptr(const __thread_specific_ptr&);
115 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
116
117 static void __at_thread_exit(void*);
118public:
119 typedef _Tp* pointer;
120
121 __thread_specific_ptr();
122 ~__thread_specific_ptr();
123
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000125 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000127 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000129 pointer operator->() const {return get();}
130 pointer release();
131 void reset(pointer __p = nullptr);
132};
133
134template <class _Tp>
135void
136__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
137{
138 delete static_cast<pointer>(__p);
139}
140
141template <class _Tp>
142__thread_specific_ptr<_Tp>::__thread_specific_ptr()
143{
144 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
145 if (__ec)
146 throw system_error(error_code(__ec, system_category()),
147 "__thread_specific_ptr construction failed");
148}
149
150template <class _Tp>
151__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
152{
153 pthread_key_delete(__key_);
154}
155
156template <class _Tp>
157typename __thread_specific_ptr<_Tp>::pointer
158__thread_specific_ptr<_Tp>::release()
159{
160 pointer __p = get();
161 pthread_setspecific(__key_, 0);
162 return __p;
163}
164
165template <class _Tp>
166void
167__thread_specific_ptr<_Tp>::reset(pointer __p)
168{
169 pointer __p_old = get();
170 pthread_setspecific(__key_, __p);
171 delete __p_old;
172}
173
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174class thread;
175class __thread_id;
176
177namespace this_thread
178{
179
180__thread_id get_id();
181
182} // this_thread
183
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000184class _LIBCPP_VISIBLE __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185{
Howard Hinnantadff4892010-05-24 17:49:41 +0000186 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
187 // NULL is the no-thread value on Darwin. Someone needs to check
188 // on other platforms. We assume 0 works everywhere for now.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189 pthread_t __id_;
190
191public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193 __thread_id() : __id_(0) {}
194
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000195 friend _LIBCPP_INLINE_VISIBILITY
196 bool operator==(__thread_id __x, __thread_id __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197 {return __x.__id_ == __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000198 friend _LIBCPP_INLINE_VISIBILITY
199 bool operator!=(__thread_id __x, __thread_id __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200 {return !(__x == __y);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000201 friend _LIBCPP_INLINE_VISIBILITY
202 bool operator< (__thread_id __x, __thread_id __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000203 {return __x.__id_ < __y.__id_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000204 friend _LIBCPP_INLINE_VISIBILITY
205 bool operator<=(__thread_id __x, __thread_id __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206 {return !(__y < __x);}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000207 friend _LIBCPP_INLINE_VISIBILITY
208 bool operator> (__thread_id __x, __thread_id __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209 {return __y < __x ;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000210 friend _LIBCPP_INLINE_VISIBILITY
211 bool operator>=(__thread_id __x, __thread_id __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212 {return !(__x < __y);}
213
214 template<class _CharT, class _Traits>
215 friend
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 basic_ostream<_CharT, _Traits>&
218 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
219 {return __os << __id.__id_;}
220
221private:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223 __thread_id(pthread_t __id) : __id_(__id) {}
224
225 friend __thread_id this_thread::get_id();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000226 friend class _LIBCPP_VISIBLE thread;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227};
228
229template<class _Tp> struct hash;
230
231template<>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000232struct _LIBCPP_VISIBLE hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233 : public unary_function<__thread_id, size_t>
234{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236 size_t operator()(__thread_id __v) const
237 {
238 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
239 return *__p;
240 }
241};
242
243namespace this_thread
244{
245
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000246inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247__thread_id
248get_id()
249{
250 return pthread_self();
251}
252
253} // this_thread
254
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000255class _LIBCPP_VISIBLE thread
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256{
257 pthread_t __t_;
258
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000259 thread(const thread&);
260 thread& operator=(const thread&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261public:
262 typedef __thread_id id;
263 typedef pthread_t native_handle_type;
264
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266 thread() : __t_(0) {}
267#ifndef _LIBCPP_HAS_NO_VARIADICS
268 template <class _F, class ..._Args,
269 class = typename enable_if
270 <
271 !is_same<typename decay<_F>::type, thread>::value
272 >::type
273 >
274 explicit thread(_F&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000275#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276 template <class _F> explicit thread(_F __f);
277#endif
278 ~thread();
279
Howard Hinnant73d21a42010-09-04 23:28:19 +0000280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282 thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283 thread& operator=(thread&& __t);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000284#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287 void swap(thread& __t) {_STD::swap(__t_, __t.__t_);}
288
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantadff4892010-05-24 17:49:41 +0000290 bool joinable() const {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291 void join();
292 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294 id get_id() const {return __t_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296 native_handle_type native_handle() {return __t_;}
297
298 static unsigned hardware_concurrency();
299};
300
Howard Hinnant47499b12010-08-27 20:10:19 +0000301class __assoc_sub_state;
302
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000303class _LIBCPP_HIDDEN __thread_struct_imp;
Howard Hinnant47499b12010-08-27 20:10:19 +0000304
305class __thread_struct
306{
307 __thread_struct_imp* __p_;
308
309 __thread_struct(const __thread_struct&);
310 __thread_struct& operator=(const __thread_struct&);
311public:
312 __thread_struct();
313 ~__thread_struct();
314
Howard Hinnante6e4d012010-09-03 21:46:37 +0000315 void notify_all_at_thread_exit(condition_variable*, mutex*);
Howard Hinnant47499b12010-08-27 20:10:19 +0000316 void __make_ready_at_thread_exit(__assoc_sub_state*);
317};
318
Howard Hinnant5306d682010-10-14 19:18:04 +0000319__thread_specific_ptr<__thread_struct>& __thread_local_data();
Howard Hinnant47499b12010-08-27 20:10:19 +0000320
Howard Hinnant656bdc32011-05-16 18:40:35 +0000321#ifndef _LIBCPP_HAS_NO_VARIADICS
322
323template <class _F, class ..._Args, size_t ..._Indices>
324inline _LIBCPP_INLINE_VISIBILITY
325void
326__threaad_execute(tuple<_F, _Args...>& __t, __tuple_indices<_Indices...>)
327{
Howard Hinnant57cff292011-05-19 15:05:04 +0000328 __invoke(_STD::move(_STD::get<0>(__t)), _STD::move(_STD::get<_Indices>(__t))...);
Howard Hinnant656bdc32011-05-16 18:40:35 +0000329}
330
331template <class _F>
332void*
333__thread_proxy(void* __vp)
334{
335 __thread_local_data().reset(new __thread_struct);
336 std::unique_ptr<_F> __p(static_cast<_F*>(__vp));
337 typedef typename __make_tuple_indices<tuple_size<_F>::value, 1>::type _Index;
338 __threaad_execute(*__p, _Index());
339 return nullptr;
340}
341
342template <class _F, class ..._Args,
343 class
344 >
345thread::thread(_F&& __f, _Args&&... __args)
346{
347 typedef tuple<typename decay<_F>::type, typename decay<_Args>::type...> _G;
348 _STD::unique_ptr<_G> __p(new _G(__decay_copy(_STD::forward<_F>(__f)),
349 __decay_copy(_STD::forward<_Args>(__args))...));
350 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_G>, __p.get());
351 if (__ec == 0)
352 __p.release();
353 else
354 __throw_system_error(__ec, "thread constructor failed");
355}
356
357#else // _LIBCPP_HAS_NO_VARIADICS
358
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359template <class _F>
360void*
361__thread_proxy(void* __vp)
362{
Howard Hinnant5306d682010-10-14 19:18:04 +0000363 __thread_local_data().reset(new __thread_struct);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364 std::unique_ptr<_F> __p(static_cast<_F*>(__vp));
365 (*__p)();
366 return nullptr;
367}
368
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369template <class _F>
370thread::thread(_F __f)
371{
372 std::unique_ptr<_F> __p(new _F(__f));
373 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_F>, __p.get());
374 if (__ec == 0)
375 __p.release();
376 else
377 __throw_system_error(__ec, "thread constructor failed");
378}
379
Howard Hinnant324bb032010-08-22 00:02:43 +0000380#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
Howard Hinnant73d21a42010-09-04 23:28:19 +0000382#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000384inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385thread&
386thread::operator=(thread&& __t)
387{
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000388 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389 terminate();
390 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000391 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392 return *this;
393}
394
Howard Hinnant73d21a42010-09-04 23:28:19 +0000395#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000397inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398void swap(thread& __x, thread& __y) {__x.swap(__y);}
399
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400namespace this_thread
401{
402
403void sleep_for(const chrono::nanoseconds& ns);
404
405template <class _Rep, class _Period>
406void
407sleep_for(const chrono::duration<_Rep, _Period>& __d)
408{
409 using namespace chrono;
410 nanoseconds __ns = duration_cast<nanoseconds>(__d);
411 if (__ns < __d)
412 ++__ns;
413 sleep_for(__ns);
414}
415
416template <class _Clock, class _Duration>
417void
418sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
419{
420 using namespace chrono;
421 mutex __mut;
422 condition_variable __cv;
423 unique_lock<mutex> __lk(__mut);
424 while (_Clock::now() < __t)
425 __cv.wait_until(__lk, __t);
426}
427
428template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000429inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430void
Howard Hinnantf8f85212010-11-20 19:16:30 +0000431sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432{
433 using namespace chrono;
Howard Hinnantf8f85212010-11-20 19:16:30 +0000434 sleep_for(__t - steady_clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435}
436
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000437inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438void yield() {sched_yield();}
439
440} // this_thread
441
442_LIBCPP_END_NAMESPACE_STD
443
444#endif // _LIBCPP_THREAD