3030#include < __memory/compressed_pair.h>
3131#include < __memory/construct_at.h>
3232#include < __memory/pointer_traits.h>
33+ #include < __memory/shared_count.h>
3334#include < __memory/uninitialized_algorithms.h>
3435#include < __memory/unique_ptr.h>
3536#include < __type_traits/add_lvalue_reference.h>
@@ -70,55 +71,6 @@ _LIBCPP_PUSH_MACROS
7071
7172_LIBCPP_BEGIN_NAMESPACE_STD
7273
73- // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
74- // should be sufficient for thread safety.
75- // See https://llvm.org/PR22803
76- #if (defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && \
77- defined (__ATOMIC_ACQ_REL)) || \
78- defined(_LIBCPP_COMPILER_GCC)
79- # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 1
80- #else
81- # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 0
82- #endif
83-
84- template <class _ValueType >
85- inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load (_ValueType const * __value) {
86- #if _LIBCPP_HAS_THREADS && defined(__ATOMIC_RELAXED) && \
87- (__has_builtin (__atomic_load_n) || defined (_LIBCPP_COMPILER_GCC))
88- return __atomic_load_n (__value, __ATOMIC_RELAXED);
89- #else
90- return *__value;
91- #endif
92- }
93-
94- template <class _ValueType >
95- inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load (_ValueType const * __value) {
96- #if _LIBCPP_HAS_THREADS && defined(__ATOMIC_ACQUIRE) && \
97- (__has_builtin (__atomic_load_n) || defined (_LIBCPP_COMPILER_GCC))
98- return __atomic_load_n (__value, __ATOMIC_ACQUIRE);
99- #else
100- return *__value;
101- #endif
102- }
103-
104- template <class _Tp >
105- inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment (_Tp& __t ) _NOEXCEPT {
106- #if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
107- return __atomic_add_fetch (&__t , 1 , __ATOMIC_RELAXED);
108- #else
109- return __t += 1 ;
110- #endif
111- }
112-
113- template <class _Tp >
114- inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement (_Tp& __t ) _NOEXCEPT {
115- #if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
116- return __atomic_add_fetch (&__t , -1 , __ATOMIC_ACQ_REL);
117- #else
118- return __t -= 1 ;
119- #endif
120- }
121-
12274class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr : public std::exception {
12375public:
12476 _LIBCPP_HIDE_FROM_ABI bad_weak_ptr () _NOEXCEPT = default;
@@ -139,70 +91,6 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr : public std::exception {
13991template <class _Tp >
14092class _LIBCPP_TEMPLATE_VIS weak_ptr;
14193
142- class _LIBCPP_EXPORTED_FROM_ABI __shared_count {
143- __shared_count (const __shared_count&);
144- __shared_count& operator =(const __shared_count&);
145-
146- protected:
147- long __shared_owners_;
148- virtual ~__shared_count ();
149-
150- private:
151- virtual void __on_zero_shared () _NOEXCEPT = 0;
152-
153- public:
154- _LIBCPP_HIDE_FROM_ABI explicit __shared_count (long __refs = 0 ) _NOEXCEPT : __shared_owners_(__refs) {}
155-
156- #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
157- void __add_shared () noexcept ;
158- bool __release_shared () noexcept ;
159- #else
160- _LIBCPP_HIDE_FROM_ABI void __add_shared () _NOEXCEPT { __libcpp_atomic_refcount_increment (__shared_owners_); }
161- _LIBCPP_HIDE_FROM_ABI bool __release_shared () _NOEXCEPT {
162- if (__libcpp_atomic_refcount_decrement (__shared_owners_) == -1 ) {
163- __on_zero_shared ();
164- return true ;
165- }
166- return false ;
167- }
168- #endif
169- _LIBCPP_HIDE_FROM_ABI long use_count () const _NOEXCEPT { return __libcpp_relaxed_load (&__shared_owners_) + 1 ; }
170- };
171-
172- class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {
173- long __shared_weak_owners_;
174-
175- public:
176- _LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count (long __refs = 0 ) _NOEXCEPT
177- : __shared_count(__refs),
178- __shared_weak_owners_(__refs) {}
179-
180- protected:
181- ~__shared_weak_count () override ;
182-
183- public:
184- #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
185- void __add_shared () noexcept ;
186- void __add_weak () noexcept ;
187- void __release_shared () noexcept ;
188- #else
189- _LIBCPP_HIDE_FROM_ABI void __add_shared () _NOEXCEPT { __shared_count::__add_shared (); }
190- _LIBCPP_HIDE_FROM_ABI void __add_weak () _NOEXCEPT { __libcpp_atomic_refcount_increment (__shared_weak_owners_); }
191- _LIBCPP_HIDE_FROM_ABI void __release_shared () _NOEXCEPT {
192- if (__shared_count::__release_shared ())
193- __release_weak ();
194- }
195- #endif
196- void __release_weak () _NOEXCEPT;
197- _LIBCPP_HIDE_FROM_ABI long use_count () const _NOEXCEPT { return __shared_count::use_count (); }
198- __shared_weak_count* lock () _NOEXCEPT;
199-
200- virtual const void * __get_deleter (const type_info&) const _NOEXCEPT;
201-
202- private:
203- virtual void __on_zero_shared_weak () _NOEXCEPT = 0;
204- };
205-
20694template <class _Tp , class _Dp , class _Alloc >
20795class __shared_ptr_pointer : public __shared_weak_count {
20896 _LIBCPP_COMPRESSED_TRIPLE (_Tp, __ptr_, _Dp, __deleter_, _Alloc, __alloc_);
0 commit comments