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