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