1 //===------------------------ memory.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 10 #define _LIBCPP_BUILDING_MEMORY 11 #include "memory" 12 #ifndef _LIBCPP_HAS_NO_THREADS 13 #include "mutex" 14 #include "thread" 15 #endif 16 #include "include/atomic_support.h" 17 18 _LIBCPP_BEGIN_NAMESPACE_STD 19 20 namespace 21 { 22 23 // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively) 24 // should be sufficient for thread safety. 25 // See https://llvm.org/bugs/show_bug.cgi?id=22803 26 template <class T> 27 inline T 28 increment(T& t) _NOEXCEPT 29 { 30 return __libcpp_atomic_add(&t, 1, _AO_Relaxed); 31 } 32 33 template <class T> 34 inline T 35 decrement(T& t) _NOEXCEPT 36 { 37 return __libcpp_atomic_add(&t, -1, _AO_Acq_Rel); 38 } 39 40 } // namespace 41 42 const allocator_arg_t allocator_arg = allocator_arg_t(); 43 44 bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {} 45 46 const char* 47 bad_weak_ptr::what() const _NOEXCEPT 48 { 49 return "bad_weak_ptr"; 50 } 51 52 __shared_count::~__shared_count() 53 { 54 } 55 56 void 57 __shared_count::__add_shared() _NOEXCEPT 58 { 59 increment(__shared_owners_); 60 } 61 62 bool 63 __shared_count::__release_shared() _NOEXCEPT 64 { 65 if (decrement(__shared_owners_) == -1) 66 { 67 __on_zero_shared(); 68 return true; 69 } 70 return false; 71 } 72 73 __shared_weak_count::~__shared_weak_count() 74 { 75 } 76 77 void 78 __shared_weak_count::__add_shared() _NOEXCEPT 79 { 80 __shared_count::__add_shared(); 81 } 82 83 void 84 __shared_weak_count::__add_weak() _NOEXCEPT 85 { 86 increment(__shared_weak_owners_); 87 } 88 89 void 90 __shared_weak_count::__release_shared() _NOEXCEPT 91 { 92 if (__shared_count::__release_shared()) 93 __release_weak(); 94 } 95 96 void 97 __shared_weak_count::__release_weak() _NOEXCEPT 98 { 99 // NOTE: The acquire load here is an optimization of the very 100 // common case where a shared pointer is being destructed while 101 // having no other contended references. 102 // 103 // BENEFIT: We avoid expensive atomic stores like XADD and STREX 104 // in a common case. Those instructions are slow and do nasty 105 // things to caches. 106 // 107 // IS THIS SAFE? Yes. During weak destruction, if we see that we 108 // are the last reference, we know that no-one else is accessing 109 // us. If someone were accessing us, then they would be doing so 110 // while the last shared / weak_ptr was being destructed, and 111 // that's undefined anyway. 112 // 113 // If we see anything other than a 0, then we have possible 114 // contention, and need to use an atomicrmw primitive. 115 // The same arguments don't apply for increment, where it is legal 116 // (though inadvisable) to share shared_ptr references between 117 // threads, and have them all get copied at once. The argument 118 // also doesn't apply for __release_shared, because an outstanding 119 // weak_ptr::lock() could read / modify the shared count. 120 if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0) 121 { 122 // no need to do this store, because we are about 123 // to destroy everything. 124 //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release); 125 __on_zero_shared_weak(); 126 } 127 else if (decrement(__shared_weak_owners_) == -1) 128 __on_zero_shared_weak(); 129 } 130 131 __shared_weak_count* 132 __shared_weak_count::lock() _NOEXCEPT 133 { 134 long object_owners = __libcpp_atomic_load(&__shared_owners_); 135 while (object_owners != -1) 136 { 137 if (__libcpp_atomic_compare_exchange(&__shared_owners_, 138 &object_owners, 139 object_owners+1)) 140 return this; 141 } 142 return 0; 143 } 144 145 #if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC) 146 147 const void* 148 __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT 149 { 150 return 0; 151 } 152 153 #endif // _LIBCPP_NO_RTTI 154 155 #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) 156 157 _LIBCPP_SAFE_STATIC static const std::size_t __sp_mut_count = 16; 158 _LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut_back[__sp_mut_count] = 159 { 160 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, 161 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, 162 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, 163 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER 164 }; 165 166 _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT 167 : __lx(p) 168 { 169 } 170 171 void 172 __sp_mut::lock() _NOEXCEPT 173 { 174 auto m = static_cast<__libcpp_mutex_t*>(__lx); 175 unsigned count = 0; 176 while (__libcpp_mutex_trylock(m) != 0) 177 { 178 if (++count > 16) 179 { 180 __libcpp_mutex_lock(m); 181 break; 182 } 183 this_thread::yield(); 184 } 185 } 186 187 void 188 __sp_mut::unlock() _NOEXCEPT 189 { 190 __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx)); 191 } 192 193 __sp_mut& 194 __get_sp_mut(const void* p) 195 { 196 static __sp_mut muts[__sp_mut_count] 197 { 198 &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3], 199 &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7], 200 &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11], 201 &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15] 202 }; 203 return muts[hash<const void*>()(p) & (__sp_mut_count-1)]; 204 } 205 206 #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) 207 208 void 209 declare_reachable(void*) 210 { 211 } 212 213 void 214 declare_no_pointers(char*, size_t) 215 { 216 } 217 218 void 219 undeclare_no_pointers(char*, size_t) 220 { 221 } 222 223 #if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) 224 pointer_safety get_pointer_safety() _NOEXCEPT 225 { 226 return pointer_safety::relaxed; 227 } 228 #endif 229 230 void* 231 __undeclare_reachable(void* p) 232 { 233 return p; 234 } 235 236 void* 237 align(size_t alignment, size_t size, void*& ptr, size_t& space) 238 { 239 void* r = nullptr; 240 if (size <= space) 241 { 242 char* p1 = static_cast<char*>(ptr); 243 char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment); 244 size_t d = static_cast<size_t>(p2 - p1); 245 if (d <= space - size) 246 { 247 r = p2; 248 ptr = r; 249 space -= d; 250 } 251 } 252 return r; 253 } 254 255 _LIBCPP_END_NAMESPACE_STD 256