Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Louis Dionne | 0cc34ca | 2022-04-11 16:32:40 | [diff] [blame] | 9 | #include <__config> |
| 10 | #ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 11 | # define _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS |
Louis Dionne | 0cc34ca | 2022-04-11 16:32:40 | [diff] [blame] | 12 | #endif |
| 13 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Nikolas Klauser | c6f3b7b | 2024-11-06 09:39:19 | [diff] [blame^] | 16 | #if _LIBCPP_HAS_THREADS |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 17 | # include <mutex> |
| 18 | # include <thread> |
| 19 | # if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB) |
| 20 | # pragma comment(lib, "pthread") |
| 21 | # endif |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 22 | #endif |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 23 | |
Eric Fiselier | e8fd164 | 2015-08-18 21:08:54 | [diff] [blame] | 24 | #include "include/atomic_support.h" |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 25 | |
| 26 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 27 | |
Louis Dionne | 5601305 | 2021-03-01 17:09:45 | [diff] [blame] | 28 | bad_weak_ptr::~bad_weak_ptr() noexcept {} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 29 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 30 | const char* bad_weak_ptr::what() const noexcept { return "bad_weak_ptr"; } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 31 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 32 | __shared_count::~__shared_count() {} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 33 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 34 | __shared_weak_count::~__shared_weak_count() {} |
Kevin Hu | f08de52 | 2017-01-17 02:46:33 | [diff] [blame] | 35 | |
Louis Dionne | 0cc34ca | 2022-04-11 16:32:40 | [diff] [blame] | 36 | #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS) |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 37 | void __shared_count::__add_shared() noexcept { __libcpp_atomic_refcount_increment(__shared_owners_); } |
| 38 | |
| 39 | bool __shared_count::__release_shared() noexcept { |
| 40 | if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) { |
| 41 | __on_zero_shared(); |
| 42 | return true; |
| 43 | } |
| 44 | return false; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 45 | } |
| 46 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 47 | void __shared_weak_count::__add_shared() noexcept { __shared_count::__add_shared(); } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 48 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 49 | void __shared_weak_count::__add_weak() noexcept { __libcpp_atomic_refcount_increment(__shared_weak_owners_); } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 50 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 51 | void __shared_weak_count::__release_shared() noexcept { |
| 52 | if (__shared_count::__release_shared()) |
| 53 | __release_weak(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 54 | } |
Louis Dionne | 0cc34ca | 2022-04-11 16:32:40 | [diff] [blame] | 55 | #endif // _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS |
Eric Fiselier | 11f6045 | 2017-01-17 03:16:26 | [diff] [blame] | 56 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 57 | void __shared_weak_count::__release_weak() noexcept { |
| 58 | // NOTE: The acquire load here is an optimization of the very |
| 59 | // common case where a shared pointer is being destructed while |
| 60 | // having no other contended references. |
| 61 | // |
| 62 | // BENEFIT: We avoid expensive atomic stores like XADD and STREX |
| 63 | // in a common case. Those instructions are slow and do nasty |
| 64 | // things to caches. |
| 65 | // |
| 66 | // IS THIS SAFE? Yes. During weak destruction, if we see that we |
| 67 | // are the last reference, we know that no-one else is accessing |
| 68 | // us. If someone were accessing us, then they would be doing so |
| 69 | // while the last shared / weak_ptr was being destructed, and |
| 70 | // that's undefined anyway. |
| 71 | // |
| 72 | // If we see anything other than a 0, then we have possible |
| 73 | // contention, and need to use an atomicrmw primitive. |
| 74 | // The same arguments don't apply for increment, where it is legal |
| 75 | // (though inadvisable) to share shared_ptr references between |
| 76 | // threads, and have them all get copied at once. The argument |
| 77 | // also doesn't apply for __release_shared, because an outstanding |
| 78 | // weak_ptr::lock() could read / modify the shared count. |
| 79 | if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0) { |
| 80 | // no need to do this store, because we are about |
| 81 | // to destroy everything. |
| 82 | //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release); |
| 83 | __on_zero_shared_weak(); |
| 84 | } else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1) |
| 85 | __on_zero_shared_weak(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 86 | } |
| 87 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 88 | __shared_weak_count* __shared_weak_count::lock() noexcept { |
| 89 | long object_owners = __libcpp_atomic_load(&__shared_owners_); |
| 90 | while (object_owners != -1) { |
| 91 | if (__libcpp_atomic_compare_exchange(&__shared_owners_, &object_owners, object_owners + 1)) |
| 92 | return this; |
| 93 | } |
| 94 | return nullptr; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 95 | } |
| 96 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 97 | const void* __shared_weak_count::__get_deleter(const type_info&) const noexcept { return nullptr; } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 98 | |
Nikolas Klauser | c6f3b7b | 2024-11-06 09:39:19 | [diff] [blame^] | 99 | #if _LIBCPP_HAS_THREADS |
Howard Hinnant | e4b2a74 | 2012-08-19 15:13:16 | [diff] [blame] | 100 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 101 | static constexpr std::size_t __sp_mut_count = 32; |
| 102 | static constinit __libcpp_mutex_t mut_back[__sp_mut_count] = { |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 103 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
| 104 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
| 105 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
Louis Dionne | 5b386ac9 | 2022-05-26 21:42:06 | [diff] [blame] | 106 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
| 107 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
| 108 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
| 109 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 110 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER}; |
Howard Hinnant | 5efca64 | 2013-03-16 00:17:53 | [diff] [blame] | 111 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 112 | constexpr __sp_mut::__sp_mut(void* p) noexcept : __lx_(p) {} |
| 113 | |
| 114 | void __sp_mut::lock() noexcept { |
| 115 | auto m = static_cast<__libcpp_mutex_t*>(__lx_); |
| 116 | __libcpp_mutex_lock(m); |
Howard Hinnant | d77851e | 2012-07-30 01:40:57 | [diff] [blame] | 117 | } |
| 118 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 119 | void __sp_mut::unlock() noexcept { __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx_)); } |
Howard Hinnant | d77851e | 2012-07-30 01:40:57 | [diff] [blame] | 120 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 121 | __sp_mut& __get_sp_mut(const void* p) { |
| 122 | static constinit __sp_mut muts[__sp_mut_count] = { |
| 123 | &mut_back[0], &mut_back[1], &mut_back[2], &mut_back[3], &mut_back[4], &mut_back[5], &mut_back[6], |
| 124 | &mut_back[7], &mut_back[8], &mut_back[9], &mut_back[10], &mut_back[11], &mut_back[12], &mut_back[13], |
| 125 | &mut_back[14], &mut_back[15], &mut_back[16], &mut_back[17], &mut_back[18], &mut_back[19], &mut_back[20], |
| 126 | &mut_back[21], &mut_back[22], &mut_back[23], &mut_back[24], &mut_back[25], &mut_back[26], &mut_back[27], |
| 127 | &mut_back[28], &mut_back[29], &mut_back[30], &mut_back[31]}; |
| 128 | return muts[hash<const void*>()(p) & (__sp_mut_count - 1)]; |
Howard Hinnant | d77851e | 2012-07-30 01:40:57 | [diff] [blame] | 129 | } |
| 130 | |
Nikolas Klauser | c6f3b7b | 2024-11-06 09:39:19 | [diff] [blame^] | 131 | #endif // _LIBCPP_HAS_THREADS |
Howard Hinnant | d77851e | 2012-07-30 01:40:57 | [diff] [blame] | 132 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 133 | void* align(size_t alignment, size_t size, void*& ptr, size_t& space) { |
| 134 | void* r = nullptr; |
| 135 | if (size <= space) { |
| 136 | char* p1 = static_cast<char*>(ptr); |
| 137 | char* p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 + (alignment - 1)) & -alignment); |
| 138 | size_t d = static_cast<size_t>(p2 - p1); |
| 139 | if (d <= space - size) { |
| 140 | r = p2; |
| 141 | ptr = r; |
| 142 | space -= d; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 143 | } |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 144 | } |
| 145 | return r; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | _LIBCPP_END_NAMESPACE_STD |