xref: /llvm-project-15.0.7/libcxx/include/thread (revision 307beb13)
1// -*- C++ -*-
2//===--------------------------- thread -----------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_THREAD
11#define _LIBCPP_THREAD
12
13/*
14
15    thread synopsis
16
17namespace std
18{
19
20class thread
21{
22public:
23    class id;
24    typedef pthread_t native_handle_type;
25
26    thread() noexcept;
27    template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
28    ~thread();
29
30    thread(const thread&) = delete;
31    thread(thread&& t) noexcept;
32
33    thread& operator=(const thread&) = delete;
34    thread& operator=(thread&& t) noexcept;
35
36    void swap(thread& t) noexcept;
37
38    bool joinable() const noexcept;
39    void join();
40    void detach();
41    id get_id() const noexcept;
42    native_handle_type native_handle();
43
44    static unsigned hardware_concurrency() noexcept;
45};
46
47void swap(thread& x, thread& y) noexcept;
48
49class thread::id
50{
51public:
52    id() noexcept;
53};
54
55bool operator==(thread::id x, thread::id y) noexcept;
56bool operator!=(thread::id x, thread::id y) noexcept;
57bool operator< (thread::id x, thread::id y) noexcept;
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;
61
62template<class charT, class traits>
63basic_ostream<charT, traits>&
64operator<<(basic_ostream<charT, traits>& out, thread::id id);
65
66namespace this_thread
67{
68
69thread::id get_id() noexcept;
70
71void yield() noexcept;
72
73template <class Clock, class Duration>
74void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
75
76template <class Rep, class Period>
77void sleep_for(const chrono::duration<Rep, Period>& rel_time);
78
79}  // this_thread
80
81}  // std
82
83*/
84
85#include <__config>
86#include <iosfwd>
87#include <__functional_base>
88#include <type_traits>
89#include <cstddef>
90#include <functional>
91#include <memory>
92#include <system_error>
93#include <chrono>
94#include <__mutex_base>
95#ifndef _LIBCPP_CXX03_LANG
96#include <tuple>
97#endif
98#include <__threading_support>
99#include <__debug>
100
101#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
102#pragma GCC system_header
103#endif
104
105_LIBCPP_PUSH_MACROS
106#include <__undef_macros>
107
108#ifdef _LIBCPP_HAS_NO_THREADS
109#error <thread> is not supported on this single threaded system
110#else // !_LIBCPP_HAS_NO_THREADS
111
112_LIBCPP_BEGIN_NAMESPACE_STD
113
114template <class _Tp> class __thread_specific_ptr;
115class _LIBCPP_TYPE_VIS __thread_struct;
116class _LIBCPP_HIDDEN __thread_struct_imp;
117class __assoc_sub_state;
118
119_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
120
121class _LIBCPP_TYPE_VIS __thread_struct
122{
123    __thread_struct_imp* __p_;
124
125    __thread_struct(const __thread_struct&);
126    __thread_struct& operator=(const __thread_struct&);
127public:
128    __thread_struct();
129    ~__thread_struct();
130
131    void notify_all_at_thread_exit(condition_variable*, mutex*);
132    void __make_ready_at_thread_exit(__assoc_sub_state*);
133};
134
135template <class _Tp>
136class __thread_specific_ptr
137{
138    __libcpp_tls_key __key_;
139
140     // Only __thread_local_data() may construct a __thread_specific_ptr
141     // and only with _Tp == __thread_struct.
142    static_assert((is_same<_Tp, __thread_struct>::value), "");
143    __thread_specific_ptr();
144    friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
145
146    __thread_specific_ptr(const __thread_specific_ptr&);
147    __thread_specific_ptr& operator=(const __thread_specific_ptr&);
148
149    _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
150
151public:
152    typedef _Tp* pointer;
153
154    ~__thread_specific_ptr();
155
156    _LIBCPP_INLINE_VISIBILITY
157    pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
158    _LIBCPP_INLINE_VISIBILITY
159    pointer operator*() const {return *get();}
160    _LIBCPP_INLINE_VISIBILITY
161    pointer operator->() const {return get();}
162    void set_pointer(pointer __p);
163};
164
165template <class _Tp>
166void _LIBCPP_TLS_DESTRUCTOR_CC
167__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
168{
169    delete static_cast<pointer>(__p);
170}
171
172template <class _Tp>
173__thread_specific_ptr<_Tp>::__thread_specific_ptr()
174{
175  int __ec =
176      __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
177  if (__ec)
178    __throw_system_error(__ec, "__thread_specific_ptr construction failed");
179}
180
181template <class _Tp>
182__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
183{
184    // __thread_specific_ptr is only created with a static storage duration
185    // so this destructor is only invoked during program termination. Invoking
186    // pthread_key_delete(__key_) may prevent other threads from deleting their
187    // thread local data. For this reason we leak the key.
188}
189
190template <class _Tp>
191void
192__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
193{
194    _LIBCPP_ASSERT(get() == nullptr,
195                   "Attempting to overwrite thread local data");
196    __libcpp_tls_set(__key_, __p);
197}
198
199class _LIBCPP_TYPE_VIS thread;
200class _LIBCPP_TYPE_VIS __thread_id;
201
202namespace this_thread
203{
204
205_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
206
207}  // this_thread
208
209template<> struct hash<__thread_id>;
210
211class _LIBCPP_TEMPLATE_VIS __thread_id
212{
213    // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
214    // NULL is the no-thread value on Darwin.  Someone needs to check
215    // on other platforms.  We assume 0 works everywhere for now.
216    __libcpp_thread_id __id_;
217
218public:
219    _LIBCPP_INLINE_VISIBILITY
220    __thread_id() _NOEXCEPT : __id_(0) {}
221
222    friend _LIBCPP_INLINE_VISIBILITY
223        bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
224        {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
225    friend _LIBCPP_INLINE_VISIBILITY
226        bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
227        {return !(__x == __y);}
228    friend _LIBCPP_INLINE_VISIBILITY
229        bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
230        {return  __libcpp_thread_id_less(__x.__id_, __y.__id_);}
231    friend _LIBCPP_INLINE_VISIBILITY
232        bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
233        {return !(__y < __x);}
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 !(__x < __y);}
240
241    template<class _CharT, class _Traits>
242    friend
243    _LIBCPP_INLINE_VISIBILITY
244    basic_ostream<_CharT, _Traits>&
245    operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
246        {return __os << __id.__id_;}
247
248private:
249    _LIBCPP_INLINE_VISIBILITY
250    __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
251
252    friend __thread_id this_thread::get_id() _NOEXCEPT;
253    friend class _LIBCPP_TYPE_VIS thread;
254    friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
255};
256
257template<>
258struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
259    : public unary_function<__thread_id, size_t>
260{
261    _LIBCPP_INLINE_VISIBILITY
262    size_t operator()(__thread_id __v) const _NOEXCEPT
263    {
264        return hash<__libcpp_thread_id>()(__v.__id_);
265    }
266};
267
268namespace this_thread
269{
270
271inline _LIBCPP_INLINE_VISIBILITY
272__thread_id
273get_id() _NOEXCEPT
274{
275    return __libcpp_thread_get_current_id();
276}
277
278}  // this_thread
279
280class _LIBCPP_TYPE_VIS thread
281{
282    __libcpp_thread_t __t_;
283
284    thread(const thread&);
285    thread& operator=(const thread&);
286public:
287    typedef __thread_id id;
288    typedef __libcpp_thread_t native_handle_type;
289
290    _LIBCPP_INLINE_VISIBILITY
291    thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
292#ifndef _LIBCPP_CXX03_LANG
293    template <class _Fp, class ..._Args,
294              class = typename enable_if
295              <
296                   !is_same<typename __uncvref<_Fp>::type, thread>::value
297              >::type
298             >
299        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
300        explicit thread(_Fp&& __f, _Args&&... __args);
301#else  // _LIBCPP_CXX03_LANG
302    template <class _Fp>
303    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
304    explicit thread(_Fp __f);
305#endif
306    ~thread();
307
308#ifndef _LIBCPP_CXX03_LANG
309    _LIBCPP_INLINE_VISIBILITY
310    thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = _LIBCPP_NULL_THREAD;}
311    _LIBCPP_INLINE_VISIBILITY
312    thread& operator=(thread&& __t) _NOEXCEPT;
313#endif  // _LIBCPP_CXX03_LANG
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 !__libcpp_thread_isnull(&__t_);}
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_CXX03_LANG
331
332template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
333inline _LIBCPP_INLINE_VISIBILITY
334void
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
340template <class _Fp>
341void* __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
351template <class _Fp, class ..._Args,
352          class
353         >
354thread::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
370inline
371thread&
372thread::operator=(thread&& __t) _NOEXCEPT
373{
374    if (!__libcpp_thread_isnull(&__t_))
375        terminate();
376    __t_ = __t.__t_;
377    __t.__t_ = _LIBCPP_NULL_THREAD;
378    return *this;
379}
380
381#else  // _LIBCPP_CXX03_LANG
382
383template <class _Fp>
384struct __thread_invoke_pair {
385    // This type is used to pass memory for thread local storage and a functor
386    // to a newly created thread because std::pair doesn't work with
387    // std::unique_ptr in C++03.
388    __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
389    unique_ptr<__thread_struct> __tsp_;
390    _Fp __fn_;
391};
392
393template <class _Fp>
394void* __thread_proxy_cxx03(void* __vp)
395{
396    std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
397    __thread_local_data().set_pointer(__p->__tsp_.release());
398    (__p->__fn_)();
399    return nullptr;
400}
401
402template <class _Fp>
403thread::thread(_Fp __f)
404{
405
406    typedef __thread_invoke_pair<_Fp> _InvokePair;
407    typedef std::unique_ptr<_InvokePair> _PairPtr;
408    _PairPtr __pp(new _InvokePair(__f));
409    int __ec = __libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
410    if (__ec == 0)
411        __pp.release();
412    else
413        __throw_system_error(__ec, "thread constructor failed");
414}
415
416#endif  // _LIBCPP_CXX03_LANG
417
418inline _LIBCPP_INLINE_VISIBILITY
419void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
420
421namespace this_thread
422{
423
424_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
425
426template <class _Rep, class _Period>
427void
428sleep_for(const chrono::duration<_Rep, _Period>& __d)
429{
430    using namespace chrono;
431    if (__d > duration<_Rep, _Period>::zero())
432    {
433#if defined(_LIBCPP_COMPILER_GCC) && (__powerpc__ || __POWERPC__)
434    //  GCC's long double const folding is incomplete for IBM128 long doubles.
435        _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
436#else
437        _LIBCPP_CONSTEXPR duration<long double> _Max = duration<long double>(ULLONG_MAX/1000000000ULL) ;
438#endif
439        nanoseconds __ns;
440        if (__d < _Max)
441        {
442            __ns = duration_cast<nanoseconds>(__d);
443            if (__ns < __d)
444                ++__ns;
445        }
446        else
447            __ns = nanoseconds::max();
448        sleep_for(__ns);
449    }
450}
451
452template <class _Clock, class _Duration>
453void
454sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
455{
456    using namespace chrono;
457    mutex __mut;
458    condition_variable __cv;
459    unique_lock<mutex> __lk(__mut);
460    while (_Clock::now() < __t)
461        __cv.wait_until(__lk, __t);
462}
463
464template <class _Duration>
465inline _LIBCPP_INLINE_VISIBILITY
466void
467sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
468{
469    using namespace chrono;
470    sleep_for(__t - steady_clock::now());
471}
472
473inline _LIBCPP_INLINE_VISIBILITY
474void yield() _NOEXCEPT {__libcpp_thread_yield();}
475
476}  // this_thread
477
478_LIBCPP_END_NAMESPACE_STD
479
480#endif // !_LIBCPP_HAS_NO_THREADS
481
482_LIBCPP_POP_MACROS
483
484#endif  // _LIBCPP_THREAD
485