[rtsan] Remove mkfifoat interceptor (#116997)
[llvm-project.git] / libcxx / include / __memory / shared_count.h
blob1438c6ba5a6d29ca0b98d64790be00fbde078ed7
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___MEMORY_SHARED_COUNT_H
10 #define _LIBCPP___MEMORY_SHARED_COUNT_H
12 #include <__config>
13 #include <typeinfo>
15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16 # pragma GCC system_header
17 #endif
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
28 #else
29 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 0
30 #endif
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);
37 #else
38 return *__value;
39 #endif
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);
47 #else
48 return *__value;
49 #endif
52 template <class _Tp>
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);
56 #else
57 return __t += 1;
58 #endif
61 template <class _Tp>
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);
65 #else
66 return __t -= 1;
67 #endif
70 class _LIBCPP_EXPORTED_FROM_ABI __shared_count {
71 __shared_count(const __shared_count&);
72 __shared_count& operator=(const __shared_count&);
74 protected:
75 long __shared_owners_;
76 virtual ~__shared_count();
78 private:
79 virtual void __on_zero_shared() _NOEXCEPT = 0;
81 public:
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;
87 #else
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) {
91 __on_zero_shared();
92 return true;
94 return false;
96 #endif
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_;
103 public:
104 _LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
105 : __shared_count(__refs),
106 __shared_weak_owners_(__refs) {}
108 protected:
109 ~__shared_weak_count() override;
111 public:
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;
116 #else
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())
121 __release_weak();
123 #endif
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;
130 private:
131 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
134 _LIBCPP_END_NAMESPACE_STD
136 #endif // _LIBCPP___MEMORY_SHARED_COUNT_H