Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------- mutex.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 | |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 10 | #define _LIBCPP_BUILDING_MUTEX |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | #include "mutex" |
| 12 | #include "limits" |
| 13 | #include "system_error" |
Eric Fiselier | 1366d26 | 2015-08-18 21:08:54 +0000 | [diff] [blame] | 14 | #include "include/atomic_support.h" |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame^] | 15 | #include "__undef_macros" |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 16 | |
| 17 | _LIBCPP_BEGIN_NAMESPACE_STD |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 18 | #ifndef _LIBCPP_HAS_NO_THREADS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | |
| 20 | const defer_lock_t defer_lock = {}; |
| 21 | const try_to_lock_t try_to_lock = {}; |
| 22 | const adopt_lock_t adopt_lock = {}; |
| 23 | |
| 24 | mutex::~mutex() |
| 25 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 26 | __libcpp_mutex_destroy(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void |
| 30 | mutex::lock() |
| 31 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 32 | int ec = __libcpp_mutex_lock(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | if (ec) |
| 34 | __throw_system_error(ec, "mutex lock failed"); |
| 35 | } |
| 36 | |
| 37 | bool |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 38 | mutex::try_lock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | { |
Eric Fiselier | 0ec1447 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 40 | return __libcpp_mutex_trylock(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 44 | mutex::unlock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 46 | int ec = __libcpp_mutex_unlock(&__m_); |
Howard Hinnant | 9ad56f3 | 2013-09-21 21:26:37 +0000 | [diff] [blame] | 47 | (void)ec; |
Eric Fiselier | 59cd98b | 2017-02-04 23:22:28 +0000 | [diff] [blame] | 48 | _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // recursive_mutex |
| 52 | |
| 53 | recursive_mutex::recursive_mutex() |
| 54 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 55 | int ec = __libcpp_recursive_mutex_init(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | if (ec) |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 57 | __throw_system_error(ec, "recursive_mutex constructor failed"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | recursive_mutex::~recursive_mutex() |
| 61 | { |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 62 | int e = __libcpp_recursive_mutex_destroy(&__m_); |
Howard Hinnant | 9ad56f3 | 2013-09-21 21:26:37 +0000 | [diff] [blame] | 63 | (void)e; |
Eric Fiselier | 59cd98b | 2017-02-04 23:22:28 +0000 | [diff] [blame] | 64 | _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
| 68 | recursive_mutex::lock() |
| 69 | { |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 70 | int ec = __libcpp_recursive_mutex_lock(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 71 | if (ec) |
| 72 | __throw_system_error(ec, "recursive_mutex lock failed"); |
| 73 | } |
| 74 | |
| 75 | void |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 76 | recursive_mutex::unlock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | { |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 78 | int e = __libcpp_recursive_mutex_unlock(&__m_); |
Howard Hinnant | 9ad56f3 | 2013-09-21 21:26:37 +0000 | [diff] [blame] | 79 | (void)e; |
Eric Fiselier | 59cd98b | 2017-02-04 23:22:28 +0000 | [diff] [blame] | 80 | _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 84 | recursive_mutex::try_lock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | { |
Eric Fiselier | 0ec1447 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 86 | return __libcpp_recursive_mutex_trylock(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // timed_mutex |
| 90 | |
| 91 | timed_mutex::timed_mutex() |
| 92 | : __locked_(false) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | timed_mutex::~timed_mutex() |
| 97 | { |
| 98 | lock_guard<mutex> _(__m_); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | timed_mutex::lock() |
| 103 | { |
| 104 | unique_lock<mutex> lk(__m_); |
| 105 | while (__locked_) |
| 106 | __cv_.wait(lk); |
| 107 | __locked_ = true; |
| 108 | } |
| 109 | |
| 110 | bool |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 111 | timed_mutex::try_lock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | { |
| 113 | unique_lock<mutex> lk(__m_, try_to_lock); |
| 114 | if (lk.owns_lock() && !__locked_) |
| 115 | { |
| 116 | __locked_ = true; |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | void |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 123 | timed_mutex::unlock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 124 | { |
| 125 | lock_guard<mutex> _(__m_); |
| 126 | __locked_ = false; |
| 127 | __cv_.notify_one(); |
| 128 | } |
| 129 | |
| 130 | // recursive_timed_mutex |
| 131 | |
| 132 | recursive_timed_mutex::recursive_timed_mutex() |
| 133 | : __count_(0), |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 134 | __id_(0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 135 | { |
| 136 | } |
| 137 | |
| 138 | recursive_timed_mutex::~recursive_timed_mutex() |
| 139 | { |
| 140 | lock_guard<mutex> _(__m_); |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | recursive_timed_mutex::lock() |
| 145 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 146 | __libcpp_thread_id id = __libcpp_thread_get_current_id(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 147 | unique_lock<mutex> lk(__m_); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 148 | if (__libcpp_thread_id_equal(id, __id_)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 149 | { |
| 150 | if (__count_ == numeric_limits<size_t>::max()) |
| 151 | __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached"); |
| 152 | ++__count_; |
| 153 | return; |
| 154 | } |
| 155 | while (__count_ != 0) |
| 156 | __cv_.wait(lk); |
| 157 | __count_ = 1; |
| 158 | __id_ = id; |
| 159 | } |
| 160 | |
| 161 | bool |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 162 | recursive_timed_mutex::try_lock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 163 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 164 | __libcpp_thread_id id = __libcpp_thread_get_current_id(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 165 | unique_lock<mutex> lk(__m_, try_to_lock); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 166 | if (lk.owns_lock() && (__count_ == 0 || __libcpp_thread_id_equal(id, __id_))) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 167 | { |
| 168 | if (__count_ == numeric_limits<size_t>::max()) |
| 169 | return false; |
| 170 | ++__count_; |
| 171 | __id_ = id; |
| 172 | return true; |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | void |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 178 | recursive_timed_mutex::unlock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 179 | { |
| 180 | unique_lock<mutex> lk(__m_); |
| 181 | if (--__count_ == 0) |
| 182 | { |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 183 | __id_ = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 184 | lk.unlock(); |
| 185 | __cv_.notify_one(); |
| 186 | } |
| 187 | } |
| 188 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 189 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 190 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 191 | // If dispatch_once_f ever handles C++ exceptions, and if one can get to it |
| 192 | // without illegal macros (unexpected macros not beginning with _UpperCase or |
| 193 | // __lowercase), and if it stops spinning waiting threads, then call_once should |
| 194 | // call into dispatch_once_f instead of here. Relevant radar this code needs to |
| 195 | // keep in sync with: 7741191. |
| 196 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 197 | #ifndef _LIBCPP_HAS_NO_THREADS |
Eric Fiselier | 637dd95 | 2016-09-28 22:08:13 +0000 | [diff] [blame] | 198 | _LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER; |
| 199 | _LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER; |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 200 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 201 | |
| 202 | void |
| 203 | __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*)) |
| 204 | { |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 205 | #if defined(_LIBCPP_HAS_NO_THREADS) |
| 206 | if (flag == 0) |
| 207 | { |
| 208 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 209 | try |
| 210 | { |
| 211 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 212 | flag = 1; |
| 213 | func(arg); |
| 214 | flag = ~0ul; |
| 215 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 216 | } |
| 217 | catch (...) |
| 218 | { |
| 219 | flag = 0ul; |
| 220 | throw; |
| 221 | } |
| 222 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 223 | } |
| 224 | #else // !_LIBCPP_HAS_NO_THREADS |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 225 | __libcpp_mutex_lock(&mut); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 226 | while (flag == 1) |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 227 | __libcpp_condvar_wait(&cv, &mut); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 228 | if (flag == 0) |
| 229 | { |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 230 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 231 | try |
| 232 | { |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 233 | #endif // _LIBCPP_NO_EXCEPTIONS |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 234 | __libcpp_relaxed_store(&flag, 1ul); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 235 | __libcpp_mutex_unlock(&mut); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 236 | func(arg); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 237 | __libcpp_mutex_lock(&mut); |
Kuba Brecka | de9d679 | 2016-09-04 09:55:12 +0000 | [diff] [blame] | 238 | __libcpp_atomic_store(&flag, ~0ul, _AO_Release); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 239 | __libcpp_mutex_unlock(&mut); |
| 240 | __libcpp_condvar_broadcast(&cv); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 241 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 242 | } |
| 243 | catch (...) |
| 244 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 245 | __libcpp_mutex_lock(&mut); |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 246 | __libcpp_relaxed_store(&flag, 0ul); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 247 | __libcpp_mutex_unlock(&mut); |
| 248 | __libcpp_condvar_broadcast(&cv); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | throw; |
| 250 | } |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 251 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 252 | } |
| 253 | else |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 254 | __libcpp_mutex_unlock(&mut); |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 255 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 256 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | _LIBCPP_END_NAMESPACE_STD |