Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------- thread.cpp----------------------------------===// |
| 2 | // |
Howard Hinnant | c566dc3 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | ee11c31 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "thread" |
| 11 | #include "exception" |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 12 | #include "vector" |
| 13 | #include "future" |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 14 | #include "limits" |
Howard Hinnant | d031c58 | 2010-05-25 17:25:25 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
Marshall Clow | 1f25732 | 2013-03-18 17:04:29 +0000 | [diff] [blame] | 16 | #if !defined(_WIN32) |
Marshall Clow | 82378c0 | 2013-03-18 19:34:07 +0000 | [diff] [blame] | 17 | #if !defined(__sun__) && !defined(__linux__) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 18 | #include <sys/sysctl.h> |
Howard Hinnant | 55c3c5f | 2012-08-02 18:17:49 +0000 | [diff] [blame] | 19 | #else |
| 20 | #include <unistd.h> |
| 21 | #endif // !__sun__ && !__linux__ |
| 22 | #endif // !_WIN32 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | |
Joerg Sonnenberger | 2293954 | 2013-05-17 21:16:18 +0000 | [diff] [blame] | 24 | #if defined(__NetBSD__) |
| 25 | #pragma weak pthread_create // Do not create libpthread dependency |
| 26 | #endif |
| 27 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 29 | |
| 30 | thread::~thread() |
| 31 | { |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 32 | if (__t_ != 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | terminate(); |
| 34 | } |
| 35 | |
| 36 | void |
| 37 | thread::join() |
| 38 | { |
| 39 | int ec = pthread_join(__t_, 0); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 40 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 41 | if (ec) |
| 42 | throw system_error(error_code(ec, system_category()), "thread::join failed"); |
Howard Hinnant | 417bc43 | 2013-03-28 18:56:26 +0000 | [diff] [blame] | 43 | #else |
| 44 | (void)ec; |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 45 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 46 | __t_ = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
| 50 | thread::detach() |
| 51 | { |
| 52 | int ec = EINVAL; |
| 53 | if (__t_ != 0) |
| 54 | { |
| 55 | ec = pthread_detach(__t_); |
| 56 | if (ec == 0) |
| 57 | __t_ = 0; |
| 58 | } |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 59 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | if (ec) |
| 61 | throw system_error(error_code(ec, system_category()), "thread::detach failed"); |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 62 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | unsigned |
Howard Hinnant | 4bd3470 | 2012-07-21 16:50:47 +0000 | [diff] [blame] | 66 | thread::hardware_concurrency() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 67 | { |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 68 | #if defined(CTL_HW) && defined(HW_NCPU) |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 69 | unsigned n; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | int mib[2] = {CTL_HW, HW_NCPU}; |
| 71 | std::size_t s = sizeof(n); |
| 72 | sysctl(mib, 2, &n, &s, 0, 0); |
| 73 | return n; |
Howard Hinnant | 942dbd2 | 2013-03-29 18:27:28 +0000 | [diff] [blame] | 74 | #elif (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN)) || defined(EMSCRIPTEN) |
Howard Hinnant | 55c3c5f | 2012-08-02 18:17:49 +0000 | [diff] [blame] | 75 | long result = sysconf(_SC_NPROCESSORS_ONLN); |
Howard Hinnant | 3f64833 | 2012-12-27 23:24:31 +0000 | [diff] [blame] | 76 | // sysconf returns -1 if the name is invalid, the option does not exist or |
| 77 | // does not have a definite limit. |
Marshall Clow | dcfde7a | 2013-02-07 18:48:09 +0000 | [diff] [blame] | 78 | // if sysconf returns some other negative number, we have no idea |
| 79 | // what is going on. Default to something safe. |
| 80 | if (result < 0) |
Howard Hinnant | 3f64833 | 2012-12-27 23:24:31 +0000 | [diff] [blame] | 81 | return 0; |
Marshall Clow | 84d47b9 | 2013-02-07 17:37:58 +0000 | [diff] [blame] | 82 | return static_cast<unsigned>(result); |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 83 | #else // defined(CTL_HW) && defined(HW_NCPU) |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 84 | // TODO: grovel through /proc or check cpuid on x86 and similar |
| 85 | // instructions on other architectures. |
| 86 | return 0; // Means not computable [thread.thread.static] |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 87 | #endif // defined(CTL_HW) && defined(HW_NCPU) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | namespace this_thread |
| 91 | { |
| 92 | |
| 93 | void |
| 94 | sleep_for(const chrono::nanoseconds& ns) |
| 95 | { |
| 96 | using namespace chrono; |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 97 | if (ns > nanoseconds::zero()) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 98 | { |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 99 | seconds s = duration_cast<seconds>(ns); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 100 | timespec ts; |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 101 | typedef decltype(ts.tv_sec) ts_sec; |
| 102 | _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); |
| 103 | if (s.count() < ts_sec_max) |
| 104 | { |
| 105 | ts.tv_sec = static_cast<ts_sec>(s.count()); |
| 106 | ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns-s).count()); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | ts.tv_sec = ts_sec_max; |
| 111 | ts.tv_nsec = giga::num - 1; |
| 112 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 113 | nanosleep(&ts, 0); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } // this_thread |
| 118 | |
Howard Hinnant | 15d5505 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 119 | __thread_specific_ptr<__thread_struct>& |
| 120 | __thread_local_data() |
| 121 | { |
| 122 | static __thread_specific_ptr<__thread_struct> __p; |
| 123 | return __p; |
| 124 | } |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 125 | |
| 126 | // __thread_struct_imp |
| 127 | |
Howard Hinnant | cf82332 | 2010-12-17 14:46:43 +0000 | [diff] [blame] | 128 | template <class T> |
| 129 | class _LIBCPP_HIDDEN __hidden_allocator |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 130 | { |
Howard Hinnant | cf82332 | 2010-12-17 14:46:43 +0000 | [diff] [blame] | 131 | public: |
| 132 | typedef T value_type; |
| 133 | |
| 134 | T* allocate(size_t __n) |
| 135 | {return static_cast<T*>(::operator new(__n * sizeof(T)));} |
| 136 | void deallocate(T* __p, size_t) {::operator delete((void*)__p);} |
| 137 | |
| 138 | size_t max_size() const {return size_t(~0) / sizeof(T);} |
| 139 | }; |
| 140 | |
| 141 | class _LIBCPP_HIDDEN __thread_struct_imp |
| 142 | { |
| 143 | typedef vector<__assoc_sub_state*, |
| 144 | __hidden_allocator<__assoc_sub_state*> > _AsyncStates; |
| 145 | typedef vector<pair<condition_variable*, mutex*>, |
| 146 | __hidden_allocator<pair<condition_variable*, mutex*> > > _Notify; |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 147 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 148 | _AsyncStates async_states_; |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 149 | _Notify notify_; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 150 | |
| 151 | __thread_struct_imp(const __thread_struct_imp&); |
| 152 | __thread_struct_imp& operator=(const __thread_struct_imp&); |
| 153 | public: |
| 154 | __thread_struct_imp() {} |
| 155 | ~__thread_struct_imp(); |
| 156 | |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 157 | void notify_all_at_thread_exit(condition_variable* cv, mutex* m); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 158 | void __make_ready_at_thread_exit(__assoc_sub_state* __s); |
| 159 | }; |
| 160 | |
| 161 | __thread_struct_imp::~__thread_struct_imp() |
| 162 | { |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 163 | for (_Notify::iterator i = notify_.begin(), e = notify_.end(); |
| 164 | i != e; ++i) |
| 165 | { |
| 166 | i->second->unlock(); |
| 167 | i->first->notify_all(); |
| 168 | } |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 169 | for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end(); |
| 170 | i != e; ++i) |
| 171 | { |
| 172 | (*i)->__make_ready(); |
| 173 | (*i)->__release_shared(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 178 | __thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m) |
| 179 | { |
| 180 | notify_.push_back(pair<condition_variable*, mutex*>(cv, m)); |
| 181 | } |
| 182 | |
| 183 | void |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 184 | __thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s) |
| 185 | { |
| 186 | async_states_.push_back(__s); |
| 187 | __s->__add_shared(); |
| 188 | } |
| 189 | |
| 190 | // __thread_struct |
| 191 | |
| 192 | __thread_struct::__thread_struct() |
| 193 | : __p_(new __thread_struct_imp) |
| 194 | { |
| 195 | } |
| 196 | |
| 197 | __thread_struct::~__thread_struct() |
| 198 | { |
| 199 | delete __p_; |
| 200 | } |
| 201 | |
| 202 | void |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 203 | __thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m) |
| 204 | { |
| 205 | __p_->notify_all_at_thread_exit(cv, m); |
| 206 | } |
| 207 | |
| 208 | void |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 209 | __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s) |
| 210 | { |
| 211 | __p_->__make_ready_at_thread_exit(__s); |
| 212 | } |
| 213 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | _LIBCPP_END_NAMESPACE_STD |