]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/thread
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / include / thread
1 // -*- C++ -*-
2 //===--------------------------- thread -----------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_THREAD
12 #define _LIBCPP_THREAD
13
14 /*
15
16     thread synopsis
17
18 #define __STDCPP_THREADS__ __cplusplus
19
20 namespace std
21 {
22
23 class thread
24 {
25 public:
26     class id;
27     typedef pthread_t native_handle_type;
28
29     thread() noexcept;
30     template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
31     ~thread();
32
33     thread(const thread&) = delete;
34     thread(thread&& t) noexcept;
35
36     thread& operator=(const thread&) = delete;
37     thread& operator=(thread&& t) noexcept;
38
39     void swap(thread& t) noexcept;
40
41     bool joinable() const noexcept;
42     void join();
43     void detach();
44     id get_id() const noexcept;
45     native_handle_type native_handle();
46
47     static unsigned hardware_concurrency() noexcept;
48 };
49
50 void swap(thread& x, thread& y) noexcept;
51
52 class thread::id
53 {
54 public:
55     id() noexcept;
56 };
57
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;
64
65 template<class charT, class traits>
66 basic_ostream<charT, traits>&
67 operator<<(basic_ostream<charT, traits>& out, thread::id id);
68
69 namespace this_thread
70 {
71
72 thread::id get_id() noexcept;
73
74 void yield() noexcept;
75
76 template <class Clock, class Duration>
77 void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
78
79 template <class Rep, class Period>
80 void 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 #ifndef _LIBCPP_HAS_NO_VARIADICS
99 #include <tuple>
100 #endif
101 #include <__threading_support>
102 #include <__debug>
103
104 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
105 #pragma GCC system_header
106 #endif
107
108 #define __STDCPP_THREADS__ __cplusplus
109
110 #ifdef _LIBCPP_HAS_NO_THREADS
111 #error <thread> is not supported on this single threaded system
112 #else // !_LIBCPP_HAS_NO_THREADS
113
114 _LIBCPP_BEGIN_NAMESPACE_STD
115
116 template <class _Tp> class __thread_specific_ptr;
117 class _LIBCPP_TYPE_VIS __thread_struct;
118 class _LIBCPP_HIDDEN __thread_struct_imp;
119 class __assoc_sub_state;
120
121 _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
122
123 class _LIBCPP_TYPE_VIS __thread_struct
124 {
125     __thread_struct_imp* __p_;
126
127     __thread_struct(const __thread_struct&);
128     __thread_struct& operator=(const __thread_struct&);
129 public:
130     __thread_struct();
131     ~__thread_struct();
132
133     void notify_all_at_thread_exit(condition_variable*, mutex*);
134     void __make_ready_at_thread_exit(__assoc_sub_state*);
135 };
136
137 template <class _Tp>
138 class __thread_specific_ptr
139 {
140     __libcpp_tls_key __key_;
141
142      // Only __thread_local_data() may construct a __thread_specific_ptr
143      // and only with _Tp == __thread_struct.
144     static_assert((is_same<_Tp, __thread_struct>::value), "");
145     __thread_specific_ptr();
146     friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
147
148     __thread_specific_ptr(const __thread_specific_ptr&);
149     __thread_specific_ptr& operator=(const __thread_specific_ptr&);
150
151     static void __at_thread_exit(void*);
152 public:
153     typedef _Tp* pointer;
154
155     ~__thread_specific_ptr();
156
157     _LIBCPP_INLINE_VISIBILITY
158     pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
159     _LIBCPP_INLINE_VISIBILITY
160     pointer operator*() const {return *get();}
161     _LIBCPP_INLINE_VISIBILITY
162     pointer operator->() const {return get();}
163     void set_pointer(pointer __p);
164 };
165
166 template <class _Tp>
167 void
168 __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
169 {
170     delete static_cast<pointer>(__p);
171 }
172
173 template <class _Tp>
174 __thread_specific_ptr<_Tp>::__thread_specific_ptr()
175 {
176     int __ec = __libcpp_tls_create(
177         &__key_,
178         &__thread_specific_ptr::__at_thread_exit);
179     if (__ec)
180         __throw_system_error(__ec,
181                            "__thread_specific_ptr construction failed");
182 }
183
184 template <class _Tp>
185 __thread_specific_ptr<_Tp>::~__thread_specific_ptr()
186 {
187     // __thread_specific_ptr is only created with a static storage duration
188     // so this destructor is only invoked during program termination. Invoking
189     // pthread_key_delete(__key_) may prevent other threads from deleting their
190     // thread local data. For this reason we leak the key.
191 }
192
193 template <class _Tp>
194 void
195 __thread_specific_ptr<_Tp>::set_pointer(pointer __p)
196 {
197     _LIBCPP_ASSERT(get() == nullptr,
198                    "Attempting to overwrite thread local data");
199     __libcpp_tls_set(__key_, __p);
200 }
201
202 class _LIBCPP_TYPE_VIS thread;
203 class _LIBCPP_TYPE_VIS __thread_id;
204
205 namespace this_thread
206 {
207
208 _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
209
210 }  // this_thread
211
212 template<> struct hash<__thread_id>;
213
214 class _LIBCPP_TYPE_VIS_ONLY __thread_id
215 {
216     // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
217     // NULL is the no-thread value on Darwin.  Someone needs to check
218     // on other platforms.  We assume 0 works everywhere for now.
219     __libcpp_thread_id __id_;
220
221 public:
222     _LIBCPP_INLINE_VISIBILITY
223     __thread_id() _NOEXCEPT : __id_(0) {}
224
225     friend _LIBCPP_INLINE_VISIBILITY
226         bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
227         {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
228     friend _LIBCPP_INLINE_VISIBILITY
229         bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
230         {return !(__x == __y);}
231     friend _LIBCPP_INLINE_VISIBILITY
232         bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
233         {return  __libcpp_thread_id_less(__x.__id_, __y.__id_);}
234     friend _LIBCPP_INLINE_VISIBILITY
235         bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
236         {return !(__y < __x);}
237     friend _LIBCPP_INLINE_VISIBILITY
238         bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
239         {return   __y < __x ;}
240     friend _LIBCPP_INLINE_VISIBILITY
241         bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
242         {return !(__x < __y);}
243
244     template<class _CharT, class _Traits>
245     friend
246     _LIBCPP_INLINE_VISIBILITY
247     basic_ostream<_CharT, _Traits>&
248     operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
249         {return __os << __id.__id_;}
250
251 private:
252     _LIBCPP_INLINE_VISIBILITY
253     __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
254
255     friend __thread_id this_thread::get_id() _NOEXCEPT;
256     friend class _LIBCPP_TYPE_VIS thread;
257     friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
258 };
259
260 template<>
261 struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>
262     : public unary_function<__thread_id, size_t>
263 {
264     _LIBCPP_INLINE_VISIBILITY
265     size_t operator()(__thread_id __v) const
266     {
267         return hash<__libcpp_thread_id>()(__v.__id_);
268     }
269 };
270
271 namespace this_thread
272 {
273
274 inline _LIBCPP_INLINE_VISIBILITY
275 __thread_id
276 get_id() _NOEXCEPT
277 {
278     return __libcpp_thread_get_current_id();
279 }
280
281 }  // this_thread
282
283 class _LIBCPP_TYPE_VIS thread
284 {
285     __libcpp_thread_t __t_;
286
287     thread(const thread&);
288     thread& operator=(const thread&);
289 public:
290     typedef __thread_id id;
291     typedef __libcpp_thread_t native_handle_type;
292
293     _LIBCPP_INLINE_VISIBILITY
294     thread() _NOEXCEPT : __t_(0) {}
295 #ifndef _LIBCPP_HAS_NO_VARIADICS
296     template <class _Fp, class ..._Args,
297               class = typename enable_if
298               <
299                    !is_same<typename decay<_Fp>::type, thread>::value
300               >::type
301              >
302         explicit thread(_Fp&& __f, _Args&&... __args);
303 #else  // _LIBCPP_HAS_NO_VARIADICS
304     template <class _Fp> explicit thread(_Fp __f);
305 #endif
306     ~thread();
307
308 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
309     _LIBCPP_INLINE_VISIBILITY
310     thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
311     _LIBCPP_INLINE_VISIBILITY
312     thread& operator=(thread&& __t) _NOEXCEPT;
313 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
314
315     _LIBCPP_INLINE_VISIBILITY
316     void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
317
318     _LIBCPP_INLINE_VISIBILITY
319     bool joinable() const _NOEXCEPT {return __t_ != 0;}
320     void join();
321     void detach();
322     _LIBCPP_INLINE_VISIBILITY
323     id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
324     _LIBCPP_INLINE_VISIBILITY
325     native_handle_type native_handle() _NOEXCEPT {return __t_;}
326
327     static unsigned hardware_concurrency() _NOEXCEPT;
328 };
329
330 #ifndef _LIBCPP_HAS_NO_VARIADICS
331
332 template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
333 inline _LIBCPP_INLINE_VISIBILITY
334 void
335 __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
336 {
337     __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
338 }
339
340 template <class _Fp>
341 void* __thread_proxy(void* __vp)
342 {
343     // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
344     std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
345     __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
346     typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
347     __thread_execute(*__p, _Index());
348     return nullptr;
349 }
350
351 template <class _Fp, class ..._Args,
352           class
353          >
354 thread::thread(_Fp&& __f, _Args&&... __args)
355 {
356     typedef unique_ptr<__thread_struct> _TSPtr;
357     _TSPtr __tsp(new __thread_struct);
358     typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
359     _VSTD::unique_ptr<_Gp> __p(
360             new _Gp(std::move(__tsp),
361                     __decay_copy(_VSTD::forward<_Fp>(__f)),
362                     __decay_copy(_VSTD::forward<_Args>(__args))...));
363     int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
364     if (__ec == 0)
365         __p.release();
366     else
367         __throw_system_error(__ec, "thread constructor failed");
368 }
369
370 #else  // _LIBCPP_HAS_NO_VARIADICS
371
372 template <class _Fp>
373 struct __thread_invoke_pair {
374     // This type is used to pass memory for thread local storage and a functor
375     // to a newly created thread because std::pair doesn't work with
376     // std::unique_ptr in C++03.
377     __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
378     unique_ptr<__thread_struct> __tsp_;
379     _Fp __fn_;
380 };
381
382 template <class _Fp>
383 void* __thread_proxy_cxx03(void* __vp)
384 {
385     std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
386     __thread_local_data().set_pointer(__p->__tsp_.release());
387     (__p->__fn_)();
388     return nullptr;
389 }
390
391 template <class _Fp>
392 thread::thread(_Fp __f)
393 {
394
395     typedef __thread_invoke_pair<_Fp> _InvokePair;
396     typedef std::unique_ptr<_InvokePair> _PairPtr;
397     _PairPtr __pp(new _InvokePair(__f));
398     int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
399     if (__ec == 0)
400         __pp.release();
401     else
402         __throw_system_error(__ec, "thread constructor failed");
403 }
404
405 #endif  // _LIBCPP_HAS_NO_VARIADICS
406
407 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
408
409 inline
410 thread&
411 thread::operator=(thread&& __t) _NOEXCEPT
412 {
413     if (__t_ != 0)
414         terminate();
415     __t_ = __t.__t_;
416     __t.__t_ = 0;
417     return *this;
418 }
419
420 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
421
422 inline _LIBCPP_INLINE_VISIBILITY
423 void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
424
425 namespace this_thread
426 {
427
428 _LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
429
430 template <class _Rep, class _Period>
431 void
432 sleep_for(const chrono::duration<_Rep, _Period>& __d)
433 {
434     using namespace chrono;
435     if (__d > duration<_Rep, _Period>::zero())
436     {
437         _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
438         nanoseconds __ns;
439         if (__d < _Max)
440         {
441             __ns = duration_cast<nanoseconds>(__d);
442             if (__ns < __d)
443                 ++__ns;
444         }
445         else
446             __ns = nanoseconds::max();
447         sleep_for(__ns);
448     }
449 }
450
451 template <class _Clock, class _Duration>
452 void
453 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
454 {
455     using namespace chrono;
456     mutex __mut;
457     condition_variable __cv;
458     unique_lock<mutex> __lk(__mut);
459     while (_Clock::now() < __t)
460         __cv.wait_until(__lk, __t);
461 }
462
463 template <class _Duration>
464 inline _LIBCPP_INLINE_VISIBILITY
465 void
466 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
467 {
468     using namespace chrono;
469     sleep_for(__t - steady_clock::now());
470 }
471
472 inline _LIBCPP_INLINE_VISIBILITY
473 void yield() _NOEXCEPT {__libcpp_thread_yield();}
474
475 }  // this_thread
476
477 _LIBCPP_END_NAMESPACE_STD
478
479 #endif // !_LIBCPP_HAS_NO_THREADS
480
481 #endif  // _LIBCPP_THREAD