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