1 //===------------------------- thread.cpp----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include <sys/types.h>
15 #if !_WIN32 && !__sun__
16 #include <sys/sysctl.h>
19 _LIBCPP_BEGIN_NAMESPACE_STD
30 int ec = pthread_join(__t_, 0);
31 #ifndef _LIBCPP_NO_EXCEPTIONS
33 throw system_error(error_code(ec, system_category()), "thread::join failed");
34 #endif // _LIBCPP_NO_EXCEPTIONS
44 ec = pthread_detach(__t_);
48 #ifndef _LIBCPP_NO_EXCEPTIONS
50 throw system_error(error_code(ec, system_category()), "thread::detach failed");
51 #endif // _LIBCPP_NO_EXCEPTIONS
55 thread::hardware_concurrency()
57 #if defined(CTL_HW) && defined(HW_NCPU)
59 int mib[2] = {CTL_HW, HW_NCPU};
60 std::size_t s = sizeof(n);
61 sysctl(mib, 2, &n, &s, 0, 0);
63 #else // defined(CTL_HW) && defined(HW_NCPU)
64 // TODO: grovel through /proc or check cpuid on x86 and similar
65 // instructions on other architectures.
66 return 0; // Means not computable [thread.thread.static]
67 #endif // defined(CTL_HW) && defined(HW_NCPU)
74 sleep_for(const chrono::nanoseconds& ns)
76 using namespace chrono;
77 if (ns >= nanoseconds::zero())
80 ts.tv_sec = static_cast<decltype(ts.tv_sec)>(duration_cast<seconds>(ns).count());
81 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns - seconds(ts.tv_sec)).count());
88 __thread_specific_ptr<__thread_struct>&
91 static __thread_specific_ptr<__thread_struct> __p;
95 // __thread_struct_imp
98 class _LIBCPP_HIDDEN __hidden_allocator
101 typedef T value_type;
103 T* allocate(size_t __n)
104 {return static_cast<T*>(::operator new(__n * sizeof(T)));}
105 void deallocate(T* __p, size_t) {::operator delete((void*)__p);}
107 size_t max_size() const {return size_t(~0) / sizeof(T);}
110 class _LIBCPP_HIDDEN __thread_struct_imp
112 typedef vector<__assoc_sub_state*,
113 __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
114 typedef vector<pair<condition_variable*, mutex*>,
115 __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
117 _AsyncStates async_states_;
120 __thread_struct_imp(const __thread_struct_imp&);
121 __thread_struct_imp& operator=(const __thread_struct_imp&);
123 __thread_struct_imp() {}
124 ~__thread_struct_imp();
126 void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
127 void __make_ready_at_thread_exit(__assoc_sub_state* __s);
130 __thread_struct_imp::~__thread_struct_imp()
132 for (_Notify::iterator i = notify_.begin(), e = notify_.end();
136 i->first->notify_all();
138 for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
141 (*i)->__make_ready();
142 (*i)->__release_shared();
147 __thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
149 notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
153 __thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
155 async_states_.push_back(__s);
161 __thread_struct::__thread_struct()
162 : __p_(new __thread_struct_imp)
166 __thread_struct::~__thread_struct()
172 __thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
174 __p_->notify_all_at_thread_exit(cv, m);
178 __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
180 __p_->__make_ready_at_thread_exit(__s);
183 _LIBCPP_END_NAMESPACE_STD