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 }
79
80 operator LockType<MutexType>&()
81 {
82 return lock_;
83 }
86private:
87 friend class Mutex<std::remove_const_t<ProtectedDataType>, MutexType>;
88
89 Lock(MutexType& mutex, ProtectedDataType& data) : lock_(mutex), data_(data)
90 {
91 }
92};
93
100template <typename ProtectedDataType, typename MutexType = std::mutex>
101class Mutex {
102 mutable MutexType mutex_;
103 ProtectedDataType data_;
104
105public:
106 Mutex() = default;
107
113 explicit Mutex(ProtectedDataType data) : data_(std::move(data))
114 {
115 }
116
124 template <typename... Args>
125 static Mutex
126 make(Args&&... args)
127 {
128 return Mutex{ProtectedDataType{std::forward<Args>(args)...}};
129 }
130
137 template <template <typename...> typename LockType = std::lock_guard>
139 lock() const
140 {
141 return {mutex_, data_};
142 }
143
150 template <template <typename...> typename LockType = std::lock_guard>
153 {
154 return {mutex_, data_};
155 }
156};
157
158} // 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:101
Lock< ProtectedDataType, LockType, MutexType > lock()
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:152
static Mutex make(Args &&... args)
Make a new Mutex object with the given data.
Definition Mutex.hpp:126
Mutex(ProtectedDataType data)
Construct a new Mutex object with the given data.
Definition Mutex.hpp:113
Lock< ProtectedDataType const, LockType, MutexType > lock() const
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:139
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70
This namespace contains various utilities.
Definition AccountUtils.hpp:30