xref: /llvm-project-15.0.7/libcxx/src/memory.cpp (revision fbfd828d)
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 static const std::size_t __sp_mut_count = 16;
158 static __libcpp_mutex_t mut_back_imp[__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 static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp);
167 
168 _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
169    : __lx(p)
170 {
171 }
172 
173 void
174 __sp_mut::lock() _NOEXCEPT
175 {
176     mutex& m = *static_cast<mutex*>(__lx);
177     unsigned count = 0;
178     while (!m.try_lock())
179     {
180         if (++count > 16)
181         {
182             m.lock();
183             break;
184         }
185         this_thread::yield();
186     }
187 }
188 
189 void
190 __sp_mut::unlock() _NOEXCEPT
191 {
192     static_cast<mutex*>(__lx)->unlock();
193 }
194 
195 __sp_mut&
196 __get_sp_mut(const void* p)
197 {
198     static __sp_mut muts[__sp_mut_count]
199     {
200         &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
201         &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
202         &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],
203         &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15]
204     };
205     return muts[hash<const void*>()(p) & (__sp_mut_count-1)];
206 }
207 
208 #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
209 
210 void
211 declare_reachable(void*)
212 {
213 }
214 
215 void
216 declare_no_pointers(char*, size_t)
217 {
218 }
219 
220 void
221 undeclare_no_pointers(char*, size_t)
222 {
223 }
224 
225 pointer_safety
226 get_pointer_safety() _NOEXCEPT
227 {
228     return pointer_safety::relaxed;
229 }
230 
231 void*
232 __undeclare_reachable(void* p)
233 {
234     return p;
235 }
236 
237 void*
238 align(size_t alignment, size_t size, void*& ptr, size_t& space)
239 {
240     void* r = nullptr;
241     if (size <= space)
242     {
243         char* p1 = static_cast<char*>(ptr);
244         char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
245         size_t d = static_cast<size_t>(p2 - p1);
246         if (d <= space - size)
247         {
248             r = p2;
249             ptr = r;
250             space -= d;
251         }
252     }
253     return r;
254 }
255 
256 _LIBCPP_END_NAMESPACE_STD
257