-
Notifications
You must be signed in to change notification settings - Fork 1.6k
chore: Add mutex wrapper from clio #6447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kuznetsss
wants to merge
6
commits into
XRPLF:develop
Choose a base branch
from
kuznetsss:Add_mutex
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+448
−0
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
504485d
chore: Add mutex wrapper from clio
kuznetsss f674215
Make ci happy
kuznetsss 303ee00
Add tests for Mutex
kuznetsss af88bc1
Merge branch 'develop' into Add_mutex
kuznetsss 9e6e33d
Update src/tests/libxrpl/basics/Mutex.cpp
kuznetsss 43b927f
Add default init
kuznetsss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| //------------------------------------------------------------------------------ | ||
| /* | ||
| This file is part of clio: https://github.com/XRPLF/clio | ||
| Copyright (c) 2024-2026, the clio developers. | ||
|
|
||
| Permission to use, copy, modify, and distribute this software for any | ||
| purpose with or without fee is hereby granted, provided that the above | ||
| copyright notice and this permission notice appear in all copies. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| */ | ||
| //============================================================================== | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <mutex> | ||
| #include <type_traits> | ||
|
|
||
| namespace xrpl { | ||
|
|
||
| template <typename ProtectedDataType, typename MutextType> | ||
kuznetsss marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| class Mutex; | ||
|
|
||
| /** | ||
| * @brief A lock on a mutex that provides access to the protected data. | ||
| * | ||
| * @tparam ProtectedDataType data type to hold | ||
| * @tparam LockType type of lock | ||
| * @tparam MutexType type of mutex | ||
| */ | ||
| template <typename ProtectedDataType, template <typename...> typename LockType, typename MutexType> | ||
| class Lock | ||
| { | ||
| LockType<MutexType> lock_; | ||
| ProtectedDataType& data_; | ||
|
|
||
| public: | ||
| /** @cond */ | ||
| ProtectedDataType const& | ||
| operator*() const | ||
| { | ||
| return data_; | ||
| } | ||
|
|
||
| ProtectedDataType& | ||
| operator*() | ||
| { | ||
| return data_; | ||
| } | ||
|
|
||
| ProtectedDataType const& | ||
| get() const | ||
| { | ||
| return data_; | ||
| } | ||
|
|
||
| ProtectedDataType& | ||
| get() | ||
| { | ||
| return data_; | ||
| } | ||
|
|
||
| ProtectedDataType const* | ||
| operator->() const | ||
| { | ||
| return &data_; | ||
| } | ||
|
|
||
| ProtectedDataType* | ||
| operator->() | ||
| { | ||
| return &data_; | ||
| } | ||
|
|
||
| operator LockType<MutexType>&() | ||
| { | ||
| return lock_; | ||
| } | ||
| /** @endcond */ | ||
|
|
||
| private: | ||
| friend class Mutex<std::remove_const_t<ProtectedDataType>, MutexType>; | ||
|
|
||
| Lock(MutexType& mutex, ProtectedDataType& data) : lock_(mutex), data_(data) | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @brief A container for data that is protected by a mutex. Inspired by Mutex in Rust. | ||
| * | ||
| * @tparam ProtectedDataType data type to hold | ||
| * @tparam MutexType type of mutex | ||
| */ | ||
| template <typename ProtectedDataType, typename MutexType = std::mutex> | ||
| class Mutex | ||
| { | ||
| mutable MutexType mutex_; | ||
| ProtectedDataType data_; | ||
|
|
||
| public: | ||
| Mutex() = default; | ||
|
|
||
| /** | ||
| * @brief Construct a new Mutex object with the given data | ||
| * | ||
| * @param data The data to protect | ||
| */ | ||
| explicit Mutex(ProtectedDataType data) : data_(std::move(data)) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @brief Make a new Mutex object with the given data | ||
| * | ||
| * @tparam Args The types of the arguments to forward to the constructor of the protected data | ||
| * @param args The arguments to forward to the constructor of the protected data | ||
| * @return The Mutex object that protects the given data | ||
| */ | ||
| template <typename... Args> | ||
| static Mutex | ||
| make(Args&&... args) | ||
| { | ||
| return Mutex{ProtectedDataType{std::forward<Args>(args)...}}; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Lock the mutex and get a lock object allowing access to the protected data | ||
| * | ||
| * @tparam LockType The type of lock to use | ||
| * @return A lock on the mutex and a reference to the protected data | ||
| */ | ||
| template <template <typename...> typename LockType = std::lock_guard> | ||
| Lock<ProtectedDataType const, LockType, MutexType> | ||
| lock() const | ||
| { | ||
| return {mutex_, data_}; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Lock the mutex and get a lock object allowing access to the protected data | ||
| * | ||
| * @tparam LockType The type of lock to use | ||
| * @return A lock on the mutex and a reference to the protected data | ||
| */ | ||
| template <template <typename...> typename LockType = std::lock_guard> | ||
| Lock<ProtectedDataType, LockType, MutexType> | ||
| lock() | ||
| { | ||
| return {mutex_, data_}; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace xrpl | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.