Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------ memory.cpp ----------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 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 | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Howard Hinnant | 46e9493 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 10 | #define _LIBCPP_BUILDING_MEMORY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | #include "memory" |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 12 | #ifndef _LIBCPP_HAS_NO_THREADS |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 13 | #include "mutex" |
Howard Hinnant | 65f059b | 2012-07-30 17:13:21 +0000 | [diff] [blame] | 14 | #include "thread" |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 15 | #endif |
Eric Fiselier | 3b1fb53 | 2015-08-18 21:08:54 +0000 | [diff] [blame^] | 16 | #include "include/atomic_support.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 17 | |
| 18 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 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 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | template <class T> |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 27 | inline T |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 28 | increment(T& t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | { |
Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 30 | return __libcpp_atomic_add(&t, 1, _AO_Relaxed); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | template <class T> |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 34 | inline T |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 35 | decrement(T& t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 36 | { |
Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 37 | return __libcpp_atomic_add(&t, -1, _AO_Acq_Rel); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | } // namespace |
| 41 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | const allocator_arg_t allocator_arg = allocator_arg_t(); |
| 43 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 44 | bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | |
| 46 | const char* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 47 | bad_weak_ptr::what() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | { |
| 49 | return "bad_weak_ptr"; |
| 50 | } |
| 51 | |
| 52 | __shared_count::~__shared_count() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 57 | __shared_count::__add_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 58 | { |
| 59 | increment(__shared_owners_); |
| 60 | } |
| 61 | |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 62 | bool |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 63 | __shared_count::__release_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 64 | { |
| 65 | if (decrement(__shared_owners_) == -1) |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 66 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 67 | __on_zero_shared(); |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | return false; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | __shared_weak_count::~__shared_weak_count() |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 78 | __shared_weak_count::__add_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 79 | { |
| 80 | __shared_count::__add_shared(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 84 | __shared_weak_count::__add_weak() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | { |
| 86 | increment(__shared_weak_owners_); |
| 87 | } |
| 88 | |
| 89 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 90 | __shared_weak_count::__release_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | { |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 92 | if (__shared_count::__release_shared()) |
| 93 | __release_weak(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 97 | __shared_weak_count::__release_weak() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 98 | { |
| 99 | if (decrement(__shared_weak_owners_) == -1) |
| 100 | __on_zero_shared_weak(); |
| 101 | } |
| 102 | |
| 103 | __shared_weak_count* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 104 | __shared_weak_count::lock() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 105 | { |
Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 106 | long object_owners = __libcpp_atomic_load(&__shared_owners_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | while (object_owners != -1) |
| 108 | { |
Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 109 | if (__libcpp_atomic_compare_exchange(&__shared_owners_, |
| 110 | &object_owners, |
| 111 | object_owners+1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | return this; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
Eric Fiselier | 01f6a14 | 2014-12-12 02:36:23 +0000 | [diff] [blame] | 117 | #if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC) |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 118 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | const void* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 120 | __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 121 | { |
| 122 | return 0; |
| 123 | } |
| 124 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 125 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 126 | |
Jonathan Roelofs | baed05d | 2014-09-05 20:28:44 +0000 | [diff] [blame] | 127 | #if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS) |
Howard Hinnant | 96c60b4 | 2012-08-19 15:13:16 +0000 | [diff] [blame] | 128 | |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 129 | static const std::size_t __sp_mut_count = 16; |
Howard Hinnant | e33c2d1 | 2013-03-16 00:17:53 +0000 | [diff] [blame] | 130 | static pthread_mutex_t mut_back_imp[__sp_mut_count] = |
| 131 | { |
| 132 | PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, |
| 133 | PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, |
| 134 | PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, |
| 135 | PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER |
| 136 | }; |
| 137 | |
| 138 | static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp); |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 139 | |
| 140 | _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 141 | : __lx(p) |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 142 | { |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | __sp_mut::lock() _NOEXCEPT |
| 147 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 148 | mutex& m = *static_cast<mutex*>(__lx); |
Howard Hinnant | 65f059b | 2012-07-30 17:13:21 +0000 | [diff] [blame] | 149 | unsigned count = 0; |
| 150 | while (!m.try_lock()) |
| 151 | { |
| 152 | if (++count > 16) |
| 153 | { |
| 154 | m.lock(); |
| 155 | break; |
| 156 | } |
| 157 | this_thread::yield(); |
| 158 | } |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void |
| 162 | __sp_mut::unlock() _NOEXCEPT |
| 163 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 164 | static_cast<mutex*>(__lx)->unlock(); |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | __sp_mut& |
| 168 | __get_sp_mut(const void* p) |
| 169 | { |
| 170 | static __sp_mut muts[__sp_mut_count] |
| 171 | { |
| 172 | &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3], |
| 173 | &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7], |
| 174 | &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11], |
| 175 | &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15] |
| 176 | }; |
| 177 | return muts[hash<const void*>()(p) & (__sp_mut_count-1)]; |
| 178 | } |
| 179 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 180 | #endif // __has_feature(cxx_atomic) && !_LIBCPP_HAS_NO_THREADS |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 181 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 182 | void |
| 183 | declare_reachable(void*) |
| 184 | { |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | declare_no_pointers(char*, size_t) |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | undeclare_no_pointers(char*, size_t) |
| 194 | { |
| 195 | } |
| 196 | |
| 197 | pointer_safety |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 198 | get_pointer_safety() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 199 | { |
| 200 | return pointer_safety::relaxed; |
| 201 | } |
| 202 | |
| 203 | void* |
| 204 | __undeclare_reachable(void* p) |
| 205 | { |
| 206 | return p; |
| 207 | } |
| 208 | |
| 209 | void* |
| 210 | align(size_t alignment, size_t size, void*& ptr, size_t& space) |
| 211 | { |
| 212 | void* r = nullptr; |
| 213 | if (size <= space) |
| 214 | { |
| 215 | char* p1 = static_cast<char*>(ptr); |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 216 | char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment); |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 217 | size_t d = static_cast<size_t>(p2 - p1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 218 | if (d <= space - size) |
| 219 | { |
| 220 | r = p2; |
| 221 | ptr = r; |
| 222 | space -= d; |
| 223 | } |
| 224 | } |
| 225 | return r; |
| 226 | } |
| 227 | |
| 228 | _LIBCPP_END_NAMESPACE_STD |