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