8#include <userver/engine/mutex.hpp>
10USERVER_NAMESPACE_BEGIN
16template <
typename Lock,
typename Data>
17class LockedPtr final {
19 using Mutex =
typename Lock::mutex_type;
21 LockedPtr(Mutex& mutex, Data& data) : lock_(mutex), data_(data) {}
22 LockedPtr(Lock&& lock, Data& data) : lock_(std::move(lock)), data_(data) {}
24 Data& operator*() & {
return data_; }
25 const Data& operator*()
const& {
return data_; }
28 Data&
operator*() && {
return *GetOnRvalue(); }
30 Data* operator->() & {
return &data_; }
31 const Data* operator->()
const& {
return &data_; }
34 Data*
operator->() && {
return GetOnRvalue(); }
36 Lock& GetLock() {
return lock_; }
39 const Data* GetOnRvalue() {
40 static_assert(!
sizeof(Data),
"Don't use temporary LockedPtr, store it to a variable");
57template <
typename Data,
typename Mutex = engine::Mutex>
60 template <
typename... Arg>
61 Variable(Arg&&... arg) : data_(std::forward<Arg>(arg)...) {}
63 LockedPtr<std::unique_lock<Mutex>, Data> UniqueLock() {
return {mutex_, data_}; }
65 LockedPtr<std::unique_lock<Mutex>,
const Data> UniqueLock()
const {
return {mutex_, data_}; }
67 std::optional<LockedPtr<std::unique_lock<Mutex>,
const Data>> UniqueLock(std::try_to_lock_t)
const {
68 return DoUniqueLock(*
this, std::try_to_lock);
71 std::optional<LockedPtr<std::unique_lock<Mutex>, Data>> UniqueLock(std::try_to_lock_t) {
72 return DoUniqueLock(*
this, std::try_to_lock);
75 std::optional<LockedPtr<std::unique_lock<Mutex>,
const Data>> UniqueLock(std::chrono::milliseconds try_duration
77 return DoUniqueLock(*
this, try_duration);
80 std::optional<LockedPtr<std::unique_lock<Mutex>, Data>> UniqueLock(std::chrono::milliseconds try_duration) {
81 return DoUniqueLock(*
this, try_duration);
84 LockedPtr<std::shared_lock<Mutex>,
const Data> SharedLock()
const {
return {mutex_, data_}; }
90 LockedPtr<std::lock_guard<Mutex>, Data> Lock() {
return {mutex_, data_}; }
92 LockedPtr<std::lock_guard<Mutex>,
const Data> Lock()
const {
return {mutex_, data_}; }
104 const Data& GetDataUnsafe()
const {
return data_; }
107 mutable Mutex mutex_;
114 template <
typename VariableType,
typename... StdUniqueLockArgs>
115 static auto DoUniqueLock(VariableType& concurrent_variable, StdUniqueLockArgs&&... args) {
116 std::unique_lock<Mutex> lock(concurrent_variable.mutex_, std::forward<StdUniqueLockArgs>(args)...);
117 return lock ? std::optional{LockedPtr{std::move(lock), concurrent_variable.data_}} : std::nullopt;