blob: 2b3729f674d1339b527c69e2717b44d0686c59fe [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===------------------------- thread.cpp----------------------------------===//
2//
Howard Hinnantc566dc32010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00004//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "thread"
11#include "exception"
Howard Hinnant3820f6b2010-08-27 20:10:19 +000012#include "vector"
13#include "future"
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000014#include "limits"
Howard Hinnantd031c582010-05-25 17:25:25 +000015#include <sys/types.h>
Marshall Clow1f257322013-03-18 17:04:29 +000016#if !defined(_WIN32)
Marshall Clow82378c02013-03-18 19:34:07 +000017#if !defined(__sun__) && !defined(__linux__)
Howard Hinnantc51e1022010-05-11 19:42:16 +000018#include <sys/sysctl.h>
Howard Hinnant55c3c5f2012-08-02 18:17:49 +000019#else
20#include <unistd.h>
21#endif // !__sun__ && !__linux__
22#endif // !_WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +000023
Joerg Sonnenberger22939542013-05-17 21:16:18 +000024#if defined(__NetBSD__)
25#pragma weak pthread_create // Do not create libpthread dependency
26#endif
27
Howard Hinnantc51e1022010-05-11 19:42:16 +000028_LIBCPP_BEGIN_NAMESPACE_STD
29
30thread::~thread()
31{
Howard Hinnant155c2af2010-05-24 17:49:41 +000032 if (__t_ != 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 terminate();
34}
35
36void
37thread::join()
38{
39 int ec = pthread_join(__t_, 0);
Howard Hinnant72f73582010-08-11 17:04:31 +000040#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 if (ec)
42 throw system_error(error_code(ec, system_category()), "thread::join failed");
Howard Hinnant417bc432013-03-28 18:56:26 +000043#else
44 (void)ec;
Howard Hinnantffb308e2010-08-22 00:03:27 +000045#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant155c2af2010-05-24 17:49:41 +000046 __t_ = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +000047}
48
49void
50thread::detach()
51{
52 int ec = EINVAL;
53 if (__t_ != 0)
54 {
55 ec = pthread_detach(__t_);
56 if (ec == 0)
57 __t_ = 0;
58 }
Howard Hinnant72f73582010-08-11 17:04:31 +000059#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000060 if (ec)
61 throw system_error(error_code(ec, system_category()), "thread::detach failed");
Howard Hinnantffb308e2010-08-22 00:03:27 +000062#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000063}
64
65unsigned
Howard Hinnant4bd34702012-07-21 16:50:47 +000066thread::hardware_concurrency() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000067{
Howard Hinnant155c2af2010-05-24 17:49:41 +000068#if defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnant28b24882011-12-01 20:21:04 +000069 unsigned n;
Howard Hinnantc51e1022010-05-11 19:42:16 +000070 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;
Howard Hinnant942dbd22013-03-29 18:27:28 +000074#elif (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN)) || defined(EMSCRIPTEN)
Howard Hinnant55c3c5f2012-08-02 18:17:49 +000075 long result = sysconf(_SC_NPROCESSORS_ONLN);
Howard Hinnant3f648332012-12-27 23:24:31 +000076 // sysconf returns -1 if the name is invalid, the option does not exist or
77 // does not have a definite limit.
Marshall Clowdcfde7a2013-02-07 18:48:09 +000078 // if sysconf returns some other negative number, we have no idea
79 // what is going on. Default to something safe.
80 if (result < 0)
Howard Hinnant3f648332012-12-27 23:24:31 +000081 return 0;
Marshall Clow84d47b92013-02-07 17:37:58 +000082 return static_cast<unsigned>(result);
Howard Hinnantffb308e2010-08-22 00:03:27 +000083#else // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnant155c2af2010-05-24 17:49:41 +000084 // 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]
Howard Hinnantffb308e2010-08-22 00:03:27 +000087#endif // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantc51e1022010-05-11 19:42:16 +000088}
89
90namespace this_thread
91{
92
93void
94sleep_for(const chrono::nanoseconds& ns)
95{
96 using namespace chrono;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000097 if (ns > nanoseconds::zero())
Howard Hinnantc51e1022010-05-11 19:42:16 +000098 {
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000099 seconds s = duration_cast<seconds>(ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 timespec ts;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000101 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 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113 nanosleep(&ts, 0);
114 }
115}
116
117} // this_thread
118
Howard Hinnant15d55052010-10-14 19:18:04 +0000119__thread_specific_ptr<__thread_struct>&
120__thread_local_data()
121{
122 static __thread_specific_ptr<__thread_struct> __p;
123 return __p;
124}
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000125
126// __thread_struct_imp
127
Howard Hinnantcf823322010-12-17 14:46:43 +0000128template <class T>
129class _LIBCPP_HIDDEN __hidden_allocator
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000130{
Howard Hinnantcf823322010-12-17 14:46:43 +0000131public:
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
141class _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;
Howard Hinnante6a10852010-09-03 21:46:37 +0000147
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000148 _AsyncStates async_states_;
Howard Hinnante6a10852010-09-03 21:46:37 +0000149 _Notify notify_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000150
151 __thread_struct_imp(const __thread_struct_imp&);
152 __thread_struct_imp& operator=(const __thread_struct_imp&);
153public:
154 __thread_struct_imp() {}
155 ~__thread_struct_imp();
156
Howard Hinnante6a10852010-09-03 21:46:37 +0000157 void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000158 void __make_ready_at_thread_exit(__assoc_sub_state* __s);
159};
160
161__thread_struct_imp::~__thread_struct_imp()
162{
Howard Hinnante6a10852010-09-03 21:46:37 +0000163 for (_Notify::iterator i = notify_.begin(), e = notify_.end();
164 i != e; ++i)
165 {
166 i->second->unlock();
167 i->first->notify_all();
168 }
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000169 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
177void
Howard Hinnante6a10852010-09-03 21:46:37 +0000178__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
183void
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000184__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
202void
Howard Hinnante6a10852010-09-03 21:46:37 +0000203__thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
204{
205 __p_->notify_all_at_thread_exit(cv, m);
206}
207
208void
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000209__thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
210{
211 __p_->__make_ready_at_thread_exit(__s);
212}
213
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214_LIBCPP_END_NAMESPACE_STD