blob: 4445b8dbb9c66494359eef739d6cf8e0752e491b [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 Hinnantd031c582010-05-25 17:25:25 +000014#include <sys/types.h>
Howard Hinnant55c3c5f2012-08-02 18:17:49 +000015#if !_WIN32
16#if !__sun__ && !__linux__
Howard Hinnantc51e1022010-05-11 19:42:16 +000017#include <sys/sysctl.h>
Howard Hinnant55c3c5f2012-08-02 18:17:49 +000018#else
19#include <unistd.h>
20#endif // !__sun__ && !__linux__
21#endif // !_WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +000022
23_LIBCPP_BEGIN_NAMESPACE_STD
24
25thread::~thread()
26{
Howard Hinnant155c2af2010-05-24 17:49:41 +000027 if (__t_ != 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +000028 terminate();
29}
30
31void
32thread::join()
33{
34 int ec = pthread_join(__t_, 0);
Howard Hinnant72f73582010-08-11 17:04:31 +000035#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000036 if (ec)
37 throw system_error(error_code(ec, system_category()), "thread::join failed");
Howard Hinnantffb308e2010-08-22 00:03:27 +000038#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant155c2af2010-05-24 17:49:41 +000039 __t_ = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +000040}
41
42void
43thread::detach()
44{
45 int ec = EINVAL;
46 if (__t_ != 0)
47 {
48 ec = pthread_detach(__t_);
49 if (ec == 0)
50 __t_ = 0;
51 }
Howard Hinnant72f73582010-08-11 17:04:31 +000052#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000053 if (ec)
54 throw system_error(error_code(ec, system_category()), "thread::detach failed");
Howard Hinnantffb308e2010-08-22 00:03:27 +000055#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000056}
57
58unsigned
Howard Hinnant4bd34702012-07-21 16:50:47 +000059thread::hardware_concurrency() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000060{
Howard Hinnant155c2af2010-05-24 17:49:41 +000061#if defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnant28b24882011-12-01 20:21:04 +000062 unsigned n;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063 int mib[2] = {CTL_HW, HW_NCPU};
64 std::size_t s = sizeof(n);
65 sysctl(mib, 2, &n, &s, 0, 0);
66 return n;
Howard Hinnant55c3c5f2012-08-02 18:17:49 +000067#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN)
68 long result = sysconf(_SC_NPROCESSORS_ONLN);
69 if (result < 0 || result > UINT_MAX)
70 result = 0;
71 return result;
Howard Hinnantffb308e2010-08-22 00:03:27 +000072#else // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnant155c2af2010-05-24 17:49:41 +000073 // TODO: grovel through /proc or check cpuid on x86 and similar
74 // instructions on other architectures.
75 return 0; // Means not computable [thread.thread.static]
Howard Hinnantffb308e2010-08-22 00:03:27 +000076#endif // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantc51e1022010-05-11 19:42:16 +000077}
78
79namespace this_thread
80{
81
82void
83sleep_for(const chrono::nanoseconds& ns)
84{
85 using namespace chrono;
86 if (ns >= nanoseconds::zero())
87 {
88 timespec ts;
89 ts.tv_sec = static_cast<decltype(ts.tv_sec)>(duration_cast<seconds>(ns).count());
90 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns - seconds(ts.tv_sec)).count());
91 nanosleep(&ts, 0);
92 }
93}
94
95} // this_thread
96
Howard Hinnant15d55052010-10-14 19:18:04 +000097__thread_specific_ptr<__thread_struct>&
98__thread_local_data()
99{
100 static __thread_specific_ptr<__thread_struct> __p;
101 return __p;
102}
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000103
104// __thread_struct_imp
105
Howard Hinnantcf823322010-12-17 14:46:43 +0000106template <class T>
107class _LIBCPP_HIDDEN __hidden_allocator
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000108{
Howard Hinnantcf823322010-12-17 14:46:43 +0000109public:
110 typedef T value_type;
111
112 T* allocate(size_t __n)
113 {return static_cast<T*>(::operator new(__n * sizeof(T)));}
114 void deallocate(T* __p, size_t) {::operator delete((void*)__p);}
115
116 size_t max_size() const {return size_t(~0) / sizeof(T);}
117};
118
119class _LIBCPP_HIDDEN __thread_struct_imp
120{
121 typedef vector<__assoc_sub_state*,
122 __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
123 typedef vector<pair<condition_variable*, mutex*>,
124 __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
Howard Hinnante6a10852010-09-03 21:46:37 +0000125
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000126 _AsyncStates async_states_;
Howard Hinnante6a10852010-09-03 21:46:37 +0000127 _Notify notify_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000128
129 __thread_struct_imp(const __thread_struct_imp&);
130 __thread_struct_imp& operator=(const __thread_struct_imp&);
131public:
132 __thread_struct_imp() {}
133 ~__thread_struct_imp();
134
Howard Hinnante6a10852010-09-03 21:46:37 +0000135 void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000136 void __make_ready_at_thread_exit(__assoc_sub_state* __s);
137};
138
139__thread_struct_imp::~__thread_struct_imp()
140{
Howard Hinnante6a10852010-09-03 21:46:37 +0000141 for (_Notify::iterator i = notify_.begin(), e = notify_.end();
142 i != e; ++i)
143 {
144 i->second->unlock();
145 i->first->notify_all();
146 }
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000147 for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
148 i != e; ++i)
149 {
150 (*i)->__make_ready();
151 (*i)->__release_shared();
152 }
153}
154
155void
Howard Hinnante6a10852010-09-03 21:46:37 +0000156__thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
157{
158 notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
159}
160
161void
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000162__thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
163{
164 async_states_.push_back(__s);
165 __s->__add_shared();
166}
167
168// __thread_struct
169
170__thread_struct::__thread_struct()
171 : __p_(new __thread_struct_imp)
172{
173}
174
175__thread_struct::~__thread_struct()
176{
177 delete __p_;
178}
179
180void
Howard Hinnante6a10852010-09-03 21:46:37 +0000181__thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
182{
183 __p_->notify_all_at_thread_exit(cv, m);
184}
185
186void
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000187__thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
188{
189 __p_->__make_ready_at_thread_exit(__s);
190}
191
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192_LIBCPP_END_NAMESPACE_STD