xref: /freebsd-12.1/contrib/libc++/include/thread (revision cc65eb4e)
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
20namespace std
21{
22
23class thread
24{
25public:
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
50void swap(thread& x, thread& y) noexcept;
51
52class thread::id
53{
54public:
55    id() noexcept;
56};
57
58bool operator==(thread::id x, thread::id y) noexcept;
59bool operator!=(thread::id x, thread::id y) noexcept;
60bool operator< (thread::id x, thread::id y) noexcept;
61bool operator<=(thread::id x, thread::id y) noexcept;
62bool operator> (thread::id x, thread::id y) noexcept;
63bool operator>=(thread::id x, thread::id y) noexcept;
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() noexcept;
73
74void yield() noexcept;
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#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
116template <class _Tp> class __thread_specific_ptr;
117class _LIBCPP_TYPE_VIS __thread_struct;
118class _LIBCPP_HIDDEN __thread_struct_imp;
119class __assoc_sub_state;
120
121_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
122
123class _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&);
129public:
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
137template <class _Tp>
138class __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 _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
152
153public:
154    typedef _Tp* pointer;
155
156    ~__thread_specific_ptr();
157
158    _LIBCPP_INLINE_VISIBILITY
159    pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
160    _LIBCPP_INLINE_VISIBILITY
161    pointer operator*() const {return *get();}
162    _LIBCPP_INLINE_VISIBILITY
163    pointer operator->() const {return get();}
164    void set_pointer(pointer __p);
165};
166
167template <class _Tp>
168void _LIBCPP_TLS_DESTRUCTOR_CC
169__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
170{
171    delete static_cast<pointer>(__p);
172}
173
174template <class _Tp>
175__thread_specific_ptr<_Tp>::__thread_specific_ptr()
176{
177  int __ec =
178      __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
179  if (__ec)
180    __throw_system_error(__ec, "__thread_specific_ptr construction failed");
181}
182
183template <class _Tp>
184__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
185{
186    // __thread_specific_ptr is only created with a static storage duration
187    // so this destructor is only invoked during program termination. Invoking
188    // pthread_key_delete(__key_) may prevent other threads from deleting their
189    // thread local data. For this reason we leak the key.
190}
191
192template <class _Tp>
193void
194__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
195{
196    _LIBCPP_ASSERT(get() == nullptr,
197                   "Attempting to overwrite thread local data");
198    __libcpp_tls_set(__key_, __p);
199}
200
201class _LIBCPP_TYPE_VIS thread;
202class _LIBCPP_TYPE_VIS __thread_id;
203
204namespace this_thread
205{
206
207_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
208
209}  // this_thread
210
211template<> struct hash<__thread_id>;
212
213class _LIBCPP_TEMPLATE_VIS __thread_id
214{
215    // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
216    // NULL is the no-thread value on Darwin.  Someone needs to check
217    // on other platforms.  We assume 0 works everywhere for now.
218    __libcpp_thread_id __id_;
219
220public:
221    _LIBCPP_INLINE_VISIBILITY
222    __thread_id() _NOEXCEPT : __id_(0) {}
223
224    friend _LIBCPP_INLINE_VISIBILITY
225        bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
226        {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
227    friend _LIBCPP_INLINE_VISIBILITY
228        bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
229        {return !(__x == __y);}
230    friend _LIBCPP_INLINE_VISIBILITY
231        bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
232        {return  __libcpp_thread_id_less(__x.__id_, __y.__id_);}
233    friend _LIBCPP_INLINE_VISIBILITY
234        bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
235        {return !(__y < __x);}
236    friend _LIBCPP_INLINE_VISIBILITY
237        bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
238        {return   __y < __x ;}
239    friend _LIBCPP_INLINE_VISIBILITY
240        bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
241        {return !(__x < __y);}
242
243    template<class _CharT, class _Traits>
244    friend
245    _LIBCPP_INLINE_VISIBILITY
246    basic_ostream<_CharT, _Traits>&
247    operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
248        {return __os << __id.__id_;}
249
250private:
251    _LIBCPP_INLINE_VISIBILITY
252    __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
253
254    friend __thread_id this_thread::get_id() _NOEXCEPT;
255    friend class _LIBCPP_TYPE_VIS thread;
256    friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
257};
258
259template<>
260struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
261    : public unary_function<__thread_id, size_t>
262{
263    _LIBCPP_INLINE_VISIBILITY
264    size_t operator()(__thread_id __v) const
265    {
266        return hash<__libcpp_thread_id>()(__v.__id_);
267    }
268};
269
270namespace this_thread
271{
272
273inline _LIBCPP_INLINE_VISIBILITY
274__thread_id
275get_id() _NOEXCEPT
276{
277    return __libcpp_thread_get_current_id();
278}
279
280}  // this_thread
281
282class _LIBCPP_TYPE_VIS thread
283{
284    __libcpp_thread_t __t_;
285
286    thread(const thread&);
287    thread& operator=(const thread&);
288public:
289    typedef __thread_id id;
290    typedef __libcpp_thread_t native_handle_type;
291
292    _LIBCPP_INLINE_VISIBILITY
293    thread() _NOEXCEPT : __t_(0) {}
294#ifndef _LIBCPP_HAS_NO_VARIADICS
295    template <class _Fp, class ..._Args,
296              class = typename enable_if
297              <
298                   !is_same<typename decay<_Fp>::type, thread>::value
299              >::type
300             >
301        explicit thread(_Fp&& __f, _Args&&... __args);
302#else  // _LIBCPP_HAS_NO_VARIADICS
303    template <class _Fp> explicit thread(_Fp __f);
304#endif
305    ~thread();
306
307#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
308    _LIBCPP_INLINE_VISIBILITY
309    thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
310    _LIBCPP_INLINE_VISIBILITY
311    thread& operator=(thread&& __t) _NOEXCEPT;
312#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
313
314    _LIBCPP_INLINE_VISIBILITY
315    void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
316
317    _LIBCPP_INLINE_VISIBILITY
318    bool joinable() const _NOEXCEPT {return __t_ != 0;}
319    void join();
320    void detach();
321    _LIBCPP_INLINE_VISIBILITY
322    id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
323    _LIBCPP_INLINE_VISIBILITY
324    native_handle_type native_handle() _NOEXCEPT {return __t_;}
325
326    static unsigned hardware_concurrency() _NOEXCEPT;
327};
328
329#ifndef _LIBCPP_HAS_NO_VARIADICS
330
331template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
332inline _LIBCPP_INLINE_VISIBILITY
333void
334__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
335{
336    __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
337}
338
339template <class _Fp>
340void* __thread_proxy(void* __vp)
341{
342    // _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
343    std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
344    __thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
345    typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
346    __thread_execute(*__p, _Index());
347    return nullptr;
348}
349
350template <class _Fp, class ..._Args,
351          class
352         >
353thread::thread(_Fp&& __f, _Args&&... __args)
354{
355    typedef unique_ptr<__thread_struct> _TSPtr;
356    _TSPtr __tsp(new __thread_struct);
357    typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
358    _VSTD::unique_ptr<_Gp> __p(
359            new _Gp(std::move(__tsp),
360                    __decay_copy(_VSTD::forward<_Fp>(__f)),
361                    __decay_copy(_VSTD::forward<_Args>(__args))...));
362    int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
363    if (__ec == 0)
364        __p.release();
365    else
366        __throw_system_error(__ec, "thread constructor failed");
367}
368
369#else  // _LIBCPP_HAS_NO_VARIADICS
370
371template <class _Fp>
372struct __thread_invoke_pair {
373    // This type is used to pass memory for thread local storage and a functor
374    // to a newly created thread because std::pair doesn't work with
375    // std::unique_ptr in C++03.
376    __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
377    unique_ptr<__thread_struct> __tsp_;
378    _Fp __fn_;
379};
380
381template <class _Fp>
382void* __thread_proxy_cxx03(void* __vp)
383{
384    std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
385    __thread_local_data().set_pointer(__p->__tsp_.release());
386    (__p->__fn_)();
387    return nullptr;
388}
389
390template <class _Fp>
391thread::thread(_Fp __f)
392{
393
394    typedef __thread_invoke_pair<_Fp> _InvokePair;
395    typedef std::unique_ptr<_InvokePair> _PairPtr;
396    _PairPtr __pp(new _InvokePair(__f));
397    int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
398    if (__ec == 0)
399        __pp.release();
400    else
401        __throw_system_error(__ec, "thread constructor failed");
402}
403
404#endif  // _LIBCPP_HAS_NO_VARIADICS
405
406#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
407
408inline
409thread&
410thread::operator=(thread&& __t) _NOEXCEPT
411{
412    if (__t_ != 0)
413        terminate();
414    __t_ = __t.__t_;
415    __t.__t_ = 0;
416    return *this;
417}
418
419#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
420
421inline _LIBCPP_INLINE_VISIBILITY
422void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
423
424namespace this_thread
425{
426
427_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
428
429template <class _Rep, class _Period>
430void
431sleep_for(const chrono::duration<_Rep, _Period>& __d)
432{
433    using namespace chrono;
434    if (__d > duration<_Rep, _Period>::zero())
435    {
436        _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
437        nanoseconds __ns;
438        if (__d < _Max)
439        {
440            __ns = duration_cast<nanoseconds>(__d);
441            if (__ns < __d)
442                ++__ns;
443        }
444        else
445            __ns = nanoseconds::max();
446        sleep_for(__ns);
447    }
448}
449
450template <class _Clock, class _Duration>
451void
452sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
453{
454    using namespace chrono;
455    mutex __mut;
456    condition_variable __cv;
457    unique_lock<mutex> __lk(__mut);
458    while (_Clock::now() < __t)
459        __cv.wait_until(__lk, __t);
460}
461
462template <class _Duration>
463inline _LIBCPP_INLINE_VISIBILITY
464void
465sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
466{
467    using namespace chrono;
468    sleep_for(__t - steady_clock::now());
469}
470
471inline _LIBCPP_INLINE_VISIBILITY
472void yield() _NOEXCEPT {__libcpp_thread_yield();}
473
474}  // this_thread
475
476_LIBCPP_END_NAMESPACE_STD
477
478#endif // !_LIBCPP_HAS_NO_THREADS
479
480#endif  // _LIBCPP_THREAD
481