Path: blob/main/contrib/llvm-project/libcxx/src/memory.cpp
35147 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <__config>9#ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS10# define _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS11#endif1213#include <memory>1415#ifndef _LIBCPP_HAS_NO_THREADS16# include <mutex>17# include <thread>18# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)19# pragma comment(lib, "pthread")20# endif21#endif2223#include "include/atomic_support.h"2425_LIBCPP_BEGIN_NAMESPACE_STD2627bad_weak_ptr::~bad_weak_ptr() noexcept {}2829const char* bad_weak_ptr::what() const noexcept { return "bad_weak_ptr"; }3031__shared_count::~__shared_count() {}3233__shared_weak_count::~__shared_weak_count() {}3435#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)36void __shared_count::__add_shared() noexcept { __libcpp_atomic_refcount_increment(__shared_owners_); }3738bool __shared_count::__release_shared() noexcept {39if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {40__on_zero_shared();41return true;42}43return false;44}4546void __shared_weak_count::__add_shared() noexcept { __shared_count::__add_shared(); }4748void __shared_weak_count::__add_weak() noexcept { __libcpp_atomic_refcount_increment(__shared_weak_owners_); }4950void __shared_weak_count::__release_shared() noexcept {51if (__shared_count::__release_shared())52__release_weak();53}54#endif // _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS5556void __shared_weak_count::__release_weak() noexcept {57// NOTE: The acquire load here is an optimization of the very58// common case where a shared pointer is being destructed while59// having no other contended references.60//61// BENEFIT: We avoid expensive atomic stores like XADD and STREX62// in a common case. Those instructions are slow and do nasty63// things to caches.64//65// IS THIS SAFE? Yes. During weak destruction, if we see that we66// are the last reference, we know that no-one else is accessing67// us. If someone were accessing us, then they would be doing so68// while the last shared / weak_ptr was being destructed, and69// that's undefined anyway.70//71// If we see anything other than a 0, then we have possible72// contention, and need to use an atomicrmw primitive.73// The same arguments don't apply for increment, where it is legal74// (though inadvisable) to share shared_ptr references between75// threads, and have them all get copied at once. The argument76// also doesn't apply for __release_shared, because an outstanding77// weak_ptr::lock() could read / modify the shared count.78if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0) {79// no need to do this store, because we are about80// to destroy everything.81//__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release);82__on_zero_shared_weak();83} else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1)84__on_zero_shared_weak();85}8687__shared_weak_count* __shared_weak_count::lock() noexcept {88long object_owners = __libcpp_atomic_load(&__shared_owners_);89while (object_owners != -1) {90if (__libcpp_atomic_compare_exchange(&__shared_owners_, &object_owners, object_owners + 1))91return this;92}93return nullptr;94}9596const void* __shared_weak_count::__get_deleter(const type_info&) const noexcept { return nullptr; }9798#if !defined(_LIBCPP_HAS_NO_THREADS)99100static constexpr std::size_t __sp_mut_count = 32;101static constinit __libcpp_mutex_t mut_back[__sp_mut_count] = {102_LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,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,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};110111constexpr __sp_mut::__sp_mut(void* p) noexcept : __lx_(p) {}112113void __sp_mut::lock() noexcept {114auto m = static_cast<__libcpp_mutex_t*>(__lx_);115__libcpp_mutex_lock(m);116}117118void __sp_mut::unlock() noexcept { __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx_)); }119120__sp_mut& __get_sp_mut(const void* p) {121static constinit __sp_mut muts[__sp_mut_count] = {122&mut_back[0], &mut_back[1], &mut_back[2], &mut_back[3], &mut_back[4], &mut_back[5], &mut_back[6],123&mut_back[7], &mut_back[8], &mut_back[9], &mut_back[10], &mut_back[11], &mut_back[12], &mut_back[13],124&mut_back[14], &mut_back[15], &mut_back[16], &mut_back[17], &mut_back[18], &mut_back[19], &mut_back[20],125&mut_back[21], &mut_back[22], &mut_back[23], &mut_back[24], &mut_back[25], &mut_back[26], &mut_back[27],126&mut_back[28], &mut_back[29], &mut_back[30], &mut_back[31]};127return muts[hash<const void*>()(p) & (__sp_mut_count - 1)];128}129130#endif // !defined(_LIBCPP_HAS_NO_THREADS)131132void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {133void* r = nullptr;134if (size <= space) {135char* p1 = static_cast<char*>(ptr);136char* p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 + (alignment - 1)) & -alignment);137size_t d = static_cast<size_t>(p2 - p1);138if (d <= space - size) {139r = p2;140ptr = r;141space -= d;142}143}144return r;145}146147_LIBCPP_END_NAMESPACE_STD148149150