blob: 06cb5cac13307f257c8a7fc04a456170d6312db9 [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
121 pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
122 pointer operator*() const {return *get();}
123 pointer operator->() const {return get();}
124 pointer release();
125 void reset(pointer __p = nullptr);
126};
127
128template <class _Tp>
129void
130__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
131{
132 delete static_cast<pointer>(__p);
133}
134
135template <class _Tp>
136__thread_specific_ptr<_Tp>::__thread_specific_ptr()
137{
138 int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
139 if (__ec)
140 throw system_error(error_code(__ec, system_category()),
141 "__thread_specific_ptr construction failed");
142}
143
144template <class _Tp>
145__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
146{
147 pthread_key_delete(__key_);
148}
149
150template <class _Tp>
151typename __thread_specific_ptr<_Tp>::pointer
152__thread_specific_ptr<_Tp>::release()
153{
154 pointer __p = get();
155 pthread_setspecific(__key_, 0);
156 return __p;
157}
158
159template <class _Tp>
160void
161__thread_specific_ptr<_Tp>::reset(pointer __p)
162{
163 pointer __p_old = get();
164 pthread_setspecific(__key_, __p);
165 delete __p_old;
166}
167
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168class thread;
169class __thread_id;
170
171namespace this_thread
172{
173
174__thread_id get_id();
175
176} // this_thread
177
178class __thread_id
179{
Howard Hinnantadff4892010-05-24 17:49:41 +0000180 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
181 // NULL is the no-thread value on Darwin. Someone needs to check
182 // on other platforms. We assume 0 works everywhere for now.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000183 pthread_t __id_;
184
185public:
186 __thread_id() : __id_(0) {}
187
188 friend bool operator==(__thread_id __x, __thread_id __y)
189 {return __x.__id_ == __y.__id_;}
190 friend bool operator!=(__thread_id __x, __thread_id __y)
191 {return !(__x == __y);}
192 friend bool operator< (__thread_id __x, __thread_id __y)
193 {return __x.__id_ < __y.__id_;}
194 friend bool operator<=(__thread_id __x, __thread_id __y)
195 {return !(__y < __x);}
196 friend bool operator> (__thread_id __x, __thread_id __y)
197 {return __y < __x ;}
198 friend bool operator>=(__thread_id __x, __thread_id __y)
199 {return !(__x < __y);}
200
201 template<class _CharT, class _Traits>
202 friend
203 basic_ostream<_CharT, _Traits>&
204 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
205 {return __os << __id.__id_;}
206
207private:
208 __thread_id(pthread_t __id) : __id_(__id) {}
209
210 friend __thread_id this_thread::get_id();
211 friend class thread;
212};
213
214template<class _Tp> struct hash;
215
216template<>
217struct hash<__thread_id>
218 : public unary_function<__thread_id, size_t>
219{
220 size_t operator()(__thread_id __v) const
221 {
222 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
223 return *__p;
224 }
225};
226
227namespace this_thread
228{
229
230inline
231__thread_id
232get_id()
233{
234 return pthread_self();
235}
236
237} // this_thread
238
239class thread
240{
241 pthread_t __t_;
242
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000243#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
244 thread(const thread&) = delete;
245 thread& operator=(const thread&) = delete;
Howard Hinnant324bb032010-08-22 00:02:43 +0000246#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000247 thread(const thread&);
248 thread& operator=(const thread&);
Howard Hinnant324bb032010-08-22 00:02:43 +0000249#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250public:
251 typedef __thread_id id;
252 typedef pthread_t native_handle_type;
253
254 thread() : __t_(0) {}
255#ifndef _LIBCPP_HAS_NO_VARIADICS
256 template <class _F, class ..._Args,
257 class = typename enable_if
258 <
259 !is_same<typename decay<_F>::type, thread>::value
260 >::type
261 >
262 explicit thread(_F&& __f, _Args&&... __args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000263#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264 template <class _F> explicit thread(_F __f);
265#endif
266 ~thread();
267
Howard Hinnant73d21a42010-09-04 23:28:19 +0000268#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269 thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270 thread& operator=(thread&& __t);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000271#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272
273 void swap(thread& __t) {_STD::swap(__t_, __t.__t_);}
274
Howard Hinnantadff4892010-05-24 17:49:41 +0000275 bool joinable() const {return __t_ != 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276 void join();
277 void detach();
278 id get_id() const {return __t_;}
279 native_handle_type native_handle() {return __t_;}
280
281 static unsigned hardware_concurrency();
282};
283
Howard Hinnant47499b12010-08-27 20:10:19 +0000284class __assoc_sub_state;
285
286class __thread_struct_imp;
287
288class __thread_struct
289{
290 __thread_struct_imp* __p_;
291
292 __thread_struct(const __thread_struct&);
293 __thread_struct& operator=(const __thread_struct&);
294public:
295 __thread_struct();
296 ~__thread_struct();
297
Howard Hinnante6e4d012010-09-03 21:46:37 +0000298 void notify_all_at_thread_exit(condition_variable*, mutex*);
Howard Hinnant47499b12010-08-27 20:10:19 +0000299 void __make_ready_at_thread_exit(__assoc_sub_state*);
300};
301
302extern __thread_specific_ptr<__thread_struct> __thread_local_data;
303
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304template <class _F>
305void*
306__thread_proxy(void* __vp)
307{
Howard Hinnant47499b12010-08-27 20:10:19 +0000308 __thread_local_data.reset(new __thread_struct);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309 std::unique_ptr<_F> __p(static_cast<_F*>(__vp));
310 (*__p)();
311 return nullptr;
312}
313
314#ifndef _LIBCPP_HAS_NO_VARIADICS
315
316template <class _F, class ..._Args,
317 class
318 >
319thread::thread(_F&& __f, _Args&&... __args)
320{
321 typedef decltype(bind(std::forward<_F>(__f), std::forward<_Args>(__args)...)) _G;
322 std::unique_ptr<_G> __p(new _G(bind(std::forward<_F>(__f),
323 std::forward<_Args>(__args)...)));
324 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_G>, __p.get());
325 if (__ec == 0)
326 __p.release();
327 else
328 __throw_system_error(__ec, "thread constructor failed");
329}
330
Howard Hinnant324bb032010-08-22 00:02:43 +0000331#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332
333template <class _F>
334thread::thread(_F __f)
335{
336 std::unique_ptr<_F> __p(new _F(__f));
337 int __ec = pthread_create(&__t_, 0, &__thread_proxy<_F>, __p.get());
338 if (__ec == 0)
339 __p.release();
340 else
341 __throw_system_error(__ec, "thread constructor failed");
342}
343
Howard Hinnant324bb032010-08-22 00:02:43 +0000344#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345
Howard Hinnant73d21a42010-09-04 23:28:19 +0000346#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347
348inline
349thread&
350thread::operator=(thread&& __t)
351{
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000352 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353 terminate();
354 __t_ = __t.__t_;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000355 __t.__t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356 return *this;
357}
358
Howard Hinnant73d21a42010-09-04 23:28:19 +0000359#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360
361inline
362void swap(thread& __x, thread& __y) {__x.swap(__y);}
363
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364namespace this_thread
365{
366
367void sleep_for(const chrono::nanoseconds& ns);
368
369template <class _Rep, class _Period>
370void
371sleep_for(const chrono::duration<_Rep, _Period>& __d)
372{
373 using namespace chrono;
374 nanoseconds __ns = duration_cast<nanoseconds>(__d);
375 if (__ns < __d)
376 ++__ns;
377 sleep_for(__ns);
378}
379
380template <class _Clock, class _Duration>
381void
382sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
383{
384 using namespace chrono;
385 mutex __mut;
386 condition_variable __cv;
387 unique_lock<mutex> __lk(__mut);
388 while (_Clock::now() < __t)
389 __cv.wait_until(__lk, __t);
390}
391
392template <class _Duration>
393inline
394void
395sleep_until(const chrono::time_point<chrono::monotonic_clock, _Duration>& __t)
396{
397 using namespace chrono;
398 sleep_for(__t - monotonic_clock::now());
399}
400
401inline
402void yield() {sched_yield();}
403
404} // this_thread
405
406_LIBCPP_END_NAMESPACE_STD
407
408#endif // _LIBCPP_THREAD