1 //===------------------------- thread.cpp----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_HAS_NO_THREADS
17 #include <sys/types.h>
19 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
20 # include <sys/param.h>
21 # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
22 # include <sys/sysctl.h>
24 #endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
26 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__CloudABI__) || defined(__Fuchsia__) || defined(__wasi__)
28 #endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__CloudABI__) || defined(__Fuchsia__) || defined(__wasi__)
30 #if defined(__NetBSD__)
31 #pragma weak pthread_create // Do not create libpthread dependency
34 #if defined(_LIBCPP_WIN32API)
38 #if defined(__unix__) && defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA)
39 #pragma comment(lib, "pthread")
42 _LIBCPP_BEGIN_NAMESPACE_STD
46 if (!__libcpp_thread_isnull(&__t_))
54 if (!__libcpp_thread_isnull(&__t_))
56 ec = __libcpp_thread_join(&__t_);
58 __t_ = _LIBCPP_NULL_THREAD;
62 __throw_system_error(ec, "thread::join failed");
69 if (!__libcpp_thread_isnull(&__t_))
71 ec = __libcpp_thread_detach(&__t_);
73 __t_ = _LIBCPP_NULL_THREAD;
77 __throw_system_error(ec, "thread::detach failed");
81 thread::hardware_concurrency() _NOEXCEPT
83 #if defined(CTL_HW) && defined(HW_NCPU)
85 int mib[2] = {CTL_HW, HW_NCPU};
86 std::size_t s = sizeof(n);
87 sysctl(mib, 2, &n, &s, 0, 0);
89 #elif defined(_SC_NPROCESSORS_ONLN)
90 long result = sysconf(_SC_NPROCESSORS_ONLN);
91 // sysconf returns -1 if the name is invalid, the option does not exist or
92 // does not have a definite limit.
93 // if sysconf returns some other negative number, we have no idea
94 // what is going on. Default to something safe.
97 return static_cast<unsigned>(result);
98 #elif defined(_LIBCPP_WIN32API)
100 GetSystemInfo(&info);
101 return info.dwNumberOfProcessors;
102 #else // defined(CTL_HW) && defined(HW_NCPU)
103 // TODO: grovel through /proc or check cpuid on x86 and similar
104 // instructions on other architectures.
105 # if defined(_LIBCPP_WARNING)
106 _LIBCPP_WARNING("hardware_concurrency not yet implemented")
108 # warning hardware_concurrency not yet implemented
110 return 0; // Means not computable [thread.thread.static]
111 #endif // defined(CTL_HW) && defined(HW_NCPU)
114 namespace this_thread
118 sleep_for(const chrono::nanoseconds& ns)
120 if (ns > chrono::nanoseconds::zero())
122 __libcpp_thread_sleep_for(ns);
128 __thread_specific_ptr<__thread_struct>&
129 __thread_local_data()
131 static __thread_specific_ptr<__thread_struct> __p;
135 // __thread_struct_imp
138 class _LIBCPP_HIDDEN __hidden_allocator
141 typedef T value_type;
143 T* allocate(size_t __n)
144 {return static_cast<T*>(::operator new(__n * sizeof(T)));}
145 void deallocate(T* __p, size_t) {::operator delete(static_cast<void*>(__p));}
147 size_t max_size() const {return size_t(~0) / sizeof(T);}
150 class _LIBCPP_HIDDEN __thread_struct_imp
152 typedef vector<__assoc_sub_state*,
153 __hidden_allocator<__assoc_sub_state*> > _AsyncStates;
154 typedef vector<pair<condition_variable*, mutex*>,
155 __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify;
157 _AsyncStates async_states_;
160 __thread_struct_imp(const __thread_struct_imp&);
161 __thread_struct_imp& operator=(const __thread_struct_imp&);
163 __thread_struct_imp() {}
164 ~__thread_struct_imp();
166 void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
167 void __make_ready_at_thread_exit(__assoc_sub_state* __s);
170 __thread_struct_imp::~__thread_struct_imp()
172 for (_Notify::iterator i = notify_.begin(), e = notify_.end();
176 i->first->notify_all();
178 for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
181 (*i)->__make_ready();
182 (*i)->__release_shared();
187 __thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
189 notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
193 __thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
195 async_states_.push_back(__s);
201 __thread_struct::__thread_struct()
202 : __p_(new __thread_struct_imp)
206 __thread_struct::~__thread_struct()
212 __thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
214 __p_->notify_all_at_thread_exit(cv, m);
218 __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
220 __p_->__make_ready_at_thread_exit(__s);
223 _LIBCPP_END_NAMESPACE_STD
225 #endif // !_LIBCPP_HAS_NO_THREADS