xref: /llvm-project-15.0.7/libcxx/src/thread.cpp (revision de2ddc8f)
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     static __thread_specific_ptr<__thread_struct> __p;
119     return __p;
120 }
121 
122 // __thread_struct_imp
123 
124 template <class T>
125 class _LIBCPP_HIDDEN __hidden_allocator
126 {
127 public:
128     typedef T  value_type;
129 
130     T* allocate(size_t __n)
131         {return static_cast<T*>(::operator new(__n * sizeof(T)));}
132     void deallocate(T* __p, size_t) {::operator delete(static_cast<void*>(__p));}
133 
134     size_t max_size() const {return size_t(~0) / sizeof(T);}
135 };
136 
137 class _LIBCPP_HIDDEN __thread_struct_imp
138 {
139     typedef vector<__assoc_sub_state*,
140                           __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
141     typedef vector<pair<condition_variable*, mutex*>,
142                __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
143 
144     _AsyncStates async_states_;
145     _Notify notify_;
146 
147     __thread_struct_imp(const __thread_struct_imp&);
148     __thread_struct_imp& operator=(const __thread_struct_imp&);
149 public:
150     __thread_struct_imp() {}
151     ~__thread_struct_imp();
152 
153     void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
154     void __make_ready_at_thread_exit(__assoc_sub_state* __s);
155 };
156 
157 __thread_struct_imp::~__thread_struct_imp()
158 {
159     for (_Notify::iterator i = notify_.begin(), e = notify_.end();
160             i != e; ++i)
161     {
162         i->second->unlock();
163         i->first->notify_all();
164     }
165     for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
166             i != e; ++i)
167     {
168         (*i)->__make_ready();
169         (*i)->__release_shared();
170     }
171 }
172 
173 void
174 __thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
175 {
176     notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
177 }
178 
179 void
180 __thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
181 {
182     async_states_.push_back(__s);
183     __s->__add_shared();
184 }
185 
186 // __thread_struct
187 
188 __thread_struct::__thread_struct()
189     : __p_(new __thread_struct_imp)
190 {
191 }
192 
193 __thread_struct::~__thread_struct()
194 {
195     delete __p_;
196 }
197 
198 void
199 __thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
200 {
201     __p_->notify_all_at_thread_exit(cv, m);
202 }
203 
204 void
205 __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
206 {
207     __p_->__make_ready_at_thread_exit(__s);
208 }
209 
210 _LIBCPP_END_NAMESPACE_STD
211 
212 #endif // !_LIBCPP_HAS_NO_THREADS
213