1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___MEMORY_SHARED_COUNT_H
10 #define _LIBCPP___MEMORY_SHARED_COUNT_H
15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16 # pragma GCC system_header
19 _LIBCPP_BEGIN_NAMESPACE_STD
21 // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
22 // should be sufficient for thread safety.
23 // See https://llvm.org/PR22803
24 #if (defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && \
25 defined(__ATOMIC_ACQ_REL)) || \
26 defined(_LIBCPP_COMPILER_GCC)
27 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 1
29 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 0
32 template <class _ValueType
>
33 inline _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))
36 return __atomic_load_n(__value
, __ATOMIC_RELAXED
);
42 template <class _ValueType
>
43 inline _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))
46 return __atomic_load_n(__value
, __ATOMIC_ACQUIRE
);
53 inline _LIBCPP_HIDE_FROM_ABI _Tp
__libcpp_atomic_refcount_increment(_Tp
& __t
) _NOEXCEPT
{
54 #if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
55 return __atomic_add_fetch(&__t
, 1, __ATOMIC_RELAXED
);
62 inline _LIBCPP_HIDE_FROM_ABI _Tp
__libcpp_atomic_refcount_decrement(_Tp
& __t
) _NOEXCEPT
{
63 #if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
64 return __atomic_add_fetch(&__t
, -1, __ATOMIC_ACQ_REL
);
70 class _LIBCPP_EXPORTED_FROM_ABI __shared_count
{
71 __shared_count(const __shared_count
&);
72 __shared_count
& operator=(const __shared_count
&);
75 long __shared_owners_
;
76 virtual ~__shared_count();
79 virtual void __on_zero_shared() _NOEXCEPT
= 0;
82 _LIBCPP_HIDE_FROM_ABI
explicit __shared_count(long __refs
= 0) _NOEXCEPT
: __shared_owners_(__refs
) {}
84 #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
85 void __add_shared() noexcept
;
86 bool __release_shared() noexcept
;
88 _LIBCPP_HIDE_FROM_ABI
void __add_shared() _NOEXCEPT
{ __libcpp_atomic_refcount_increment(__shared_owners_
); }
89 _LIBCPP_HIDE_FROM_ABI
bool __release_shared() _NOEXCEPT
{
90 if (__libcpp_atomic_refcount_decrement(__shared_owners_
) == -1) {
97 _LIBCPP_HIDE_FROM_ABI
long use_count() const _NOEXCEPT
{ return __libcpp_relaxed_load(&__shared_owners_
) + 1; }
100 class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count
: private __shared_count
{
101 long __shared_weak_owners_
;
104 _LIBCPP_HIDE_FROM_ABI
explicit __shared_weak_count(long __refs
= 0) _NOEXCEPT
105 : __shared_count(__refs
),
106 __shared_weak_owners_(__refs
) {}
109 ~__shared_weak_count() override
;
112 #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
113 void __add_shared() noexcept
;
114 void __add_weak() noexcept
;
115 void __release_shared() noexcept
;
117 _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
{
120 if (__shared_count::__release_shared())
124 void __release_weak() _NOEXCEPT
;
125 _LIBCPP_HIDE_FROM_ABI
long use_count() const _NOEXCEPT
{ return __shared_count::use_count(); }
126 __shared_weak_count
* lock() _NOEXCEPT
;
128 virtual const void* __get_deleter(const type_info
&) const _NOEXCEPT
;
131 virtual void __on_zero_shared_weak() _NOEXCEPT
= 0;
134 _LIBCPP_END_NAMESPACE_STD
136 #endif // _LIBCPP___MEMORY_SHARED_COUNT_H