userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
lazy_shared_ptr.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/utils/lazy_shared_ptr.hpp
4
/// @brief @copybrief utils::LazySharedPtr
5
6
#
include
<
atomic
>
7
#
include
<
functional
>
8
#
include
<
memory
>
9
#
include
<
utility
>
10
11
#
include
<
userver
/
compiler
/
impl
/
lifetime
.
hpp
>
12
#
include
<
userver
/
engine
/
sleep
.
hpp
>
13
#
include
<
userver
/
utils
/
assert
.
hpp
>
14
#
include
<
userver
/
utils
/
shared_readable_ptr
.
hpp
>
15
16
USERVER_NAMESPACE_BEGIN
17
18
namespace
utils
{
19
20
/// @ingroup userver_containers
21
///
22
/// @brief A lazy wrapper around utils::SharedReadablePtr that fetches the data
23
/// on first access.
24
///
25
/// Provides standard thread-safety guarantee: `const` methods can be called
26
/// concurrently. Once fetched, the same data is guaranteed to be returned
27
/// unless the pointer is assigned to.
28
template
<
typename
T>
29
class
LazySharedPtr final {
30
public
:
31
/// @brief The default constructor, initializes with `nullptr`.
32
LazySharedPtr
()
noexcept
: value_(
nullptr
), shared_filled_(
true
), get_data_() {}
33
34
/// @brief The non-lazy constructor
35
LazySharedPtr
(
utils
::SharedReadablePtr<T> ptr)
noexcept
36
: value_(ptr.Get()), shared_filled_(
true
), shared_(std::move(ptr)) {}
37
38
/// @brief The lazy constructor
39
LazySharedPtr
(std::function<
utils
::SharedReadablePtr<T>()> function)
noexcept
: get_data_(std::move(function)) {}
40
41
/// @brief The copy constructor.
42
/// @note If `other` has not been fetched yet, `this` will not fetch
43
/// immediately, so `this` and `other` may end up pointing to different
44
/// objects.
45
LazySharedPtr
(
const
LazySharedPtr& other)
46
: get_data_(other.get_data_)
47
{
48
if
(other.shared_filled_.load()) {
49
value_.store(other.shared_.Get());
50
shared_ = other.shared_;
51
shared_filled_.store(
true
);
52
}
53
}
54
55
/// @brief The move constructor.
56
LazySharedPtr
(LazySharedPtr&& other)
noexcept
57
: value_(other.value_.load()),
58
shared_filled_(other.shared_filled_.load()),
59
shared_(std::move(other.shared_)),
60
get_data_(std::move(other.get_data_)) {}
61
62
/// @brief The copy assignment operator.
63
/// @note Like copy-constructor, it does not generate pointers if `other` do
64
/// not generate them.
65
LazySharedPtr&
operator
=(
const
LazySharedPtr& other) {
66
*
this
= LazySharedPtr(other);
67
return
*
this
;
68
}
69
70
/// @brief The move assignment operator.
71
LazySharedPtr&
operator
=(LazySharedPtr&& other)
noexcept
{
72
value_.store(other.value_.load(), std::memory_order_relaxed);
73
shared_filled_.store(other.shared_filled_.load(), std::memory_order_relaxed);
74
shared_ = std::move(other.shared_);
75
get_data_ = std::move(other.get_data_);
76
return
*
this
;
77
}
78
79
/// @brief Get a pointer to the data (may be null). Fetches the data on the
80
/// first access.
81
const
T*
Get
()
const
& USERVER_IMPL_LIFETIME_BOUND {
82
if
(
const
auto
* current_value = value_.load(); current_value != kUnfilled) {
83
return
current_value;
84
}
85
auto
readable = get_data_();
86
if
(
const
auto
* expected = kUnfilled; value_.compare_exchange_strong(expected, readable.Get())) {
87
shared_ = std::move(readable);
88
shared_filled_.store(
true
);
89
}
90
return
value_;
91
}
92
93
/// @brief Get a smart pointer to the data (may be null). Fetches the data on
94
/// the first access.
95
const
utils
::SharedReadablePtr<T>&
GetShared
()
const
& USERVER_IMPL_LIFETIME_BOUND {
96
if
(value_ == kUnfilled) {
97
auto
readable = get_data_();
98
if
(
const
auto
* expected = kUnfilled; value_.compare_exchange_strong(expected, readable.Get())) {
99
shared_ = std::move(readable);
100
shared_filled_.store(
true
);
101
}
102
}
103
while
(!shared_filled_.load()) {
104
// Another thread has filled 'value_', but not yet 'shared_'. It will be
105
// filled in a few CPU cycles. Discovering LazySharedPtr in such a state
106
// is expected to be rare.
107
engine
::
Yield
(
)
;
108
}
109
return
shared_;
110
}
111
112
///@returns `*Get()`
113
///@note `Get()` must not be `nullptr`.
114
const
T&
operator
*()
const
& USERVER_IMPL_LIFETIME_BOUND {
115
const
auto
* res =
Get
(
)
;
116
UASSERT
(res);
117
return
*res;
118
}
119
120
/// @returns `Get()`
121
/// @note `Get()` must not be `nullptr`.
122
const
T*
operator
->()
const
& USERVER_IMPL_LIFETIME_BOUND {
return
&**
this
; }
123
124
/// @returns `Get() != nullptr`
125
explicit
operator
bool
()
const
& USERVER_IMPL_LIFETIME_BOUND {
return
!!
Get
(
)
; }
126
127
/// @returns `GetShared()`
128
operator
const
utils
::SharedReadablePtr<T>&()
const
& USERVER_IMPL_LIFETIME_BOUND {
return
GetShared
(
)
; }
129
130
/// @returns `GetShared()`
131
operator
std::shared_ptr<
const
T>()
const
& USERVER_IMPL_LIFETIME_BOUND {
return
GetShared
(
)
; }
132
133
private
:
134
static
inline
const
auto
kUnfilled =
reinterpret_cast
<
const
T*>(std::uintptr_t{1});
135
136
mutable
std::atomic<
const
T*> value_{kUnfilled};
137
mutable
std::atomic<
bool
> shared_filled_{
false
};
138
mutable
utils
::SharedReadablePtr<T> shared_{
nullptr
};
139
std::function<
utils
::SharedReadablePtr<T>()> get_data_{};
140
};
141
142
/// @brief Make a lazy pointer to the data of a cache.
143
///
144
/// The cache type must have:
145
/// - `DataType` member type;
146
/// - `Get() -> utils::SharedReadablePtr<DataType>` method.
147
///
148
/// For example, components::CachingComponentBase satisfies these requirements.
149
template
<
typename
Cache>
150
LazySharedPtr<
typename
Cache::
DataType
>
MakeLazyCachePtr
(Cache& cache) {
151
return
utils
::LazySharedPtr<
typename
Cache::DataType>([&cache] {
return
cache.Get(); });
152
}
153
154
}
// namespace utils
155
156
USERVER_NAMESPACE_END
userver
utils
lazy_shared_ptr.hpp
Generated on
for userver by
Doxygen
1.17.0