Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Mutex.hpp
1#pragma once
2
3#include <mutex>
4#include <type_traits>
5
6namespace util {
7
8template <typename ProtectedDataType, typename MutextType>
9class Mutex;
10
18template <typename ProtectedDataType, template <typename...> typename LockType, typename MutexType>
19class Lock {
20 LockType<MutexType> lock_;
21 ProtectedDataType& data_;
22
23public:
25 ProtectedDataType const&
26 operator*() const
27 {
28 return data_;
29 }
30
31 ProtectedDataType&
32 operator*()
33 {
34 return data_;
35 }
36
37 ProtectedDataType const&
38 get() const
39 {
40 return data_;
41 }
42
43 ProtectedDataType&
44 get()
45 {
46 return data_;
47 }
48
49 ProtectedDataType const*
50 operator->() const
51 {
52 return &data_;
53 }
54
55 ProtectedDataType*
56 operator->()
57 {
58 return &data_;
59 }
60
61 operator LockType<MutexType>&()
62 {
63 return lock_;
64 }
66
67private:
68 friend class Mutex<std::remove_const_t<ProtectedDataType>, MutexType>;
69
70 Lock(MutexType& mutex, ProtectedDataType& data) : lock_(mutex), data_(data)
71 {
72 }
73};
74
81template <typename ProtectedDataType, typename MutexType = std::mutex>
82class Mutex {
83 mutable MutexType mutex_;
84 ProtectedDataType data_;
85
86public:
87 Mutex() = default;
88
94 explicit Mutex(ProtectedDataType data) : data_(std::move(data))
95 {
96 }
97
105 template <typename... Args>
106 static Mutex
107 make(Args&&... args)
108 {
109 return Mutex{ProtectedDataType{std::forward<Args>(args)...}};
110 }
111
118 template <template <typename...> typename LockType = std::lock_guard>
120 lock() const
121 {
122 return {mutex_, data_};
123 }
124
131 template <template <typename...> typename LockType = std::lock_guard>
134 {
135 return {mutex_, data_};
136 }
137};
138
139} // namespace util
A lock on a mutex that provides access to the protected data.
Definition Mutex.hpp:19
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:82
Lock< ProtectedDataType, LockType, MutexType > lock()
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:133
static Mutex make(Args &&... args)
Make a new Mutex object with the given data.
Definition Mutex.hpp:107
Mutex(ProtectedDataType data)
Construct a new Mutex object with the given data.
Definition Mutex.hpp:94
Lock< ProtectedDataType const, LockType, MutexType > lock() const
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:120
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:56
This namespace contains various utilities.
Definition AccountUtils.hpp:11