1*c303c47eSjoerg //===------------------------- thread.cpp----------------------------------===//
2*c303c47eSjoerg //
3*c303c47eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*c303c47eSjoerg // See https://llvm.org/LICENSE.txt for license information.
5*c303c47eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*c303c47eSjoerg //
7*c303c47eSjoerg //===----------------------------------------------------------------------===//
8*c303c47eSjoerg 
9*c303c47eSjoerg #include "__config"
10*c303c47eSjoerg #ifndef _LIBCPP_HAS_NO_THREADS
11*c303c47eSjoerg 
12*c303c47eSjoerg #include "thread"
13*c303c47eSjoerg #include "exception"
14*c303c47eSjoerg #include "vector"
15*c303c47eSjoerg #include "future"
16*c303c47eSjoerg #include "limits"
17*c303c47eSjoerg 
18*c303c47eSjoerg #if __has_include(<unistd.h>)
19*c303c47eSjoerg # include <unistd.h> // for sysconf
20*c303c47eSjoerg #endif
21*c303c47eSjoerg 
22*c303c47eSjoerg #if defined(__NetBSD__)
23*c303c47eSjoerg #pragma weak pthread_create // Do not create libpthread dependency
24*c303c47eSjoerg #endif
25*c303c47eSjoerg 
26*c303c47eSjoerg #if defined(_LIBCPP_WIN32API)
27*c303c47eSjoerg #include <windows.h>
28*c303c47eSjoerg #endif
29*c303c47eSjoerg 
30*c303c47eSjoerg #if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
31*c303c47eSjoerg #pragma comment(lib, "pthread")
32*c303c47eSjoerg #endif
33*c303c47eSjoerg 
34*c303c47eSjoerg _LIBCPP_BEGIN_NAMESPACE_STD
35*c303c47eSjoerg 
~thread()36*c303c47eSjoerg thread::~thread()
37*c303c47eSjoerg {
38*c303c47eSjoerg     if (!__libcpp_thread_isnull(&__t_))
39*c303c47eSjoerg         terminate();
40*c303c47eSjoerg }
41*c303c47eSjoerg 
42*c303c47eSjoerg void
join()43*c303c47eSjoerg thread::join()
44*c303c47eSjoerg {
45*c303c47eSjoerg     int ec = EINVAL;
46*c303c47eSjoerg     if (!__libcpp_thread_isnull(&__t_))
47*c303c47eSjoerg     {
48*c303c47eSjoerg         ec = __libcpp_thread_join(&__t_);
49*c303c47eSjoerg         if (ec == 0)
50*c303c47eSjoerg             __t_ = _LIBCPP_NULL_THREAD;
51*c303c47eSjoerg     }
52*c303c47eSjoerg 
53*c303c47eSjoerg     if (ec)
54*c303c47eSjoerg         __throw_system_error(ec, "thread::join failed");
55*c303c47eSjoerg }
56*c303c47eSjoerg 
57*c303c47eSjoerg void
detach()58*c303c47eSjoerg thread::detach()
59*c303c47eSjoerg {
60*c303c47eSjoerg     int ec = EINVAL;
61*c303c47eSjoerg     if (!__libcpp_thread_isnull(&__t_))
62*c303c47eSjoerg     {
63*c303c47eSjoerg         ec = __libcpp_thread_detach(&__t_);
64*c303c47eSjoerg         if (ec == 0)
65*c303c47eSjoerg             __t_ = _LIBCPP_NULL_THREAD;
66*c303c47eSjoerg     }
67*c303c47eSjoerg 
68*c303c47eSjoerg     if (ec)
69*c303c47eSjoerg         __throw_system_error(ec, "thread::detach failed");
70*c303c47eSjoerg }
71*c303c47eSjoerg 
72*c303c47eSjoerg unsigned
hardware_concurrency()73*c303c47eSjoerg thread::hardware_concurrency() noexcept
74*c303c47eSjoerg {
75*c303c47eSjoerg #if defined(_SC_NPROCESSORS_ONLN)
76*c303c47eSjoerg     long result = sysconf(_SC_NPROCESSORS_ONLN);
77*c303c47eSjoerg     // sysconf returns -1 if the name is invalid, the option does not exist or
78*c303c47eSjoerg     // does not have a definite limit.
79*c303c47eSjoerg     // if sysconf returns some other negative number, we have no idea
80*c303c47eSjoerg     // what is going on. Default to something safe.
81*c303c47eSjoerg     if (result < 0)
82*c303c47eSjoerg         return 0;
83*c303c47eSjoerg     return static_cast<unsigned>(result);
84*c303c47eSjoerg #elif defined(_LIBCPP_WIN32API)
85*c303c47eSjoerg     SYSTEM_INFO info;
86*c303c47eSjoerg     GetSystemInfo(&info);
87*c303c47eSjoerg     return info.dwNumberOfProcessors;
88*c303c47eSjoerg #else  // defined(CTL_HW) && defined(HW_NCPU)
89*c303c47eSjoerg     // TODO: grovel through /proc or check cpuid on x86 and similar
90*c303c47eSjoerg     // instructions on other architectures.
91*c303c47eSjoerg #   if defined(_LIBCPP_WARNING)
92*c303c47eSjoerg         _LIBCPP_WARNING("hardware_concurrency not yet implemented")
93*c303c47eSjoerg #   else
94*c303c47eSjoerg #       warning hardware_concurrency not yet implemented
95*c303c47eSjoerg #   endif
96*c303c47eSjoerg     return 0;  // Means not computable [thread.thread.static]
97*c303c47eSjoerg #endif // defined(CTL_HW) && defined(HW_NCPU)
98*c303c47eSjoerg }
99*c303c47eSjoerg 
100*c303c47eSjoerg namespace this_thread
101*c303c47eSjoerg {
102*c303c47eSjoerg 
103*c303c47eSjoerg void
sleep_for(const chrono::nanoseconds & ns)104*c303c47eSjoerg sleep_for(const chrono::nanoseconds& ns)
105*c303c47eSjoerg {
106*c303c47eSjoerg     if (ns > chrono::nanoseconds::zero())
107*c303c47eSjoerg     {
108*c303c47eSjoerg         __libcpp_thread_sleep_for(ns);
109*c303c47eSjoerg     }
110*c303c47eSjoerg }
111*c303c47eSjoerg 
112*c303c47eSjoerg }  // this_thread
113*c303c47eSjoerg 
114*c303c47eSjoerg __thread_specific_ptr<__thread_struct>&
__thread_local_data()115*c303c47eSjoerg __thread_local_data()
116*c303c47eSjoerg {
117*c303c47eSjoerg     static __thread_specific_ptr<__thread_struct> __p;
118*c303c47eSjoerg     return __p;
119*c303c47eSjoerg }
120*c303c47eSjoerg 
121*c303c47eSjoerg // __thread_struct_imp
122*c303c47eSjoerg 
123*c303c47eSjoerg template <class T>
124*c303c47eSjoerg class _LIBCPP_HIDDEN __hidden_allocator
125*c303c47eSjoerg {
126*c303c47eSjoerg public:
127*c303c47eSjoerg     typedef T  value_type;
128*c303c47eSjoerg 
allocate(size_t __n)129*c303c47eSjoerg     T* allocate(size_t __n)
130*c303c47eSjoerg         {return static_cast<T*>(::operator new(__n * sizeof(T)));}
deallocate(T * __p,size_t)131*c303c47eSjoerg     void deallocate(T* __p, size_t) {::operator delete(static_cast<void*>(__p));}
132*c303c47eSjoerg 
max_size() const133*c303c47eSjoerg     size_t max_size() const {return size_t(~0) / sizeof(T);}
134*c303c47eSjoerg };
135*c303c47eSjoerg 
136*c303c47eSjoerg class _LIBCPP_HIDDEN __thread_struct_imp
137*c303c47eSjoerg {
138*c303c47eSjoerg     typedef vector<__assoc_sub_state*,
139*c303c47eSjoerg                           __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
140*c303c47eSjoerg     typedef vector<pair<condition_variable*, mutex*>,
141*c303c47eSjoerg                __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
142*c303c47eSjoerg 
143*c303c47eSjoerg     _AsyncStates async_states_;
144*c303c47eSjoerg     _Notify notify_;
145*c303c47eSjoerg 
146*c303c47eSjoerg     __thread_struct_imp(const __thread_struct_imp&);
147*c303c47eSjoerg     __thread_struct_imp& operator=(const __thread_struct_imp&);
148*c303c47eSjoerg public:
__thread_struct_imp()149*c303c47eSjoerg     __thread_struct_imp() {}
150*c303c47eSjoerg     ~__thread_struct_imp();
151*c303c47eSjoerg 
152*c303c47eSjoerg     void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
153*c303c47eSjoerg     void __make_ready_at_thread_exit(__assoc_sub_state* __s);
154*c303c47eSjoerg };
155*c303c47eSjoerg 
~__thread_struct_imp()156*c303c47eSjoerg __thread_struct_imp::~__thread_struct_imp()
157*c303c47eSjoerg {
158*c303c47eSjoerg     for (_Notify::iterator i = notify_.begin(), e = notify_.end();
159*c303c47eSjoerg             i != e; ++i)
160*c303c47eSjoerg     {
161*c303c47eSjoerg         i->second->unlock();
162*c303c47eSjoerg         i->first->notify_all();
163*c303c47eSjoerg     }
164*c303c47eSjoerg     for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
165*c303c47eSjoerg             i != e; ++i)
166*c303c47eSjoerg     {
167*c303c47eSjoerg         (*i)->__make_ready();
168*c303c47eSjoerg         (*i)->__release_shared();
169*c303c47eSjoerg     }
170*c303c47eSjoerg }
171*c303c47eSjoerg 
172*c303c47eSjoerg void
notify_all_at_thread_exit(condition_variable * cv,mutex * m)173*c303c47eSjoerg __thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
174*c303c47eSjoerg {
175*c303c47eSjoerg     notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
176*c303c47eSjoerg }
177*c303c47eSjoerg 
178*c303c47eSjoerg void
__make_ready_at_thread_exit(__assoc_sub_state * __s)179*c303c47eSjoerg __thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
180*c303c47eSjoerg {
181*c303c47eSjoerg     async_states_.push_back(__s);
182*c303c47eSjoerg     __s->__add_shared();
183*c303c47eSjoerg }
184*c303c47eSjoerg 
185*c303c47eSjoerg // __thread_struct
186*c303c47eSjoerg 
__thread_struct()187*c303c47eSjoerg __thread_struct::__thread_struct()
188*c303c47eSjoerg     : __p_(new __thread_struct_imp)
189*c303c47eSjoerg {
190*c303c47eSjoerg }
191*c303c47eSjoerg 
~__thread_struct()192*c303c47eSjoerg __thread_struct::~__thread_struct()
193*c303c47eSjoerg {
194*c303c47eSjoerg     delete __p_;
195*c303c47eSjoerg }
196*c303c47eSjoerg 
197*c303c47eSjoerg void
notify_all_at_thread_exit(condition_variable * cv,mutex * m)198*c303c47eSjoerg __thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
199*c303c47eSjoerg {
200*c303c47eSjoerg     __p_->notify_all_at_thread_exit(cv, m);
201*c303c47eSjoerg }
202*c303c47eSjoerg 
203*c303c47eSjoerg void
__make_ready_at_thread_exit(__assoc_sub_state * __s)204*c303c47eSjoerg __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
205*c303c47eSjoerg {
206*c303c47eSjoerg     __p_->__make_ready_at_thread_exit(__s);
207*c303c47eSjoerg }
208*c303c47eSjoerg 
209*c303c47eSjoerg _LIBCPP_END_NAMESPACE_STD
210*c303c47eSjoerg 
211*c303c47eSjoerg #endif // !_LIBCPP_HAS_NO_THREADS
212