1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___MEMORY_SHARED_COUNT_H
10#define _LIBCPP___MEMORY_SHARED_COUNT_H
11
12#include <__config>
13#include <__memory/addressof.h>
14#include <typeinfo>
15
16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17# pragma GCC system_header
18#endif
19
20_LIBCPP_BEGIN_NAMESPACE_STD
21
22// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
23// should be sufficient for thread safety.
24// See https://llvm.org/PR22803
25
26template <class _Tp>
27inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT {
28#if _LIBCPP_HAS_THREADS
29 return __atomic_add_fetch(std::addressof(__t), 1, __ATOMIC_RELAXED);
30#else
31 return __t += 1;
32#endif
33}
34
35template <class _Tp>
36inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT {
37#if _LIBCPP_HAS_THREADS
38 return __atomic_add_fetch(std::addressof(__t), -1, __ATOMIC_ACQ_REL);
39#else
40 return __t -= 1;
41#endif
42}
43
44class _LIBCPP_EXPORTED_FROM_ABI __shared_count {
45 __shared_count(const __shared_count&);
46 __shared_count& operator=(const __shared_count&);
47
48protected:
49 long __shared_owners_;
50 virtual ~__shared_count();
51
52private:
53 virtual void __on_zero_shared() _NOEXCEPT = 0;
54
55public:
56 _LIBCPP_HIDE_FROM_ABI explicit __shared_count(long __refs = 0) _NOEXCEPT : __shared_owners_(__refs) {}
57
58#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
59 void __add_shared() noexcept;
60 bool __release_shared() noexcept;
61#else
62 _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __libcpp_atomic_refcount_increment(t&: __shared_owners_); }
63 _LIBCPP_HIDE_FROM_ABI bool __release_shared() _NOEXCEPT {
64 if (__libcpp_atomic_refcount_decrement(t&: __shared_owners_) == -1) {
65 __on_zero_shared();
66 return true;
67 }
68 return false;
69 }
70#endif
71 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT {
72#if _LIBCPP_HAS_THREADS
73 return __atomic_load_n(&__shared_owners_, __ATOMIC_RELAXED) + 1;
74#else
75 return __shared_owners_ + 1;
76#endif
77 }
78};
79
80class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {
81 long __shared_weak_owners_;
82
83public:
84 _LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
85 : __shared_count(__refs),
86 __shared_weak_owners_(__refs) {}
87
88protected:
89 ~__shared_weak_count() override;
90
91public:
92#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
93 void __add_shared() noexcept;
94 void __add_weak() noexcept;
95 void __release_shared() noexcept;
96#else
97 _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __shared_count::__add_shared(); }
98 _LIBCPP_HIDE_FROM_ABI void __add_weak() _NOEXCEPT { __libcpp_atomic_refcount_increment(t&: __shared_weak_owners_); }
99 _LIBCPP_HIDE_FROM_ABI void __release_shared() _NOEXCEPT {
100 if (__shared_count::__release_shared())
101 __release_weak();
102 }
103#endif
104 void __release_weak() _NOEXCEPT;
105 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __shared_count::use_count(); }
106 __shared_weak_count* lock() _NOEXCEPT;
107
108 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
109
110private:
111 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
112};
113
114_LIBCPP_END_NAMESPACE_STD
115
116#endif // _LIBCPP___MEMORY_SHARED_COUNT_H
117