Sponsored Content
Skip to content

feat: Add mutex wrapper from clio - #6447

Merged
bthomee merged 16 commits into
XRPLF:developfrom
kuznetsss:Add_mutex
Mar 9, 2026
Merged

feat: Add mutex wrapper from clio#6447
bthomee merged 16 commits into
XRPLF:developfrom
kuznetsss:Add_mutex

Conversation

@kuznetsss

@kuznetsss kuznetsss commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

High Level Overview of Change

This PR adds a mutex wrapper copied from clio. This wrapper makes a mutex attached to the data it protects which improves safety and readability.

Context of Change

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Performance (increase or change in throughput and/or latency)
  • Tests (you added tests for code that already exists, or your new feature included in this PR)
  • Documentation update
  • [] Chore (no impact to binary, e.g. .gitignore, formatting, dropping support for older tooling)
  • Release

API Impact

  • Public API: New feature (new methods and/or new fields)
  • Public API: Breaking change (in general, breaking changes should only impact the next api_version)
  • libxrpl change (any change that may affect libxrpl or dependents of libxrpl)
  • Peer protocol change (must be backward compatible or bump the peer protocol version)

Comment thread src/libxrpl/basics/Mutex.hpp Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new mutex+data wrapper (ported from clio) intended to bundle protected data with its mutex and provide an RAII lock object that grants scoped access to the data.

Changes:

  • Add xrpl::Mutex<T, MutexType> container that owns T and a mutex.
  • Add xrpl::Lock<T, LockType, MutexType> to hold the lock and provide *, ->, and get() accessors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/xrpl/basics/Mutex.hpp
Comment thread src/libxrpl/basics/Mutex.hpp Outdated
Comment thread include/xrpl/basics/Mutex.hpp
Comment thread include/xrpl/basics/Mutex.hpp
Copilot AI review requested due to automatic review settings February 27, 2026 15:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented Feb 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.8%. Comparing base (1ee0567) to head (c3db38a).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           develop   #6447   +/-   ##
=======================================
  Coverage     79.8%   79.8%           
=======================================
  Files          861     862    +1     
  Lines        67857   67876   +19     
  Branches      7553    7556    +3     
=======================================
+ Hits         54169   54185   +16     
- Misses       13688   13691    +3     
Files with missing lines Coverage Δ
include/xrpl/basics/Mutex.hpp 100.0% <100.0%> (ø)

... and 5 files with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vlntb vlntb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Let's resolve the question about the unit test, and I'm happy to approve.

Copilot AI review requested due to automatic review settings March 2, 2026 17:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/xrpl/basics/Mutex.hpp
Comment thread include/xrpl/basics/Mutex.hpp
Comment thread src/tests/libxrpl/basics/Mutex.cpp
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 2, 2026 17:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/xrpl/basics/Mutex.hpp Outdated
@kuznetsss kuznetsss added the Ready to merge *PR author* thinks it's ready to merge. Has passed code review. Perf sign-off may still be required. label Mar 3, 2026
Copilot AI review requested due to automatic review settings March 3, 2026 14:21

@godexsoft godexsoft left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:rage1:

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/xrpl/basics/Mutex.hpp
Comment thread include/xrpl/basics/Mutex.hpp
Comment thread src/tests/libxrpl/basics/Mutex.cpp Outdated
Comment thread src/tests/libxrpl/basics/Mutex.cpp
Comment thread include/xrpl/basics/Mutex.hpp Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 3, 2026 14:50
kuznetsss and others added 2 commits March 3, 2026 14:51
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/xrpl/basics/Mutex.hpp
Comment thread include/xrpl/basics/Mutex.hpp
Comment thread include/xrpl/basics/Mutex.hpp

@ximinez ximinez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a "chore". It is a new feature. Just because it's not user-facing doesn't mean it's not new.

Comment on lines +217 to +219
std::unique_lock<std::mutex>& ul = lock;
ul.unlock();
ul.lock();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this leave the data vulnerable to modification outside of lock?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that it is possible to get reference to the internal lock, unlock it and change underlying data?

It is not perfectly safe, but having this conversion we are able to use lock with condition variables. We use it in clio only in one place:

       auto lock = data_.lock<std::unique_lock>();
       cv_.wait(lock, [&] { return stopping_ or not lock->forwardLoadQueue.empty(); });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that it is possible to get reference to the internal lock, unlock it and change underlying data?

Yeah, basically.

Like in the test here, you could write

    std::unique_lock<std::mutex>& ul = lock;
    ul.unlock();
    ul.lock(); 
    EXPECT_EQ(*lock, 2);
    ul.unlock();
	*lock = 4;
    ul.lock(); 
    EXPECT_EQ(*lock, 4);

And I'm assuming it would "pass", even though it's clearly not what we want.

What do you think about using some template magic to check owns_lock if the LockType class defines one, and throwing if it's false everywhere you return data? It's probably overkill, and it's easy to defeat by grabbing a reference before releasing the lock, but it would catch accidental usages.

It is not perfectly safe, but having this conversion we are able to use lock with condition variables. We use it in clio only in one place:

       auto lock = data_.lock<std::unique_lock>();
       cv_.wait(lock, [&] { return stopping_ or not lock->forwardLoadQueue.empty(); });

That is an excellent use case, though!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding a check for owns_lock will slightly impact performance and as you said, it will not completely prevent unsafe usage of this class.
The best solution probably would be to remove the conversion operator and add a wrap for condition variable (maybe not in this PR but when it will be needed). What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding a check for owns_lock will slightly impact performance and as you said, it will not completely prevent unsafe usage of this class. The best solution probably would be to remove the conversion operator and add a wrap for condition variable (maybe not in this PR but when it will be needed). What do you think?

That's a great idea, and I'm fine with doing it in a follow-up PR.

Comment thread src/tests/libxrpl/basics/Mutex.cpp Outdated
Comment thread include/xrpl/basics/Mutex.hpp
@kuznetsss kuznetsss changed the title chore: Add mutex wrapper from clio feat: Add mutex wrapper from clio Mar 4, 2026
@kuznetsss
kuznetsss requested a review from ximinez March 5, 2026 11:50
@bthomee
bthomee enabled auto-merge (squash) March 9, 2026 15:51
@bthomee
bthomee merged commit e2290b1 into XRPLF:develop Mar 9, 2026
3 checks passed
@mvadari mvadari added this to the 3.2.0 milestone May 20, 2026
beartec-jpg pushed a commit to beartec-jpg/FalconLedger that referenced this pull request Jun 1, 2026
This change adds a mutex wrapper copied from clio. The wrapper attaches a mutex to the data it protects, which improves safety and readability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready to merge *PR author* thinks it's ready to merge. Has passed code review. Perf sign-off may still be required.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants