blob: 3d388e9f0301d12313abef1a4f82212de030207a [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//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "thread"
11#include "exception"
Howard Hinnant47499b12010-08-27 20:10:19 +000012#include "vector"
13#include "future"
Howard Hinnant0bfe8802010-05-25 17:25:25 +000014#include <sys/types.h>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000015#include <sys/sysctl.h>
16
17_LIBCPP_BEGIN_NAMESPACE_STD
18
19thread::~thread()
20{
Howard Hinnantadff4892010-05-24 17:49:41 +000021 if (__t_ != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022 terminate();
23}
24
25void
26thread::join()
27{
28 int ec = pthread_join(__t_, 0);
Howard Hinnantd4444702010-08-11 17:04:31 +000029#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030 if (ec)
31 throw system_error(error_code(ec, system_category()), "thread::join failed");
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000032#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantadff4892010-05-24 17:49:41 +000033 __t_ = 0;
Howard Hinnantbc8d3f92010-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 Hinnantd4444702010-08-11 17:04:31 +000046#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 if (ec)
48 throw system_error(error_code(ec, system_category()), "thread::detach failed");
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000049#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000050}
51
52unsigned
53thread::hardware_concurrency()
54{
Howard Hinnantadff4892010-05-24 17:49:41 +000055#if defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantbc8d3f92010-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 Hinnant16e6e1d2010-08-22 00:03:27 +000061#else // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantadff4892010-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 Hinnant16e6e1d2010-08-22 00:03:27 +000065#endif // defined(CTL_HW) && defined(HW_NCPU)
Howard Hinnantbc8d3f92010-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 Hinnant47499b12010-08-27 20:10:19 +000086__thread_specific_ptr<__thread_struct> __thread_local_data;
87
88// __thread_struct_imp
89
90class __thread_struct_imp
91{
92 typedef vector<__assoc_sub_state*> _AsyncStates;
Howard Hinnante6e4d012010-09-03 21:46:37 +000093 typedef vector<pair<condition_variable*, mutex*> > _Notify;
94
Howard Hinnant47499b12010-08-27 20:10:19 +000095 _AsyncStates async_states_;
Howard Hinnante6e4d012010-09-03 21:46:37 +000096 _Notify notify_;
Howard Hinnant47499b12010-08-27 20:10:19 +000097
98 __thread_struct_imp(const __thread_struct_imp&);
99 __thread_struct_imp& operator=(const __thread_struct_imp&);
100public:
101 __thread_struct_imp() {}
102 ~__thread_struct_imp();
103
Howard Hinnante6e4d012010-09-03 21:46:37 +0000104 void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
Howard Hinnant47499b12010-08-27 20:10:19 +0000105 void __make_ready_at_thread_exit(__assoc_sub_state* __s);
106};
107
108__thread_struct_imp::~__thread_struct_imp()
109{
Howard Hinnante6e4d012010-09-03 21:46:37 +0000110 for (_Notify::iterator i = notify_.begin(), e = notify_.end();
111 i != e; ++i)
112 {
113 i->second->unlock();
114 i->first->notify_all();
115 }
Howard Hinnant47499b12010-08-27 20:10:19 +0000116 for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
117 i != e; ++i)
118 {
119 (*i)->__make_ready();
120 (*i)->__release_shared();
121 }
122}
123
124void
Howard Hinnante6e4d012010-09-03 21:46:37 +0000125__thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
126{
127 notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
128}
129
130void
Howard Hinnant47499b12010-08-27 20:10:19 +0000131__thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
132{
133 async_states_.push_back(__s);
134 __s->__add_shared();
135}
136
137// __thread_struct
138
139__thread_struct::__thread_struct()
140 : __p_(new __thread_struct_imp)
141{
142}
143
144__thread_struct::~__thread_struct()
145{
146 delete __p_;
147}
148
149void
Howard Hinnante6e4d012010-09-03 21:46:37 +0000150__thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
151{
152 __p_->notify_all_at_thread_exit(cv, m);
153}
154
155void
Howard Hinnant47499b12010-08-27 20:10:19 +0000156__thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
157{
158 __p_->__make_ready_at_thread_exit(__s);
159}
160
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161_LIBCPP_END_NAMESPACE_STD