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