blob: 4ccff3277f53e976953d5634e735796ab5bf5564 [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 Hinnantc51e1022010-05-11 19:42:16 +000015#include <sys/sysctl.h>
16
17_LIBCPP_BEGIN_NAMESPACE_STD
18
19thread::~thread()
20{
Howard Hinnant155c2af2010-05-24 17:49:41 +000021 if (__t_ != 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +000022 terminate();
23}
24
25void
26thread::join()
27{
28 int ec = pthread_join(__t_, 0);
Howard Hinnant72f73582010-08-11 17:04:31 +000029#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000030 if (ec)
31 throw system_error(error_code(ec, system_category()), "thread::join failed");
Howard Hinnantffb308e2010-08-22 00:03:27 +000032#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant155c2af2010-05-24 17:49:41 +000033 __t_ = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +000034}
35
36void
37thread::detach()
38{
39 int ec = EINVAL;
40 if (__t_ != 0)
41 {
42 ec = pthread_detach(__t_);
43 if (ec == 0)
44 __t_ = 0;
45 }
Howard Hinnant72f73582010-08-11 17:04:31 +000046#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 if (ec)
48 throw system_error(error_code(ec, system_category()), "thread::detach failed");
Howard Hinnantffb308e2010-08-22 00:03:27 +000049#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +000050}
51
52unsigned
53thread::hardware_concurrency()
54{
Howard Hinnant155c2af2010-05-24 17:49:41 +000055#if defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantc51e1022010-05-11 19:42:16 +000056 int n;
57 int mib[2] = {CTL_HW, HW_NCPU};
58 std::size_t s = sizeof(n);
59 sysctl(mib, 2, &n, &s, 0, 0);
60 return n;
Howard Hinnantffb308e2010-08-22 00:03:27 +000061#else // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnant155c2af2010-05-24 17:49:41 +000062 // TODO: grovel through /proc or check cpuid on x86 and similar
63 // instructions on other architectures.
64 return 0; // Means not computable [thread.thread.static]
Howard Hinnantffb308e2010-08-22 00:03:27 +000065#endif // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantc51e1022010-05-11 19:42:16 +000066}
67
68namespace this_thread
69{
70
71void
72sleep_for(const chrono::nanoseconds& ns)
73{
74 using namespace chrono;
75 if (ns >= nanoseconds::zero())
76 {
77 timespec ts;
78 ts.tv_sec = static_cast<decltype(ts.tv_sec)>(duration_cast<seconds>(ns).count());
79 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns - seconds(ts.tv_sec)).count());
80 nanosleep(&ts, 0);
81 }
82}
83
84} // this_thread
85
Howard Hinnant15d55052010-10-14 19:18:04 +000086__thread_specific_ptr<__thread_struct>&
87__thread_local_data()
88{
89 static __thread_specific_ptr<__thread_struct> __p;
90 return __p;
91}
Howard Hinnant3820f6b2010-08-27 20:10:19 +000092
93// __thread_struct_imp
94
Howard Hinnantcf823322010-12-17 14:46:43 +000095template <class T>
96class _LIBCPP_HIDDEN __hidden_allocator
Howard Hinnant3820f6b2010-08-27 20:10:19 +000097{
Howard Hinnantcf823322010-12-17 14:46:43 +000098public:
99 typedef T value_type;
100
101 T* allocate(size_t __n)
102 {return static_cast<T*>(::operator new(__n * sizeof(T)));}
103 void deallocate(T* __p, size_t) {::operator delete((void*)__p);}
104
105 size_t max_size() const {return size_t(~0) / sizeof(T);}
106};
107
108class _LIBCPP_HIDDEN __thread_struct_imp
109{
110 typedef vector<__assoc_sub_state*,
111 __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
112 typedef vector<pair<condition_variable*, mutex*>,
113 __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
Howard Hinnante6a10852010-09-03 21:46:37 +0000114
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000115 _AsyncStates async_states_;
Howard Hinnante6a10852010-09-03 21:46:37 +0000116 _Notify notify_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000117
118 __thread_struct_imp(const __thread_struct_imp&);
119 __thread_struct_imp& operator=(const __thread_struct_imp&);
120public:
121 __thread_struct_imp() {}
122 ~__thread_struct_imp();
123
Howard Hinnante6a10852010-09-03 21:46:37 +0000124 void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000125 void __make_ready_at_thread_exit(__assoc_sub_state* __s);
126};
127
128__thread_struct_imp::~__thread_struct_imp()
129{
Howard Hinnante6a10852010-09-03 21:46:37 +0000130 for (_Notify::iterator i = notify_.begin(), e = notify_.end();
131 i != e; ++i)
132 {
133 i->second->unlock();
134 i->first->notify_all();
135 }
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000136 for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
137 i != e; ++i)
138 {
139 (*i)->__make_ready();
140 (*i)->__release_shared();
141 }
142}
143
144void
Howard Hinnante6a10852010-09-03 21:46:37 +0000145__thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
146{
147 notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
148}
149
150void
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000151__thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
152{
153 async_states_.push_back(__s);
154 __s->__add_shared();
155}
156
157// __thread_struct
158
159__thread_struct::__thread_struct()
160 : __p_(new __thread_struct_imp)
161{
162}
163
164__thread_struct::~__thread_struct()
165{
166 delete __p_;
167}
168
169void
Howard Hinnante6a10852010-09-03 21:46:37 +0000170__thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
171{
172 __p_->notify_all_at_thread_exit(cv, m);
173}
174
175void
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000176__thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
177{
178 __p_->__make_ready_at_thread_exit(__s);
179}
180
Howard Hinnantc51e1022010-05-11 19:42:16 +0000181_LIBCPP_END_NAMESPACE_STD