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