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