Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Mutex.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include <mutex>
23#include <type_traits>
24
25namespace util {
26
27template <typename ProtectedDataType, typename MutextType>
28class Mutex;
29
37template <typename ProtectedDataType, template <typename...> typename LockType, typename MutexType>
38class Lock {
39 LockType<MutexType> lock_;
40 ProtectedDataType& data_;
41
42public:
44 ProtectedDataType const&
45 operator*() const
46 {
47 return data_;
48 }
49
50 ProtectedDataType&
51 operator*()
52 {
53 return data_;
54 }
55
56 ProtectedDataType const&
57 get() const
58 {
59 return data_;
60 }
61
62 ProtectedDataType&
63 get()
64 {
65 return data_;
66 }
67
68 ProtectedDataType const*
69 operator->() const
70 {
71 return &data_;
72 }
73
74 ProtectedDataType*
75 operator->()
76 {
77 return &data_;
78 }
81private:
82 friend class Mutex<std::remove_const_t<ProtectedDataType>, MutexType>;
83
84 Lock(MutexType& mutex, ProtectedDataType& data) : lock_(mutex), data_(data)
85 {
86 }
87};
88
95template <typename ProtectedDataType, typename MutexType = std::mutex>
96class Mutex {
97 mutable MutexType mutex_;
98 ProtectedDataType data_;
99
100public:
101 Mutex() = default;
102
108 explicit Mutex(ProtectedDataType data) : data_(std::move(data))
109 {
110 }
111
119 template <typename... Args>
120 static Mutex
121 make(Args&&... args)
122 {
123 return Mutex{ProtectedDataType{std::forward<Args>(args)...}};
124 }
125
132 template <template <typename...> typename LockType = std::lock_guard>
134 lock() const
135 {
136 return {mutex_, data_};
137 }
138
145 template <template <typename...> typename LockType = std::lock_guard>
148 {
149 return {mutex_, data_};
150 }
151};
152
153} // namespace util
A lock on a mutex that provides access to the protected data.
Definition Mutex.hpp:38
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:96
Lock< ProtectedDataType, LockType, MutexType > lock()
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:147
static Mutex make(Args &&... args)
Make a new Mutex object with the given data.
Definition Mutex.hpp:121
Mutex(ProtectedDataType data)
Construct a new Mutex object with the given data.
Definition Mutex.hpp:108
Lock< ProtectedDataType const, LockType, MutexType > lock() const
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:134
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70
This namespace contains various utilities.
Definition AccountUtils.hpp:30