Path: blob/main/contrib/llvm-project/libcxx/include/__memory/shared_count.h
213766 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#ifndef _LIBCPP___MEMORY_SHARED_COUNT_H9#define _LIBCPP___MEMORY_SHARED_COUNT_H1011#include <__config>12#include <__memory/addressof.h>13#include <typeinfo>1415#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)16# pragma GCC system_header17#endif1819_LIBCPP_BEGIN_NAMESPACE_STD2021// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)22// should be sufficient for thread safety.23// See https://llvm.org/PR2280324#if (defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && \25defined(__ATOMIC_ACQ_REL)) || \26defined(_LIBCPP_COMPILER_GCC)27# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 128#else29# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 030#endif3132template <class _ValueType>33inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load(_ValueType const* __value) {34#if _LIBCPP_HAS_THREADS && defined(__ATOMIC_RELAXED) && \35(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))36return __atomic_load_n(__value, __ATOMIC_RELAXED);37#else38return *__value;39#endif40}4142template <class _ValueType>43inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {44#if _LIBCPP_HAS_THREADS && defined(__ATOMIC_ACQUIRE) && \45(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))46return __atomic_load_n(__value, __ATOMIC_ACQUIRE);47#else48return *__value;49#endif50}5152template <class _Tp>53inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT {54#if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS55return __atomic_add_fetch(std::addressof(__t), 1, __ATOMIC_RELAXED);56#else57return __t += 1;58#endif59}6061template <class _Tp>62inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT {63#if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS64return __atomic_add_fetch(std::addressof(__t), -1, __ATOMIC_ACQ_REL);65#else66return __t -= 1;67#endif68}6970class _LIBCPP_EXPORTED_FROM_ABI __shared_count {71__shared_count(const __shared_count&);72__shared_count& operator=(const __shared_count&);7374protected:75long __shared_owners_;76virtual ~__shared_count();7778private:79virtual void __on_zero_shared() _NOEXCEPT = 0;8081public:82_LIBCPP_HIDE_FROM_ABI explicit __shared_count(long __refs = 0) _NOEXCEPT : __shared_owners_(__refs) {}8384#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)85void __add_shared() noexcept;86bool __release_shared() noexcept;87#else88_LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_owners_); }89_LIBCPP_HIDE_FROM_ABI bool __release_shared() _NOEXCEPT {90if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {91__on_zero_shared();92return true;93}94return false;95}96#endif97_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __libcpp_relaxed_load(&__shared_owners_) + 1; }98};99100class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {101long __shared_weak_owners_;102103public:104_LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count(long __refs = 0) _NOEXCEPT105: __shared_count(__refs),106__shared_weak_owners_(__refs) {}107108protected:109~__shared_weak_count() override;110111public:112#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)113void __add_shared() noexcept;114void __add_weak() noexcept;115void __release_shared() noexcept;116#else117_LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __shared_count::__add_shared(); }118_LIBCPP_HIDE_FROM_ABI void __add_weak() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_weak_owners_); }119_LIBCPP_HIDE_FROM_ABI void __release_shared() _NOEXCEPT {120if (__shared_count::__release_shared())121__release_weak();122}123#endif124void __release_weak() _NOEXCEPT;125_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __shared_count::use_count(); }126__shared_weak_count* lock() _NOEXCEPT;127128virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;129130private:131virtual void __on_zero_shared_weak() _NOEXCEPT = 0;132};133134_LIBCPP_END_NAMESPACE_STD135136#endif // _LIBCPP___MEMORY_SHARED_COUNT_H137138139