blob: 406e71d2ac64d8f835515d99936457c75fa98b59 [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 Rathnayake35ff03b2016-05-06 14:06:29 +000049 int ec = __libcpp_thread_join(&__t_);
Howard Hinnantd4444702010-08-11 17:04:31 +000050#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051 if (ec)
52 throw system_error(error_code(ec, system_category()), "thread::join failed");
Howard Hinnantdb4d4782013-03-28 18:56:26 +000053#else
54 (void)ec;
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000055#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantadff4892010-05-24 17:49:41 +000056 __t_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057}
58
59void
60thread::detach()
61{
62 int ec = EINVAL;
63 if (__t_ != 0)
64 {
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +000065 ec = __libcpp_thread_detach(&__t_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066 if (ec == 0)
67 __t_ = 0;
68 }
Howard Hinnantd4444702010-08-11 17:04:31 +000069#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070 if (ec)
71 throw system_error(error_code(ec, system_category()), "thread::detach failed");
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000072#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073}
74
75unsigned
Howard Hinnant6e1d8512012-07-21 16:50:47 +000076thread::hardware_concurrency() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077{
Howard Hinnantadff4892010-05-24 17:49:41 +000078#if defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantec3773c2011-12-01 20:21:04 +000079 unsigned n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080 int mib[2] = {CTL_HW, HW_NCPU};
81 std::size_t s = sizeof(n);
82 sysctl(mib, 2, &n, &s, 0, 0);
83 return n;
Howard Hinnant312926e2013-06-30 00:14:43 +000084#elif defined(_SC_NPROCESSORS_ONLN)
Howard Hinnant403f91a2012-08-02 18:17:49 +000085 long result = sysconf(_SC_NPROCESSORS_ONLN);
Howard Hinnant5f767b72012-12-27 23:24:31 +000086 // sysconf returns -1 if the name is invalid, the option does not exist or
87 // does not have a definite limit.
Marshall Clowb18165e2013-02-07 18:48:09 +000088 // if sysconf returns some other negative number, we have no idea
89 // what is going on. Default to something safe.
90 if (result < 0)
Howard Hinnant5f767b72012-12-27 23:24:31 +000091 return 0;
Marshall Clowd854ce62013-02-07 17:37:58 +000092 return static_cast<unsigned>(result);
Howard Hinnant725ae712013-07-02 17:53:48 +000093#elif defined(_WIN32)
94 SYSTEM_INFO info;
95 GetSystemInfo(&info);
96 return info.dwNumberOfProcessors;
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000097#else // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantadff4892010-05-24 17:49:41 +000098 // TODO: grovel through /proc or check cpuid on x86 and similar
99 // instructions on other architectures.
Howard Hinnantf7555062013-10-04 21:14:44 +0000100# if defined(_MSC_VER) && ! defined(__clang__)
101 _LIBCPP_WARNING("hardware_concurrency not yet implemented")
102# else
103# warning hardware_concurrency not yet implemented
104# endif
Howard Hinnantadff4892010-05-24 17:49:41 +0000105 return 0; // Means not computable [thread.thread.static]
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000106#endif // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107}
108
109namespace this_thread
110{
111
112void
113sleep_for(const chrono::nanoseconds& ns)
114{
115 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000116 if (ns > nanoseconds::zero())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117 {
Howard Hinnantcf115d22012-08-30 19:14:33 +0000118 seconds s = duration_cast<seconds>(ns);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119 timespec ts;
Howard Hinnantcf115d22012-08-30 19:14:33 +0000120 typedef decltype(ts.tv_sec) ts_sec;
121 _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
122 if (s.count() < ts_sec_max)
123 {
124 ts.tv_sec = static_cast<ts_sec>(s.count());
125 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns-s).count());
126 }
127 else
128 {
129 ts.tv_sec = ts_sec_max;
130 ts.tv_nsec = giga::num - 1;
131 }
David Majnemer0707b672014-06-04 19:43:20 +0000132
133 while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
134 ;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135 }
136}
137
138} // this_thread
139
Howard Hinnant5306d682010-10-14 19:18:04 +0000140__thread_specific_ptr<__thread_struct>&
141__thread_local_data()
142{
143 static __thread_specific_ptr<__thread_struct> __p;
144 return __p;
145}
Howard Hinnant47499b12010-08-27 20:10:19 +0000146
147// __thread_struct_imp
148
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000149template <class T>
150class _LIBCPP_HIDDEN __hidden_allocator
Howard Hinnant47499b12010-08-27 20:10:19 +0000151{
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000152public:
153 typedef T value_type;
154
155 T* allocate(size_t __n)
156 {return static_cast<T*>(::operator new(__n * sizeof(T)));}
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000157 void deallocate(T* __p, size_t) {::operator delete(static_cast<void*>(__p));}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000158
159 size_t max_size() const {return size_t(~0) / sizeof(T);}
160};
161
162class _LIBCPP_HIDDEN __thread_struct_imp
163{
164 typedef vector<__assoc_sub_state*,
165 __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
166 typedef vector<pair<condition_variable*, mutex*>,
167 __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
Howard Hinnante6e4d012010-09-03 21:46:37 +0000168
Howard Hinnant47499b12010-08-27 20:10:19 +0000169 _AsyncStates async_states_;
Howard Hinnante6e4d012010-09-03 21:46:37 +0000170 _Notify notify_;
Howard Hinnant47499b12010-08-27 20:10:19 +0000171
172 __thread_struct_imp(const __thread_struct_imp&);
173 __thread_struct_imp& operator=(const __thread_struct_imp&);
174public:
175 __thread_struct_imp() {}
176 ~__thread_struct_imp();
177
Howard Hinnante6e4d012010-09-03 21:46:37 +0000178 void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
Howard Hinnant47499b12010-08-27 20:10:19 +0000179 void __make_ready_at_thread_exit(__assoc_sub_state* __s);
180};
181
182__thread_struct_imp::~__thread_struct_imp()
183{
Howard Hinnante6e4d012010-09-03 21:46:37 +0000184 for (_Notify::iterator i = notify_.begin(), e = notify_.end();
185 i != e; ++i)
186 {
187 i->second->unlock();
188 i->first->notify_all();
189 }
Howard Hinnant47499b12010-08-27 20:10:19 +0000190 for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
191 i != e; ++i)
192 {
193 (*i)->__make_ready();
194 (*i)->__release_shared();
195 }
196}
197
198void
Howard Hinnante6e4d012010-09-03 21:46:37 +0000199__thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
200{
201 notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
202}
203
204void
Howard Hinnant47499b12010-08-27 20:10:19 +0000205__thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
206{
207 async_states_.push_back(__s);
208 __s->__add_shared();
209}
210
211// __thread_struct
212
213__thread_struct::__thread_struct()
214 : __p_(new __thread_struct_imp)
215{
216}
217
218__thread_struct::~__thread_struct()
219{
220 delete __p_;
221}
222
223void
Howard Hinnante6e4d012010-09-03 21:46:37 +0000224__thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
225{
226 __p_->notify_all_at_thread_exit(cv, m);
227}
228
229void
Howard Hinnant47499b12010-08-27 20:10:19 +0000230__thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
231{
232 __p_->__make_ready_at_thread_exit(__s);
233}
234
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000236
237#endif // !_LIBCPP_HAS_NO_THREADS