blob: 7b6cf9323f1a67bee09ad91220fb8114b1b615ee [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//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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>
98#include <pthread.h>
99
100#pragma GCC system_header
101
Howard Hinnanta785e4e2010-08-21 21:01:59 +0000102#define __STDCPP_THREADS__ __cplusplus
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103
104_LIBCPP_BEGIN_NAMESPACE_STD
105
Howard Hinnant47499b12010-08-27 20:10:19 +0000106template <class _Tp>
107class __thread_specific_ptr
108{
109 pthread_key_t __key_;
110
111 __thread_specific_ptr(const __thread_specific_ptr&);
112 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
113
114 static void __at_thread_exit(void*);
115public:
116 typedef _Tp* pointer;
117
118 __thread_specific_ptr();
119 ~__thread_specific_ptr();
120
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000122 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000124 pointer operator*() const {return *get();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000126 pointer operator->() const {return get();}
127 pointer release();
128 void reset(pointer __p = nullptr);
129};
130
131template <class _Tp>
132void
133__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
134{
135 delete static_cast<pointer>(__p);
136}
137
138template <class _Tp>
139__thread_specific_ptr<_Tp>::__thread_specific_ptr()
140{
141 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
142 if (__ec)
143 throw system_error(error_code(__ec, system_category()),
144 "__thread_specific_ptr construction failed");
145}
146
147template <class _Tp>
148__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
149{
150 pthread_key_delete(__key_);
151}
152
153template <class _Tp>
154typename __thread_specific_ptr<_Tp>::pointer
155__thread_specific_ptr<_Tp>::release()
156{
157 pointer __p = get();
158 pthread_setspecific(__key_, 0);
159 return __p;
160}
161
162template <class _Tp>
163void
164__thread_specific_ptr<_Tp>::reset(pointer __p)
165{
166 pointer __p_old = get();
167 pthread_setspecific(__key_, __p);
168 delete __p_old;
169}
170
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171class thread;
172class __thread_id;
173
174namespace this_thread
175{
176
177__thread_id get_id();
178
179} // this_thread
180
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000181class _LIBCPP_VISIBLE __thread_id
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182{
Howard Hinnantadff4892010-05-24 17:49:41 +0000183 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
184 // NULL is the no-thread value on Darwin. Someone needs to check
185 // on other platforms. We assume 0 works everywhere for now.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000186 pthread_t __id_;
187
188public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190 __thread_id() : __id_(0) {}
191
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000192 friend _LIBCPP_INLINE_VISIBILITY
193 bool operator==(__thread_id __x, __thread_id __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194 {return __x.__id_ == __y.__id_;}
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 == __y);}
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.__id_ < __y.__id_;}
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 !(__y < __x);}
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 !(__x < __y);}
210
211 template<class _CharT, class _Traits>
212 friend
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214 basic_ostream<_CharT, _Traits>&
215 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
216 {return __os << __id.__id_;}
217
218private:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220 __thread_id(pthread_t __id) : __id_(__id) {}
221
222 friend __thread_id this_thread::get_id();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000223 friend class _LIBCPP_VISIBLE thread;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000224};
225
226template<class _Tp> struct hash;
227
228template<>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000229struct _LIBCPP_VISIBLE hash<__thread_id>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230 : public unary_function<__thread_id, size_t>
231{
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233 size_t operator()(__thread_id __v) const
234 {
235 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
236 return *__p;
237 }
238};
239
240namespace this_thread
241{
242
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000243inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244__thread_id
245get_id()
246{
247 return pthread_self();
248}
249
250} // this_thread
251
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000252class _LIBCPP_VISIBLE thread
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253{
254 pthread_t __t_;
255
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000256#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
257 thread(const thread&) = delete;
258 thread& operator=(const thread&) = delete;
Howard Hinnant324bb032010-08-22 00:02:43 +0000259#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000260 thread(const thread&);
261 thread& operator=(const thread&);
Howard Hinnant324bb032010-08-22 00:02:43 +0000262#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263public:
264 typedef __thread_id id;
265 typedef pthread_t native_handle_type;
266
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268 thread() : __t_(0) {}
269#ifndef _LIBCPP_HAS_NO_VARIADICS
270 template <class _F, class ..._Args,
271 class = typename enable_if
272 <
273 !is_same<typename decay<_F>::type, thread>::value
274 >::type
275 >
276 explicit thread(_F&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000277#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278 template <class _F> explicit thread(_F __f);
279#endif
280 ~thread();
281
Howard Hinnant73d21a42010-09-04 23:28:19 +0000282#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284 thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285 thread& operator=(thread&& __t);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000286#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000289 void swap(thread& __t) {_STD::swap(__t_, __t.__t_);}
290
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantadff4892010-05-24 17:49:41 +0000292 bool joinable() const {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 void join();
294 void detach();
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296 id get_id() const {return __t_;}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298 native_handle_type native_handle() {return __t_;}
299
300 static unsigned hardware_concurrency();
301};
302
Howard Hinnant47499b12010-08-27 20:10:19 +0000303class __assoc_sub_state;
304
305class __thread_struct_imp;
306
307class __thread_struct
308{
309 __thread_struct_imp* __p_;
310
311 __thread_struct(const __thread_struct&);
312 __thread_struct& operator=(const __thread_struct&);
313public:
314 __thread_struct();
315 ~__thread_struct();
316
Howard Hinnante6e4d012010-09-03 21:46:37 +0000317 void notify_all_at_thread_exit(condition_variable*, mutex*);
Howard Hinnant47499b12010-08-27 20:10:19 +0000318 void __make_ready_at_thread_exit(__assoc_sub_state*);
319};
320
Howard Hinnant5306d682010-10-14 19:18:04 +0000321__thread_specific_ptr<__thread_struct>& __thread_local_data();
Howard Hinnant47499b12010-08-27 20:10:19 +0000322
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323template <class _F>
324void*
325__thread_proxy(void* __vp)
326{
Howard Hinnant5306d682010-10-14 19:18:04 +0000327 __thread_local_data().reset(new __thread_struct);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328 std::unique_ptr<_F> __p(static_cast<_F*>(__vp));
329 (*__p)();
330 return nullptr;
331}
332
333#ifndef _LIBCPP_HAS_NO_VARIADICS
334
335template <class _F, class ..._Args,
336 class
337 >
338thread::thread(_F&& __f, _Args&&... __args)
339{
340 typedef decltype(bind(std::forward<_F>(__f), std::forward<_Args>(__args)...)) _G;
341 std::unique_ptr<_G> __p(new _G(bind(std::forward<_F>(__f),
342 std::forward<_Args>(__args)...)));
343 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_G>, __p.get());
344 if (__ec == 0)
345 __p.release();
346 else
347 __throw_system_error(__ec, "thread constructor failed");
348}
349
Howard Hinnant324bb032010-08-22 00:02:43 +0000350#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
352template <class _F>
353thread::thread(_F __f)
354{
355 std::unique_ptr<_F> __p(new _F(__f));
356 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_F>, __p.get());
357 if (__ec == 0)
358 __p.release();
359 else
360 __throw_system_error(__ec, "thread constructor failed");
361}
362
Howard Hinnant324bb032010-08-22 00:02:43 +0000363#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
Howard Hinnant73d21a42010-09-04 23:28:19 +0000365#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000367inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368thread&
369thread::operator=(thread&& __t)
370{
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000371 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372 terminate();
373 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000374 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375 return *this;
376}
377
Howard Hinnant73d21a42010-09-04 23:28:19 +0000378#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000380inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381void swap(thread& __x, thread& __y) {__x.swap(__y);}
382
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383namespace this_thread
384{
385
386void sleep_for(const chrono::nanoseconds& ns);
387
388template <class _Rep, class _Period>
389void
390sleep_for(const chrono::duration<_Rep, _Period>& __d)
391{
392 using namespace chrono;
393 nanoseconds __ns = duration_cast<nanoseconds>(__d);
394 if (__ns < __d)
395 ++__ns;
396 sleep_for(__ns);
397}
398
399template <class _Clock, class _Duration>
400void
401sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
402{
403 using namespace chrono;
404 mutex __mut;
405 condition_variable __cv;
406 unique_lock<mutex> __lk(__mut);
407 while (_Clock::now() < __t)
408 __cv.wait_until(__lk, __t);
409}
410
411template <class _Duration>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413void
414sleep_until(const chrono::time_point<chrono::monotonic_clock, _Duration>& __t)
415{
416 using namespace chrono;
417 sleep_for(__t - monotonic_clock::now());
418}
419
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421void yield() {sched_yield();}
422
423} // this_thread
424
425_LIBCPP_END_NAMESPACE_STD
426
427#endif // _LIBCPP_THREAD