HowToReleaseLLVM: Add description of the bug triage process
[llvm-project.git] / libcxx / src / memory.cpp
blobbf96efa9ce6d41180ba1a9aa7f90fd83beeccdf2
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include <__config>
10 #ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
11 # define _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS
12 #endif
14 #include <memory>
16 #ifndef _LIBCPP_HAS_NO_THREADS
17 # include <mutex>
18 # include <thread>
19 # if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
20 # pragma comment(lib, "pthread")
21 # endif
22 #endif
24 #include "include/atomic_support.h"
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 const allocator_arg_t allocator_arg = allocator_arg_t();
30 bad_weak_ptr::~bad_weak_ptr() noexcept {}
32 const char*
33 bad_weak_ptr::what() const noexcept
35 return "bad_weak_ptr";
38 __shared_count::~__shared_count()
42 __shared_weak_count::~__shared_weak_count()
46 #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
47 void
48 __shared_count::__add_shared() noexcept
50 __libcpp_atomic_refcount_increment(__shared_owners_);
53 bool
54 __shared_count::__release_shared() noexcept
56 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1)
58 __on_zero_shared();
59 return true;
61 return false;
64 void
65 __shared_weak_count::__add_shared() noexcept
67 __shared_count::__add_shared();
70 void
71 __shared_weak_count::__add_weak() noexcept
73 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
76 void
77 __shared_weak_count::__release_shared() noexcept
79 if (__shared_count::__release_shared())
80 __release_weak();
82 #endif // _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS
84 void
85 __shared_weak_count::__release_weak() noexcept
87 // NOTE: The acquire load here is an optimization of the very
88 // common case where a shared pointer is being destructed while
89 // having no other contended references.
91 // BENEFIT: We avoid expensive atomic stores like XADD and STREX
92 // in a common case. Those instructions are slow and do nasty
93 // things to caches.
95 // IS THIS SAFE? Yes. During weak destruction, if we see that we
96 // are the last reference, we know that no-one else is accessing
97 // us. If someone were accessing us, then they would be doing so
98 // while the last shared / weak_ptr was being destructed, and
99 // that's undefined anyway.
101 // If we see anything other than a 0, then we have possible
102 // contention, and need to use an atomicrmw primitive.
103 // The same arguments don't apply for increment, where it is legal
104 // (though inadvisable) to share shared_ptr references between
105 // threads, and have them all get copied at once. The argument
106 // also doesn't apply for __release_shared, because an outstanding
107 // weak_ptr::lock() could read / modify the shared count.
108 if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0)
110 // no need to do this store, because we are about
111 // to destroy everything.
112 //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release);
113 __on_zero_shared_weak();
115 else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1)
116 __on_zero_shared_weak();
119 __shared_weak_count*
120 __shared_weak_count::lock() noexcept
122 long object_owners = __libcpp_atomic_load(&__shared_owners_);
123 while (object_owners != -1)
125 if (__libcpp_atomic_compare_exchange(&__shared_owners_,
126 &object_owners,
127 object_owners+1))
128 return this;
130 return nullptr;
133 const void*
134 __shared_weak_count::__get_deleter(const type_info&) const noexcept
136 return nullptr;
139 #if !defined(_LIBCPP_HAS_NO_THREADS)
141 static constexpr std::size_t __sp_mut_count = 32;
142 static constinit __libcpp_mutex_t mut_back[__sp_mut_count] =
144 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
145 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
146 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
147 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
148 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
149 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
150 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
151 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER
154 _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) noexcept
155 : __lx(p)
159 void
160 __sp_mut::lock() noexcept
162 auto m = static_cast<__libcpp_mutex_t*>(__lx);
163 __libcpp_mutex_lock(m);
166 void
167 __sp_mut::unlock() noexcept
169 __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx));
172 __sp_mut&
173 __get_sp_mut(const void* p)
175 static constinit __sp_mut muts[__sp_mut_count] = {
176 &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
177 &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
178 &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],
179 &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15],
180 &mut_back[16], &mut_back[17], &mut_back[18], &mut_back[19],
181 &mut_back[20], &mut_back[21], &mut_back[22], &mut_back[23],
182 &mut_back[24], &mut_back[25], &mut_back[26], &mut_back[27],
183 &mut_back[28], &mut_back[29], &mut_back[30], &mut_back[31]
185 return muts[hash<const void*>()(p) & (__sp_mut_count-1)];
188 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
190 void*
191 align(size_t alignment, size_t size, void*& ptr, size_t& space)
193 void* r = nullptr;
194 if (size <= space)
196 char* p1 = static_cast<char*>(ptr);
197 char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
198 size_t d = static_cast<size_t>(p2 - p1);
199 if (d <= space - size)
201 r = p2;
202 ptr = r;
203 space -= d;
206 return r;
209 _LIBCPP_END_NAMESPACE_STD