Howard Hinnant | 4a88971 | 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 | 12315ed | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 10 | #include "__cxxabi_config.h" |
| 11 | |
Nick Kledzik | 49cbb02 | 2011-08-02 01:18:14 +0000 | [diff] [blame] | 12 | #include "abort_message.h" |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 13 | #include "config.h" |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 14 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 15 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 16 | # include <pthread.h> |
| 17 | #endif |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 18 | #include <stdint.h> |
Howard Hinnant | 5d6b9d2 | 2012-01-25 19:02:40 +0000 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | This implementation must be careful to not call code external to this file |
| 22 | which will turn around and try to call __cxa_guard_acquire reentrantly. |
| 23 | For this reason, the headers of this file are as restricted as possible. |
| 24 | Previous implementations of this code for __APPLE__ have used |
| 25 | pthread_mutex_lock and the abort_message utility without problem. This |
| 26 | implementation also uses pthread_cond_wait which has tested to not be a |
| 27 | problem. |
| 28 | */ |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 29 | |
| 30 | namespace __cxxabiv1 |
| 31 | { |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | |
Dan Albert | 3bd13ca | 2015-02-05 01:33:15 +0000 | [diff] [blame] | 36 | #ifdef __arm__ |
Nick Lewycky | 69e35a7 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 37 | // A 32-bit, 4-byte-aligned static data value. The least significant 2 bits must |
| 38 | // be statically initialized to 0. |
| 39 | typedef uint32_t guard_type; |
| 40 | |
Saleem Abdulrasool | 7c47005 | 2016-08-31 20:29:05 +0000 | [diff] [blame] | 41 | inline void set_initialized(guard_type* guard_object) { |
| 42 | *guard_object |= 1; |
| 43 | } |
| 44 | #else |
| 45 | typedef uint64_t guard_type; |
| 46 | |
| 47 | void set_initialized(guard_type* guard_object) { |
| 48 | char* initialized = (char*)guard_object; |
| 49 | *initialized = 1; |
| 50 | } |
| 51 | #endif |
| 52 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 53 | #if defined(_LIBCXXABI_HAS_NO_THREADS) || (defined(__APPLE__) && !defined(__arm__)) |
Saleem Abdulrasool | 7c47005 | 2016-08-31 20:29:05 +0000 | [diff] [blame] | 54 | #ifdef __arm__ |
| 55 | |
Nick Lewycky | 69e35a7 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 56 | // Test the lowest bit. |
| 57 | inline bool is_initialized(guard_type* guard_object) { |
| 58 | return (*guard_object) & 1; |
| 59 | } |
| 60 | |
Nick Lewycky | 69e35a7 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 61 | #else |
| 62 | |
Nick Lewycky | 69e35a7 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 63 | bool is_initialized(guard_type* guard_object) { |
| 64 | char* initialized = (char*)guard_object; |
| 65 | return *initialized; |
| 66 | } |
| 67 | |
Saleem Abdulrasool | 7c47005 | 2016-08-31 20:29:05 +0000 | [diff] [blame] | 68 | #endif |
Nick Lewycky | 69e35a7 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 71 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 72 | pthread_mutex_t guard_mut = PTHREAD_MUTEX_INITIALIZER; |
| 73 | pthread_cond_t guard_cv = PTHREAD_COND_INITIALIZER; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 74 | #endif |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 75 | |
Howard Hinnant | ffa2666 | 2012-03-14 19:39:50 +0000 | [diff] [blame] | 76 | #if defined(__APPLE__) && !defined(__arm__) |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 77 | |
| 78 | typedef uint32_t lock_type; |
| 79 | |
| 80 | #if __LITTLE_ENDIAN__ |
| 81 | |
| 82 | inline |
| 83 | lock_type |
| 84 | get_lock(uint64_t x) |
| 85 | { |
| 86 | return static_cast<lock_type>(x >> 32); |
| 87 | } |
| 88 | |
| 89 | inline |
| 90 | void |
| 91 | set_lock(uint64_t& x, lock_type y) |
| 92 | { |
| 93 | x = static_cast<uint64_t>(y) << 32; |
| 94 | } |
| 95 | |
| 96 | #else // __LITTLE_ENDIAN__ |
| 97 | |
| 98 | inline |
| 99 | lock_type |
| 100 | get_lock(uint64_t x) |
| 101 | { |
| 102 | return static_cast<lock_type>(x); |
| 103 | } |
| 104 | |
| 105 | inline |
| 106 | void |
| 107 | set_lock(uint64_t& x, lock_type y) |
| 108 | { |
| 109 | x = y; |
| 110 | } |
| 111 | |
| 112 | #endif // __LITTLE_ENDIAN__ |
| 113 | |
Howard Hinnant | baae2be | 2012-03-14 19:30:00 +0000 | [diff] [blame] | 114 | #else // !__APPLE__ || __arm__ |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 115 | |
| 116 | typedef bool lock_type; |
| 117 | |
| 118 | inline |
| 119 | lock_type |
| 120 | get_lock(uint64_t x) |
| 121 | { |
| 122 | union |
| 123 | { |
| 124 | uint64_t guard; |
| 125 | uint8_t lock[2]; |
| 126 | } f = {x}; |
| 127 | return f.lock[1] != 0; |
| 128 | } |
| 129 | |
| 130 | inline |
| 131 | void |
| 132 | set_lock(uint64_t& x, lock_type y) |
| 133 | { |
| 134 | union |
| 135 | { |
| 136 | uint64_t guard; |
| 137 | uint8_t lock[2]; |
| 138 | } f = {0}; |
| 139 | f.lock[1] = y; |
| 140 | x = f.guard; |
| 141 | } |
| 142 | |
Nick Lewycky | 69e35a7 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 143 | inline |
| 144 | lock_type |
| 145 | get_lock(uint32_t x) |
| 146 | { |
| 147 | union |
| 148 | { |
| 149 | uint32_t guard; |
| 150 | uint8_t lock[2]; |
| 151 | } f = {x}; |
| 152 | return f.lock[1] != 0; |
| 153 | } |
| 154 | |
| 155 | inline |
| 156 | void |
| 157 | set_lock(uint32_t& x, lock_type y) |
| 158 | { |
| 159 | union |
| 160 | { |
| 161 | uint32_t guard; |
| 162 | uint8_t lock[2]; |
| 163 | } f = {0}; |
| 164 | f.lock[1] = y; |
| 165 | x = f.guard; |
| 166 | } |
| 167 | |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 168 | #endif // __APPLE__ |
| 169 | |
| 170 | } // unnamed namespace |
| 171 | |
| 172 | extern "C" |
| 173 | { |
| 174 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 175 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Saleem Abdulrasool | 12315ed | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 176 | _LIBCXXABI_FUNC_VIS int __cxa_guard_acquire(guard_type *guard_object) { |
Jonathan Roelofs | bce1e4d | 2014-05-08 18:48:43 +0000 | [diff] [blame] | 177 | char* initialized = (char*)guard_object; |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 178 | if (pthread_mutex_lock(&guard_mut)) |
| 179 | abort_message("__cxa_guard_acquire failed to acquire mutex"); |
| 180 | int result = *initialized == 0; |
| 181 | if (result) |
| 182 | { |
Howard Hinnant | ffa2666 | 2012-03-14 19:39:50 +0000 | [diff] [blame] | 183 | #if defined(__APPLE__) && !defined(__arm__) |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 184 | const lock_type id = pthread_mach_thread_np(pthread_self()); |
| 185 | lock_type lock = get_lock(*guard_object); |
| 186 | if (lock) |
| 187 | { |
| 188 | // if this thread set lock for this same guard_object, abort |
| 189 | if (lock == id) |
| 190 | abort_message("__cxa_guard_acquire detected deadlock"); |
| 191 | do |
| 192 | { |
| 193 | if (pthread_cond_wait(&guard_cv, &guard_mut)) |
| 194 | abort_message("__cxa_guard_acquire condition variable wait failed"); |
| 195 | lock = get_lock(*guard_object); |
| 196 | } while (lock); |
Nick Lewycky | 69e35a7 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 197 | result = !is_initialized(guard_object); |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 198 | if (result) |
| 199 | set_lock(*guard_object, id); |
| 200 | } |
| 201 | else |
| 202 | set_lock(*guard_object, id); |
Howard Hinnant | baae2be | 2012-03-14 19:30:00 +0000 | [diff] [blame] | 203 | #else // !__APPLE__ || __arm__ |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 204 | while (get_lock(*guard_object)) |
| 205 | if (pthread_cond_wait(&guard_cv, &guard_mut)) |
| 206 | abort_message("__cxa_guard_acquire condition variable wait failed"); |
| 207 | result = *initialized == 0; |
| 208 | if (result) |
| 209 | set_lock(*guard_object, true); |
Howard Hinnant | baae2be | 2012-03-14 19:30:00 +0000 | [diff] [blame] | 210 | #endif // !__APPLE__ || __arm__ |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 211 | } |
| 212 | if (pthread_mutex_unlock(&guard_mut)) |
| 213 | abort_message("__cxa_guard_acquire failed to release mutex"); |
| 214 | return result; |
| 215 | } |
| 216 | |
Saleem Abdulrasool | 12315ed | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 217 | _LIBCXXABI_FUNC_VIS void __cxa_guard_release(guard_type *guard_object) { |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 218 | if (pthread_mutex_lock(&guard_mut)) |
| 219 | abort_message("__cxa_guard_release failed to acquire mutex"); |
| 220 | *guard_object = 0; |
Nick Lewycky | 69e35a7 | 2011-06-07 18:46:10 +0000 | [diff] [blame] | 221 | set_initialized(guard_object); |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 222 | if (pthread_mutex_unlock(&guard_mut)) |
| 223 | abort_message("__cxa_guard_release failed to release mutex"); |
| 224 | if (pthread_cond_broadcast(&guard_cv)) |
| 225 | abort_message("__cxa_guard_release failed to broadcast condition variable"); |
| 226 | } |
| 227 | |
Saleem Abdulrasool | 12315ed | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 228 | _LIBCXXABI_FUNC_VIS void __cxa_guard_abort(guard_type *guard_object) { |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 229 | if (pthread_mutex_lock(&guard_mut)) |
| 230 | abort_message("__cxa_guard_abort failed to acquire mutex"); |
| 231 | *guard_object = 0; |
| 232 | if (pthread_mutex_unlock(&guard_mut)) |
| 233 | abort_message("__cxa_guard_abort failed to release mutex"); |
| 234 | if (pthread_cond_broadcast(&guard_cv)) |
| 235 | abort_message("__cxa_guard_abort failed to broadcast condition variable"); |
| 236 | } |
| 237 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 238 | #else // _LIBCXXABI_HAS_NO_THREADS |
| 239 | |
| 240 | _LIBCXXABI_FUNC_VIS int __cxa_guard_acquire(guard_type *guard_object) { |
| 241 | return !is_initialized(guard_object); |
| 242 | } |
| 243 | |
| 244 | _LIBCXXABI_FUNC_VIS void __cxa_guard_release(guard_type *guard_object) { |
| 245 | *guard_object = 0; |
| 246 | set_initialized(guard_object); |
| 247 | } |
| 248 | |
| 249 | _LIBCXXABI_FUNC_VIS void __cxa_guard_abort(guard_type *guard_object) { |
| 250 | *guard_object = 0; |
| 251 | } |
| 252 | |
| 253 | #endif // !_LIBCXXABI_HAS_NO_THREADS |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 254 | |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 +0000 | [diff] [blame] | 255 | } // extern "C" |
| 256 | |
| 257 | } // __cxxabiv1 |