xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/shared_count.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric 
9*700637cbSDimitry Andric #ifndef _LIBCPP___MEMORY_SHARED_COUNT_H
10*700637cbSDimitry Andric #define _LIBCPP___MEMORY_SHARED_COUNT_H
11*700637cbSDimitry Andric 
12*700637cbSDimitry Andric #include <__config>
13*700637cbSDimitry Andric #include <__memory/addressof.h>
14*700637cbSDimitry Andric #include <typeinfo>
15*700637cbSDimitry Andric 
16*700637cbSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17*700637cbSDimitry Andric #  pragma GCC system_header
18*700637cbSDimitry Andric #endif
19*700637cbSDimitry Andric 
20*700637cbSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
21*700637cbSDimitry Andric 
22*700637cbSDimitry Andric // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
23*700637cbSDimitry Andric // should be sufficient for thread safety.
24*700637cbSDimitry Andric // See https://llvm.org/PR22803
25*700637cbSDimitry Andric #if (defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) &&                           \
26*700637cbSDimitry Andric      defined(__ATOMIC_ACQ_REL)) ||                                                                                     \
27*700637cbSDimitry Andric     defined(_LIBCPP_COMPILER_GCC)
28*700637cbSDimitry Andric #  define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 1
29*700637cbSDimitry Andric #else
30*700637cbSDimitry Andric #  define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 0
31*700637cbSDimitry Andric #endif
32*700637cbSDimitry Andric 
33*700637cbSDimitry Andric template <class _ValueType>
__libcpp_relaxed_load(_ValueType const * __value)34*700637cbSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
35*700637cbSDimitry Andric #if _LIBCPP_HAS_THREADS && defined(__ATOMIC_RELAXED) &&                                                                \
36*700637cbSDimitry Andric     (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
37*700637cbSDimitry Andric   return __atomic_load_n(__value, __ATOMIC_RELAXED);
38*700637cbSDimitry Andric #else
39*700637cbSDimitry Andric   return *__value;
40*700637cbSDimitry Andric #endif
41*700637cbSDimitry Andric }
42*700637cbSDimitry Andric 
43*700637cbSDimitry Andric template <class _ValueType>
__libcpp_acquire_load(_ValueType const * __value)44*700637cbSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {
45*700637cbSDimitry Andric #if _LIBCPP_HAS_THREADS && defined(__ATOMIC_ACQUIRE) &&                                                                \
46*700637cbSDimitry Andric     (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
47*700637cbSDimitry Andric   return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
48*700637cbSDimitry Andric #else
49*700637cbSDimitry Andric   return *__value;
50*700637cbSDimitry Andric #endif
51*700637cbSDimitry Andric }
52*700637cbSDimitry Andric 
53*700637cbSDimitry Andric template <class _Tp>
__libcpp_atomic_refcount_increment(_Tp & __t)54*700637cbSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT {
55*700637cbSDimitry Andric #if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
56*700637cbSDimitry Andric   return __atomic_add_fetch(std::addressof(__t), 1, __ATOMIC_RELAXED);
57*700637cbSDimitry Andric #else
58*700637cbSDimitry Andric   return __t += 1;
59*700637cbSDimitry Andric #endif
60*700637cbSDimitry Andric }
61*700637cbSDimitry Andric 
62*700637cbSDimitry Andric template <class _Tp>
__libcpp_atomic_refcount_decrement(_Tp & __t)63*700637cbSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT {
64*700637cbSDimitry Andric #if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
65*700637cbSDimitry Andric   return __atomic_add_fetch(std::addressof(__t), -1, __ATOMIC_ACQ_REL);
66*700637cbSDimitry Andric #else
67*700637cbSDimitry Andric   return __t -= 1;
68*700637cbSDimitry Andric #endif
69*700637cbSDimitry Andric }
70*700637cbSDimitry Andric 
71*700637cbSDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI __shared_count {
72*700637cbSDimitry Andric   __shared_count(const __shared_count&);
73*700637cbSDimitry Andric   __shared_count& operator=(const __shared_count&);
74*700637cbSDimitry Andric 
75*700637cbSDimitry Andric protected:
76*700637cbSDimitry Andric   long __shared_owners_;
77*700637cbSDimitry Andric   virtual ~__shared_count();
78*700637cbSDimitry Andric 
79*700637cbSDimitry Andric private:
80*700637cbSDimitry Andric   virtual void __on_zero_shared() _NOEXCEPT = 0;
81*700637cbSDimitry Andric 
82*700637cbSDimitry Andric public:
__shared_owners_(__refs)83*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit __shared_count(long __refs = 0) _NOEXCEPT : __shared_owners_(__refs) {}
84*700637cbSDimitry Andric 
85*700637cbSDimitry Andric #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
86*700637cbSDimitry Andric   void __add_shared() noexcept;
87*700637cbSDimitry Andric   bool __release_shared() noexcept;
88*700637cbSDimitry Andric #else
__add_shared()89*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_owners_); }
__release_shared()90*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool __release_shared() _NOEXCEPT {
91*700637cbSDimitry Andric     if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
92*700637cbSDimitry Andric       __on_zero_shared();
93*700637cbSDimitry Andric       return true;
94*700637cbSDimitry Andric     }
95*700637cbSDimitry Andric     return false;
96*700637cbSDimitry Andric   }
97*700637cbSDimitry Andric #endif
use_count()98*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __libcpp_relaxed_load(&__shared_owners_) + 1; }
99*700637cbSDimitry Andric };
100*700637cbSDimitry Andric 
101*700637cbSDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {
102*700637cbSDimitry Andric   long __shared_weak_owners_;
103*700637cbSDimitry Andric 
104*700637cbSDimitry Andric public:
105*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
__shared_count(__refs)106*700637cbSDimitry Andric       : __shared_count(__refs),
107*700637cbSDimitry Andric         __shared_weak_owners_(__refs) {}
108*700637cbSDimitry Andric 
109*700637cbSDimitry Andric protected:
110*700637cbSDimitry Andric   ~__shared_weak_count() override;
111*700637cbSDimitry Andric 
112*700637cbSDimitry Andric public:
113*700637cbSDimitry Andric #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
114*700637cbSDimitry Andric   void __add_shared() noexcept;
115*700637cbSDimitry Andric   void __add_weak() noexcept;
116*700637cbSDimitry Andric   void __release_shared() noexcept;
117*700637cbSDimitry Andric #else
118*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __shared_count::__add_shared(); }
119*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __add_weak() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_weak_owners_); }
120*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __release_shared() _NOEXCEPT {
121*700637cbSDimitry Andric     if (__shared_count::__release_shared())
122*700637cbSDimitry Andric       __release_weak();
123*700637cbSDimitry Andric   }
124*700637cbSDimitry Andric #endif
125*700637cbSDimitry Andric   void __release_weak() _NOEXCEPT;
use_count()126*700637cbSDimitry Andric   _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __shared_count::use_count(); }
127*700637cbSDimitry Andric   __shared_weak_count* lock() _NOEXCEPT;
128*700637cbSDimitry Andric 
129*700637cbSDimitry Andric   virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
130*700637cbSDimitry Andric 
131*700637cbSDimitry Andric private:
132*700637cbSDimitry Andric   virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
133*700637cbSDimitry Andric };
134*700637cbSDimitry Andric 
135*700637cbSDimitry Andric _LIBCPP_END_NAMESPACE_STD
136*700637cbSDimitry Andric 
137*700637cbSDimitry Andric #endif // _LIBCPP___MEMORY_SHARED_COUNT_H
138