Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 1 | //===---------------------------- cxa_guard.cpp ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Saleem Abdulrasool | b4ec579 | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 10 | #include "__cxxabi_config.h" |
| 11 | |
Nick Kledzik | 4168311 | 2011-08-02 01:18:14 +0000 | [diff] [blame] | 12 | #include "abort_message.h" |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 13 | #include <__threading_support> |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 14 | |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 15 | #include <stdint.h> |
Howard Hinnant | de34b3e | 2012-01-25 19:02:40 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | This implementation must be careful to not call code external to this file |
| 19 | which will turn around and try to call __cxa_guard_acquire reentrantly. |
| 20 | For this reason, the headers of this file are as restricted as possible. |
| 21 | Previous implementations of this code for __APPLE__ have used |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 22 | std::__libcpp_mutex_lock and the abort_message utility without problem. This |
| 23 | implementation also uses std::__libcpp_condvar_wait which has tested |
Asiri Rathnayake | 5180673 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 24 | to not be a problem. |
Howard Hinnant | de34b3e | 2012-01-25 19:02:40 +0000 | [diff] [blame] | 25 | */ |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 26 | |
| 27 | namespace __cxxabiv1 |
| 28 | { |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | |
Dan Albert | a1fce46 | 2015-02-05 01:33:15 +0000 | [diff] [blame] | 33 | #ifdef __arm__ |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 34 | // A 32-bit, 4-byte-aligned static data value. The least significant 2 bits must |
| 35 | // be statically initialized to 0. |
| 36 | typedef uint32_t guard_type; |
| 37 | |
Saleem Abdulrasool | 21a75e7 | 2016-08-31 20:29:05 +0000 | [diff] [blame] | 38 | inline void set_initialized(guard_type* guard_object) { |
| 39 | *guard_object |= 1; |
| 40 | } |
| 41 | #else |
| 42 | typedef uint64_t guard_type; |
| 43 | |
| 44 | void set_initialized(guard_type* guard_object) { |
| 45 | char* initialized = (char*)guard_object; |
| 46 | *initialized = 1; |
| 47 | } |
| 48 | #endif |
| 49 | |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 50 | #if defined(_LIBCXXABI_HAS_NO_THREADS) || (defined(__APPLE__) && !defined(__arm__)) |
Saleem Abdulrasool | 21a75e7 | 2016-08-31 20:29:05 +0000 | [diff] [blame] | 51 | #ifdef __arm__ |
| 52 | |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 53 | // Test the lowest bit. |
| 54 | inline bool is_initialized(guard_type* guard_object) { |
| 55 | return (*guard_object) & 1; |
| 56 | } |
| 57 | |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 58 | #else |
| 59 | |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 60 | bool is_initialized(guard_type* guard_object) { |
| 61 | char* initialized = (char*)guard_object; |
| 62 | return *initialized; |
| 63 | } |
| 64 | |
Saleem Abdulrasool | 21a75e7 | 2016-08-31 20:29:05 +0000 | [diff] [blame] | 65 | #endif |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 68 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 69 | std::__libcpp_mutex_t guard_mut = _LIBCPP_MUTEX_INITIALIZER; |
| 70 | std::__libcpp_condvar_t guard_cv = _LIBCPP_CONDVAR_INITIALIZER; |
Jonathan Roelofs | c285efa | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 71 | #endif |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 72 | |
Howard Hinnant | 4d590c3 | 2012-03-14 19:39:50 +0000 | [diff] [blame] | 73 | #if defined(__APPLE__) && !defined(__arm__) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 74 | |
| 75 | typedef uint32_t lock_type; |
| 76 | |
| 77 | #if __LITTLE_ENDIAN__ |
| 78 | |
| 79 | inline |
| 80 | lock_type |
| 81 | get_lock(uint64_t x) |
| 82 | { |
| 83 | return static_cast<lock_type>(x >> 32); |
| 84 | } |
| 85 | |
| 86 | inline |
| 87 | void |
| 88 | set_lock(uint64_t& x, lock_type y) |
| 89 | { |
| 90 | x = static_cast<uint64_t>(y) << 32; |
| 91 | } |
| 92 | |
| 93 | #else // __LITTLE_ENDIAN__ |
| 94 | |
| 95 | inline |
| 96 | lock_type |
| 97 | get_lock(uint64_t x) |
| 98 | { |
| 99 | return static_cast<lock_type>(x); |
| 100 | } |
| 101 | |
| 102 | inline |
| 103 | void |
| 104 | set_lock(uint64_t& x, lock_type y) |
| 105 | { |
| 106 | x = y; |
| 107 | } |
| 108 | |
| 109 | #endif // __LITTLE_ENDIAN__ |
| 110 | |
Howard Hinnant | fcd21a1 | 2012-03-14 19:30:00 +0000 | [diff] [blame] | 111 | #else // !__APPLE__ || __arm__ |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 112 | |
| 113 | typedef bool lock_type; |
| 114 | |
Eric Fiselier | 9e9cb7b | 2017-03-04 01:02:35 +0000 | [diff] [blame] | 115 | #if !defined(__arm__) |
| 116 | static_assert(std::is_same<guard_type, uint64_t>::value, ""); |
| 117 | |
| 118 | inline lock_type get_lock(uint64_t x) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 119 | { |
| 120 | union |
| 121 | { |
| 122 | uint64_t guard; |
| 123 | uint8_t lock[2]; |
| 124 | } f = {x}; |
| 125 | return f.lock[1] != 0; |
| 126 | } |
| 127 | |
Eric Fiselier | 9e9cb7b | 2017-03-04 01:02:35 +0000 | [diff] [blame] | 128 | inline void set_lock(uint64_t& x, lock_type y) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 129 | { |
| 130 | union |
| 131 | { |
| 132 | uint64_t guard; |
| 133 | uint8_t lock[2]; |
| 134 | } f = {0}; |
| 135 | f.lock[1] = y; |
| 136 | x = f.guard; |
| 137 | } |
Eric Fiselier | 9e9cb7b | 2017-03-04 01:02:35 +0000 | [diff] [blame] | 138 | #else // defined(__arm__) |
| 139 | static_assert(std::is_same<guard_type, uint32_t>::value, ""); |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 140 | |
Eric Fiselier | 9e9cb7b | 2017-03-04 01:02:35 +0000 | [diff] [blame] | 141 | inline lock_type get_lock(uint32_t x) |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 142 | { |
| 143 | union |
| 144 | { |
| 145 | uint32_t guard; |
| 146 | uint8_t lock[2]; |
| 147 | } f = {x}; |
| 148 | return f.lock[1] != 0; |
| 149 | } |
| 150 | |
Eric Fiselier | 9e9cb7b | 2017-03-04 01:02:35 +0000 | [diff] [blame] | 151 | inline void set_lock(uint32_t& x, lock_type y) |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 152 | { |
| 153 | union |
| 154 | { |
| 155 | uint32_t guard; |
| 156 | uint8_t lock[2]; |
| 157 | } f = {0}; |
| 158 | f.lock[1] = y; |
| 159 | x = f.guard; |
| 160 | } |
| 161 | |
Eric Fiselier | 9e9cb7b | 2017-03-04 01:02:35 +0000 | [diff] [blame] | 162 | #endif // !defined(__arm__) |
| 163 | |
| 164 | #endif // __APPLE__ && !__arm__ |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 165 | |
| 166 | } // unnamed namespace |
| 167 | |
| 168 | extern "C" |
| 169 | { |
| 170 | |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 171 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Saleem Abdulrasool | b4ec579 | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 172 | _LIBCXXABI_FUNC_VIS int __cxa_guard_acquire(guard_type *guard_object) { |
Jonathan Roelofs | c8b4d8d | 2014-05-08 18:48:43 +0000 | [diff] [blame] | 173 | char* initialized = (char*)guard_object; |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 174 | if (std::__libcpp_mutex_lock(&guard_mut)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 175 | abort_message("__cxa_guard_acquire failed to acquire mutex"); |
| 176 | int result = *initialized == 0; |
| 177 | if (result) |
| 178 | { |
Howard Hinnant | 4d590c3 | 2012-03-14 19:39:50 +0000 | [diff] [blame] | 179 | #if defined(__APPLE__) && !defined(__arm__) |
Asiri Rathnayake | bbc3824 | 2017-01-26 10:38:03 +0000 | [diff] [blame] | 180 | // This is a special-case pthread dependency for Mac. We can't pull this |
| 181 | // out into libcxx's threading API (__threading_support) because not all |
| 182 | // supported Mac environments provide this function (in pthread.h). To |
| 183 | // make it possible to build/use libcxx in those environments, we have to |
| 184 | // keep this pthread dependency local to libcxxabi. If there is some |
| 185 | // convenient way to detect precisely when pthread_mach_thread_np is |
| 186 | // available in a given Mac environment, it might still be possible to |
| 187 | // bury this dependency in __threading_support. |
| 188 | #ifdef _LIBCPP_HAS_THREAD_API_PTHREAD |
| 189 | const lock_type id = pthread_mach_thread_np(std::__libcpp_thread_get_current_id()); |
| 190 | #else |
| 191 | #error "How do I pthread_mach_thread_np()?" |
| 192 | #endif |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 193 | lock_type lock = get_lock(*guard_object); |
| 194 | if (lock) |
| 195 | { |
| 196 | // if this thread set lock for this same guard_object, abort |
| 197 | if (lock == id) |
| 198 | abort_message("__cxa_guard_acquire detected deadlock"); |
| 199 | do |
| 200 | { |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 201 | if (std::__libcpp_condvar_wait(&guard_cv, &guard_mut)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 202 | abort_message("__cxa_guard_acquire condition variable wait failed"); |
| 203 | lock = get_lock(*guard_object); |
| 204 | } while (lock); |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 205 | result = !is_initialized(guard_object); |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 206 | if (result) |
| 207 | set_lock(*guard_object, id); |
| 208 | } |
| 209 | else |
| 210 | set_lock(*guard_object, id); |
Howard Hinnant | fcd21a1 | 2012-03-14 19:30:00 +0000 | [diff] [blame] | 211 | #else // !__APPLE__ || __arm__ |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 212 | while (get_lock(*guard_object)) |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 213 | if (std::__libcpp_condvar_wait(&guard_cv, &guard_mut)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 214 | abort_message("__cxa_guard_acquire condition variable wait failed"); |
| 215 | result = *initialized == 0; |
| 216 | if (result) |
| 217 | set_lock(*guard_object, true); |
Howard Hinnant | fcd21a1 | 2012-03-14 19:30:00 +0000 | [diff] [blame] | 218 | #endif // !__APPLE__ || __arm__ |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 219 | } |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 220 | if (std::__libcpp_mutex_unlock(&guard_mut)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 221 | abort_message("__cxa_guard_acquire failed to release mutex"); |
| 222 | return result; |
| 223 | } |
| 224 | |
Saleem Abdulrasool | b4ec579 | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 225 | _LIBCXXABI_FUNC_VIS void __cxa_guard_release(guard_type *guard_object) { |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 226 | if (std::__libcpp_mutex_lock(&guard_mut)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 227 | abort_message("__cxa_guard_release failed to acquire mutex"); |
| 228 | *guard_object = 0; |
Nick Lewycky | d8cfd65 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 229 | set_initialized(guard_object); |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 230 | if (std::__libcpp_mutex_unlock(&guard_mut)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 231 | abort_message("__cxa_guard_release failed to release mutex"); |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 232 | if (std::__libcpp_condvar_broadcast(&guard_cv)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 233 | abort_message("__cxa_guard_release failed to broadcast condition variable"); |
| 234 | } |
| 235 | |
Saleem Abdulrasool | b4ec579 | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 236 | _LIBCXXABI_FUNC_VIS void __cxa_guard_abort(guard_type *guard_object) { |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 237 | if (std::__libcpp_mutex_lock(&guard_mut)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 238 | abort_message("__cxa_guard_abort failed to acquire mutex"); |
| 239 | *guard_object = 0; |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 240 | if (std::__libcpp_mutex_unlock(&guard_mut)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 241 | abort_message("__cxa_guard_abort failed to release mutex"); |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 242 | if (std::__libcpp_condvar_broadcast(&guard_cv)) |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 243 | abort_message("__cxa_guard_abort failed to broadcast condition variable"); |
| 244 | } |
| 245 | |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 246 | #else // _LIBCXXABI_HAS_NO_THREADS |
| 247 | |
| 248 | _LIBCXXABI_FUNC_VIS int __cxa_guard_acquire(guard_type *guard_object) { |
| 249 | return !is_initialized(guard_object); |
| 250 | } |
| 251 | |
| 252 | _LIBCXXABI_FUNC_VIS void __cxa_guard_release(guard_type *guard_object) { |
| 253 | *guard_object = 0; |
| 254 | set_initialized(guard_object); |
| 255 | } |
| 256 | |
| 257 | _LIBCXXABI_FUNC_VIS void __cxa_guard_abort(guard_type *guard_object) { |
| 258 | *guard_object = 0; |
| 259 | } |
| 260 | |
| 261 | #endif // !_LIBCXXABI_HAS_NO_THREADS |
Jonathan Roelofs | c285efa | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 262 | |
Howard Hinnant | 9282718 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 263 | } // extern "C" |
| 264 | |
| 265 | } // __cxxabiv1 |