Warning: This file is not a C or C++ file. It does not have highlighting.

1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
11#define _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
12
13#include <__atomic/atomic.h>
14#include <__atomic/memory_order.h>
15#include <__config>
16#include <__cstddef/nullptr_t.h>
17#include <__memory/addressof.h>
18#include <__type_traits/is_reference.h>
19#include <__utility/move.h>
20#include <__utility/swap.h>
21
22#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23# pragma GCC system_header
24#endif
25
26_LIBCPP_PUSH_MACROS
27#include <__undef_macros>
28
29_LIBCPP_BEGIN_NAMESPACE_STD
30
31#if _LIBCPP_STD_VER >= 20
32
33// For intrusive_shared_ptr to work with a type T, specialize __intrusive_shared_ptr_traits<T> and implement
34// the following function:
35//
36// static std::atomic<U>& __get_atomic_ref_count(T&);
37//
38// where U must be an integral type representing the number of references to the object.
39template <class _Tp>
40struct __intrusive_shared_ptr_traits;
41
42// A reference counting shared_ptr for types whose reference counter
43// is stored inside the class _Tp itself.
44// When the reference count goes to zero, the destructor of _Tp will be called
45template <class _Tp>
46struct __intrusive_shared_ptr {
47 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr() = default;
48
49 _LIBCPP_HIDE_FROM_ABI explicit __intrusive_shared_ptr(_Tp* __raw_ptr) : __raw_ptr_(__raw_ptr) {
50 if (__raw_ptr_)
51 __increment_ref_count(*__raw_ptr_);
52 }
53
54 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr(const __intrusive_shared_ptr& __other) noexcept
55 : __raw_ptr_(__other.__raw_ptr_) {
56 if (__raw_ptr_)
57 __increment_ref_count(*__raw_ptr_);
58 }
59
60 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr(__intrusive_shared_ptr&& __other) noexcept
61 : __raw_ptr_(__other.__raw_ptr_) {
62 __other.__raw_ptr_ = nullptr;
63 }
64
65 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr& operator=(const __intrusive_shared_ptr& __other) noexcept {
66 if (__other.__raw_ptr_ != __raw_ptr_) {
67 if (__other.__raw_ptr_) {
68 __increment_ref_count(*__other.__raw_ptr_);
69 }
70 if (__raw_ptr_) {
71 __decrement_ref_count(*__raw_ptr_);
72 }
73 __raw_ptr_ = __other.__raw_ptr_;
74 }
75 return *this;
76 }
77
78 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr& operator=(__intrusive_shared_ptr&& __other) noexcept {
79 __intrusive_shared_ptr(std::move(__other)).swap(*this);
80 return *this;
81 }
82
83 _LIBCPP_HIDE_FROM_ABI ~__intrusive_shared_ptr() {
84 if (__raw_ptr_) {
85 __decrement_ref_count(*__raw_ptr_);
86 }
87 }
88
89 _LIBCPP_HIDE_FROM_ABI _Tp* operator->() const noexcept { return __raw_ptr_; }
90 _LIBCPP_HIDE_FROM_ABI _Tp& operator*() const noexcept { return *__raw_ptr_; }
91 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __raw_ptr_ != nullptr; }
92
93 _LIBCPP_HIDE_FROM_ABI void swap(__intrusive_shared_ptr& __other) { std::swap(__raw_ptr_, __other.__raw_ptr_); }
94
95 _LIBCPP_HIDE_FROM_ABI friend void swap(__intrusive_shared_ptr& __lhs, __intrusive_shared_ptr& __rhs) {
96 __lhs.swap(__rhs);
97 }
98
99 _LIBCPP_HIDE_FROM_ABI friend bool constexpr
100 operator==(const __intrusive_shared_ptr&, const __intrusive_shared_ptr&) = default;
101
102 _LIBCPP_HIDE_FROM_ABI friend bool constexpr operator==(const __intrusive_shared_ptr& __ptr, std::nullptr_t) {
103 return __ptr.__raw_ptr_ == nullptr;
104 }
105
106private:
107 _Tp* __raw_ptr_ = nullptr;
108
109 // the memory order for increment/decrement the counter is the same for shared_ptr
110 // increment is relaxed and decrement is acq_rel
111 _LIBCPP_HIDE_FROM_ABI static void __increment_ref_count(_Tp& __obj) {
112 __get_atomic_ref_count(__obj).fetch_add(1, std::memory_order_relaxed);
113 }
114
115 _LIBCPP_HIDE_FROM_ABI static void __decrement_ref_count(_Tp& __obj) {
116 if (__get_atomic_ref_count(__obj).fetch_sub(1, std::memory_order_acq_rel) == 1) {
117 delete std::addressof(__obj);
118 }
119 }
120
121 _LIBCPP_HIDE_FROM_ABI static decltype(auto) __get_atomic_ref_count(_Tp& __obj) {
122 using __ret_type = decltype(__intrusive_shared_ptr_traits<_Tp>::__get_atomic_ref_count(__obj));
123 static_assert(
124 std::is_reference_v<__ret_type>, "__get_atomic_ref_count should return a reference to the atomic counter");
125 return __intrusive_shared_ptr_traits<_Tp>::__get_atomic_ref_count(__obj);
126 }
127};
128
129#endif // _LIBCPP_STD_VER >= 20
130
131_LIBCPP_END_NAMESPACE_STD
132
133_LIBCPP_POP_MACROS
134
135#endif // _LIBCPP___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
136

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of libcxx/include/__stop_token/intrusive_shared_ptr.h