blob: 4775e10f6ab5565ccbb940e7be0e55ee58889017 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===------------------------- thread.cpp----------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +000010#include "__config"
11#ifndef _LIBCPP_HAS_NO_THREADS
12
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000013#include "thread"
14#include "exception"
Howard Hinnant47499b12010-08-27 20:10:19 +000015#include "vector"
16#include "future"
Howard Hinnantcf115d22012-08-30 19:14:33 +000017#include "limits"
Howard Hinnant0bfe8802010-05-25 17:25:25 +000018#include <sys/types.h>
Ben Craig8057a582016-01-29 13:53:23 +000019
20#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
21# include <sys/param.h>
22# if defined(BSD)
JF Bastien93cfd7f2014-12-02 17:30:19 +000023# include <sys/sysctl.h>
Ben Craig8057a582016-01-29 13:53:23 +000024# endif // defined(BSD)
25#endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
26
27#if !defined(_WIN32)
JF Bastien93cfd7f2014-12-02 17:30:19 +000028# include <unistd.h>
Howard Hinnant403f91a2012-08-02 18:17:49 +000029#endif // !_WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030
Joerg Sonnenberger67444032013-05-17 21:16:18 +000031#if defined(__NetBSD__)
32#pragma weak pthread_create // Do not create libpthread dependency
33#endif
Howard Hinnant725ae712013-07-02 17:53:48 +000034#if defined(_WIN32)
35#include <windows.h>
36#endif
Joerg Sonnenberger67444032013-05-17 21:16:18 +000037
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000038_LIBCPP_BEGIN_NAMESPACE_STD
39
40thread::~thread()
41{
Howard Hinnantadff4892010-05-24 17:49:41 +000042 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043 terminate();
44}
45
46void
47thread::join()
48{
Asiri Rathnayake0dd618b2016-06-03 08:45:26 +000049 int ec = EINVAL;
50 if (__t_ != 0)
51 {
52 ec = __libcpp_thread_join(&__t_);
53 if (ec == 0)
54 __t_ = 0;
55 }
Marshall Clow14c09a22016-08-25 15:09:01 +000056
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057 if (ec)
Marshall Clow14c09a22016-08-25 15:09:01 +000058 __throw_system_error(ec, "thread::join failed");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059}
60
61void
62thread::detach()
63{
64 int ec = EINVAL;
65 if (__t_ != 0)
66 {
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +000067 ec = __libcpp_thread_detach(&__t_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068 if (ec == 0)
69 __t_ = 0;
70 }
Marshall Clow14c09a22016-08-25 15:09:01 +000071
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072 if (ec)
Marshall Clow14c09a22016-08-25 15:09:01 +000073 __throw_system_error(ec, "thread::detach failed");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074}
75
76unsigned
Howard Hinnant6e1d8512012-07-21 16:50:47 +000077thread::hardware_concurrency() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078{
Howard Hinnantadff4892010-05-24 17:49:41 +000079#if defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantec3773c2011-12-01 20:21:04 +000080 unsigned n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000081 int mib[2] = {CTL_HW, HW_NCPU};
82 std::size_t s = sizeof(n);
83 sysctl(mib, 2, &n, &s, 0, 0);
84 return n;
Howard Hinnant312926e2013-06-30 00:14:43 +000085#elif defined(_SC_NPROCESSORS_ONLN)
Howard Hinnant403f91a2012-08-02 18:17:49 +000086 long result = sysconf(_SC_NPROCESSORS_ONLN);
Howard Hinnant5f767b72012-12-27 23:24:31 +000087 // sysconf returns -1 if the name is invalid, the option does not exist or
88 // does not have a definite limit.
Marshall Clowb18165e2013-02-07 18:48:09 +000089 // if sysconf returns some other negative number, we have no idea
90 // what is going on. Default to something safe.
91 if (result < 0)
Howard Hinnant5f767b72012-12-27 23:24:31 +000092 return 0;
Marshall Clowd854ce62013-02-07 17:37:58 +000093 return static_cast<unsigned>(result);
Howard Hinnant725ae712013-07-02 17:53:48 +000094#elif defined(_WIN32)
95 SYSTEM_INFO info;
96 GetSystemInfo(&info);
97 return info.dwNumberOfProcessors;
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000098#else // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantadff4892010-05-24 17:49:41 +000099 // TODO: grovel through /proc or check cpuid on x86 and similar
100 // instructions on other architectures.
Howard Hinnantf7555062013-10-04 21:14:44 +0000101# if defined(_MSC_VER) && ! defined(__clang__)
102 _LIBCPP_WARNING("hardware_concurrency not yet implemented")
103# else
104# warning hardware_concurrency not yet implemented
105# endif
Howard Hinnantadff4892010-05-24 17:49:41 +0000106 return 0; // Means not computable [thread.thread.static]
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000107#endif // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000108}
109
110namespace this_thread
111{
112
113void
114sleep_for(const chrono::nanoseconds& ns)
115{
116 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000117 if (ns > nanoseconds::zero())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118 {
Howard Hinnantcf115d22012-08-30 19:14:33 +0000119 seconds s = duration_cast<seconds>(ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120 timespec ts;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000121 typedef decltype(ts.tv_sec) ts_sec;
122 _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
123 if (s.count() < ts_sec_max)
124 {
125 ts.tv_sec = static_cast<ts_sec>(s.count());
126 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns-s).count());
127 }
128 else
129 {
130 ts.tv_sec = ts_sec_max;
131 ts.tv_nsec = giga::num - 1;
132 }
David Majnemer0707b672014-06-04 19:43:20 +0000133
134 while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
135 ;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136 }
137}
138
139} // this_thread
140
Howard Hinnant5306d682010-10-14 19:18:04 +0000141__thread_specific_ptr<__thread_struct>&
142__thread_local_data()
143{
144 static __thread_specific_ptr<__thread_struct> __p;
145 return __p;
146}
Howard Hinnant47499b12010-08-27 20:10:19 +0000147
148// __thread_struct_imp
149
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000150template <class T>
151class _LIBCPP_HIDDEN __hidden_allocator
Howard Hinnant47499b12010-08-27 20:10:19 +0000152{
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000153public:
154 typedef T value_type;
155
156 T* allocate(size_t __n)
157 {return static_cast<T*>(::operator new(__n * sizeof(T)));}
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000158 void deallocate(T* __p, size_t) {::operator delete(static_cast<void*>(__p));}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000159
160 size_t max_size() const {return size_t(~0) / sizeof(T);}
161};
162
163class _LIBCPP_HIDDEN __thread_struct_imp
164{
165 typedef vector<__assoc_sub_state*,
166 __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
167 typedef vector<pair<condition_variable*, mutex*>,
168 __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
Howard Hinnante6e4d012010-09-03 21:46:37 +0000169
Howard Hinnant47499b12010-08-27 20:10:19 +0000170 _AsyncStates async_states_;
Howard Hinnante6e4d012010-09-03 21:46:37 +0000171 _Notify notify_;
Howard Hinnant47499b12010-08-27 20:10:19 +0000172
173 __thread_struct_imp(const __thread_struct_imp&);
174 __thread_struct_imp& operator=(const __thread_struct_imp&);
175public:
176 __thread_struct_imp() {}
177 ~__thread_struct_imp();
178
Howard Hinnante6e4d012010-09-03 21:46:37 +0000179 void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
Howard Hinnant47499b12010-08-27 20:10:19 +0000180 void __make_ready_at_thread_exit(__assoc_sub_state* __s);
181};
182
183__thread_struct_imp::~__thread_struct_imp()
184{
Howard Hinnante6e4d012010-09-03 21:46:37 +0000185 for (_Notify::iterator i = notify_.begin(), e = notify_.end();
186 i != e; ++i)
187 {
188 i->second->unlock();
189 i->first->notify_all();
190 }
Howard Hinnant47499b12010-08-27 20:10:19 +0000191 for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
192 i != e; ++i)
193 {
194 (*i)->__make_ready();
195 (*i)->__release_shared();
196 }
197}
198
199void
Howard Hinnante6e4d012010-09-03 21:46:37 +0000200__thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
201{
202 notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
203}
204
205void
Howard Hinnant47499b12010-08-27 20:10:19 +0000206__thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
207{
208 async_states_.push_back(__s);
209 __s->__add_shared();
210}
211
212// __thread_struct
213
214__thread_struct::__thread_struct()
215 : __p_(new __thread_struct_imp)
216{
217}
218
219__thread_struct::~__thread_struct()
220{
221 delete __p_;
222}
223
224void
Howard Hinnante6e4d012010-09-03 21:46:37 +0000225__thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
226{
227 __p_->notify_all_at_thread_exit(cv, m);
228}
229
230void
Howard Hinnant47499b12010-08-27 20:10:19 +0000231__thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
232{
233 __p_->__make_ready_at_thread_exit(__s);
234}
235
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000237
238#endif // !_LIBCPP_HAS_NO_THREADS