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