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