xref: /llvm-project-15.0.7/libcxx/src/thread.cpp (revision ec87fb0b)
1 //===------------------------- thread.cpp----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "thread"
11 #include "exception"
12 #include "vector"
13 #include "future"
14 #include "limits"
15 #include <sys/types.h>
16 #if !defined(_WIN32)
17 #if !defined(__sun__) && !defined(__linux__)
18 #include <sys/sysctl.h>
19 #else
20 #include <unistd.h>
21 #endif // !__sun__ && !__linux__
22 #endif // !_WIN32
23 
24 #if defined(__NetBSD__)
25 #pragma weak pthread_create // Do not create libpthread dependency
26 #endif
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 thread::~thread()
31 {
32     if (__t_ != 0)
33         terminate();
34 }
35 
36 void
37 thread::join()
38 {
39     int ec = pthread_join(__t_, 0);
40 #ifndef _LIBCPP_NO_EXCEPTIONS
41     if (ec)
42         throw system_error(error_code(ec, system_category()), "thread::join failed");
43 #else
44     (void)ec;
45 #endif  // _LIBCPP_NO_EXCEPTIONS
46     __t_ = 0;
47 }
48 
49 void
50 thread::detach()
51 {
52     int ec = EINVAL;
53     if (__t_ != 0)
54     {
55         ec = pthread_detach(__t_);
56         if (ec == 0)
57             __t_ = 0;
58     }
59 #ifndef _LIBCPP_NO_EXCEPTIONS
60     if (ec)
61         throw system_error(error_code(ec, system_category()), "thread::detach failed");
62 #endif  // _LIBCPP_NO_EXCEPTIONS
63 }
64 
65 unsigned
66 thread::hardware_concurrency() _NOEXCEPT
67 {
68 #if defined(CTL_HW) && defined(HW_NCPU)
69     unsigned n;
70     int mib[2] = {CTL_HW, HW_NCPU};
71     std::size_t s = sizeof(n);
72     sysctl(mib, 2, &n, &s, 0, 0);
73     return n;
74 #elif (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN)) || defined(EMSCRIPTEN)
75     long result = sysconf(_SC_NPROCESSORS_ONLN);
76     // sysconf returns -1 if the name is invalid, the option does not exist or
77     // does not have a definite limit.
78     // if sysconf returns some other negative number, we have no idea
79     // what is going on. Default to something safe.
80     if (result < 0)
81         return 0;
82     return static_cast<unsigned>(result);
83 #else  // defined(CTL_HW) && defined(HW_NCPU)
84     // TODO: grovel through /proc or check cpuid on x86 and similar
85     // instructions on other architectures.
86     return 0;  // Means not computable [thread.thread.static]
87 #endif  // defined(CTL_HW) && defined(HW_NCPU)
88 }
89 
90 namespace this_thread
91 {
92 
93 void
94 sleep_for(const chrono::nanoseconds& ns)
95 {
96     using namespace chrono;
97     if (ns > nanoseconds::zero())
98     {
99         seconds s = duration_cast<seconds>(ns);
100         timespec ts;
101         typedef decltype(ts.tv_sec) ts_sec;
102         _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
103         if (s.count() < ts_sec_max)
104         {
105             ts.tv_sec = static_cast<ts_sec>(s.count());
106             ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns-s).count());
107         }
108         else
109         {
110             ts.tv_sec = ts_sec_max;
111             ts.tv_nsec = giga::num - 1;
112         }
113         nanosleep(&ts, 0);
114     }
115 }
116 
117 }  // this_thread
118 
119 __thread_specific_ptr<__thread_struct>&
120 __thread_local_data()
121 {
122     static __thread_specific_ptr<__thread_struct> __p;
123     return __p;
124 }
125 
126 // __thread_struct_imp
127 
128 template <class T>
129 class _LIBCPP_HIDDEN __hidden_allocator
130 {
131 public:
132     typedef T  value_type;
133 
134     T* allocate(size_t __n)
135         {return static_cast<T*>(::operator new(__n * sizeof(T)));}
136     void deallocate(T* __p, size_t) {::operator delete((void*)__p);}
137 
138     size_t max_size() const {return size_t(~0) / sizeof(T);}
139 };
140 
141 class _LIBCPP_HIDDEN __thread_struct_imp
142 {
143     typedef vector<__assoc_sub_state*,
144                           __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
145     typedef vector<pair<condition_variable*, mutex*>,
146                __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
147 
148     _AsyncStates async_states_;
149     _Notify notify_;
150 
151     __thread_struct_imp(const __thread_struct_imp&);
152     __thread_struct_imp& operator=(const __thread_struct_imp&);
153 public:
154     __thread_struct_imp() {}
155     ~__thread_struct_imp();
156 
157     void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
158     void __make_ready_at_thread_exit(__assoc_sub_state* __s);
159 };
160 
161 __thread_struct_imp::~__thread_struct_imp()
162 {
163     for (_Notify::iterator i = notify_.begin(), e = notify_.end();
164             i != e; ++i)
165     {
166         i->second->unlock();
167         i->first->notify_all();
168     }
169     for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
170             i != e; ++i)
171     {
172         (*i)->__make_ready();
173         (*i)->__release_shared();
174     }
175 }
176 
177 void
178 __thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
179 {
180     notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
181 }
182 
183 void
184 __thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
185 {
186     async_states_.push_back(__s);
187     __s->__add_shared();
188 }
189 
190 // __thread_struct
191 
192 __thread_struct::__thread_struct()
193     : __p_(new __thread_struct_imp)
194 {
195 }
196 
197 __thread_struct::~__thread_struct()
198 {
199     delete __p_;
200 }
201 
202 void
203 __thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
204 {
205     __p_->notify_all_at_thread_exit(cv, m);
206 }
207 
208 void
209 __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
210 {
211     __p_->__make_ready_at_thread_exit(__s);
212 }
213 
214 _LIBCPP_END_NAMESPACE_STD
215