Skip to content

Conversation

@vasild
Copy link
Contributor

@vasild vasild commented Jul 22, 2022

Summary:

  • Reduce 4 of the MaybeCheckNotHeld() definitions to 2 by using a template.
  • Remove unused template parameter from ::UniqueLock.
  • Use MutexType instead of Mutex for a template parameter name to avoid overlap/confusion with the Mutex class.
  • Rename AnnotatedMixin::UniqueLock to AnnotatedMixin::unique_lock to avoid overlap/confusion with the global UniqueLock and for consistency with UniqueLock::reverse_lock.

The first commit sync: simplify MaybeCheckNotHeld() definitions by using a template is also part of #25390

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 23, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25390 (sync: introduce a thread-safe smart container and use it to remove a bunch of "GlobalMutex"es by vasild)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@vasild
Copy link
Contributor Author

vasild commented Aug 10, 2022

332cfac809...19fce0321b: address suggestion

Reduce 4 of the `MaybeCheckNotHeld()` definitions to 2 by using a
template. This also makes the function usable for other
[BasicLockable](https://en.cppreference.com/w/cpp/named_req/BasicLockable)
types.
The template parameter `typename Base = typename Mutex::UniqueLock` is
not used, so remove it. Use internally defined type `Base` to avoid
repetitions of `Mutex::UniqueLock`.
@vasild
Copy link
Contributor Author

vasild commented Sep 14, 2022

19fce0321b...f5b529369c: rebase due to conflict

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK f5b529369c8e5dee655ed559fd9583947188b18c. This makes nice simplifications and gets rid of some confusing shadowing, but I would suggest an alternative to the last commit.

I don't think the last commit "sync: rename the global UniqueLock to DebugLock" (f5b529369c8e5dee655ed559fd9583947188b18c) is good because it gets rid of the naming consistency between standard c++ thread primitives and our thread primitives. The standard c++ mutex class is called std::mutex while ours is called Mutex. The standard c++ recursive mutex class is called std::recursive_mutex while ours is called RecursiveMutex. Than standard c++ lock class is called std::unique_lock, so I think ours should be called UniqueLock, not DebugLock. It is true that the main features our lock class adds currently are related to debugging (though it also adds support for reverse locking), but we are not naming our custom classes after what features they add, otherwise we would be calling Mutex AnnotatedMutex etc. And I don't think it is a good idea to name our locking primitives after the particular features they add, because we should have flexibility to add and remove features from our locking primitives in the future.

Another benefit of dropping the last commit is that it would confine changes in this PR to sync.h and not change outside code in src/node/interfaces.cpp.

I do think one benefit of the last commit is that disambiguates global UniqueLock from AnnotatedMixin::UniqueLock. But I would fix this by renaming AnnotatedMixin::UniqueLock to AnnotatedMixin::unique_lock. I think AnnotatedMixin::unique_lock name makes sense because it is a typedef for std::unique_lock, and would also be consistent with current UniqueLock::reverse_lock.

Use `MutexType` instead of `Mutex` for the template parameter of
`UniqueLock` because there is already a class named `Mutex` and the
naming overlap is confusing. `MutexType` is used elsewhere in `sync.h`.
Use `UniqueLock` directly. Type deduction works just fine from the first
argument to the constructor of `UniqueLock`, so there is no need to
repeat

```cpp
UniqueLock<typename std::remove_reference<typename std::remove_pointer<decltype(cs)>::type>::type>
```

five times in the `LOCK` macros. Just `UniqueLock` suffices.
This avoids confusion with the global `UniqueLock` and the snake case
is consistent with `UniqueLock::reverse_lock.
@vasild
Copy link
Contributor Author

vasild commented Oct 10, 2022

f5b529369c...75c3f9f880: @ryanofsky, done!

@fanquake
Copy link
Member

done!

@vasild looks like you now also need to update the PR description?

@vasild
Copy link
Contributor Author

vasild commented Oct 10, 2022

@fanquake, updated, thanks!

Copy link
Contributor

@aureleoules aureleoules left a comment

Choose a reason for hiding this comment

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

ACK 75c3f9f - LGTM
I verified all commits compile and unit tests pass.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK 75c3f9f. Nice cleanups! Just suggested changes since last review: keeping UniqueLock name and fixing a missed rename in a code comment

@fanquake fanquake merged commit 2e77dff into bitcoin:master Oct 11, 2022
@vasild vasild deleted the improve_sync.h branch October 11, 2022 07:27
@ajtowns
Copy link
Contributor

ajtowns commented Oct 11, 2022

Post-merge ACK 75c3f9f

Nice cleanup, especially with the final simplifications

@jonatack
Copy link
Member

Post-merge ACK 75c3f9f

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

Post-merge ACK 75c3f9f, I have reviewed the code and it looks OK.

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Oct 23, 2022
75c3f9f sync: rename AnnotatedMixin::UniqueLock to AnnotatedMixin::unique_lock (Vasil Dimov)
8d9ee8e sync: remove DebugLock alias template (Vasil Dimov)
4b2e167 sync: avoid confusing name overlap (Mutex) (Vasil Dimov)
9d7ae4b sync: remove unused template parameter from ::UniqueLock (Vasil Dimov)
11c190e sync: simplify MaybeCheckNotHeld() definitions by using a template (Vasil Dimov)

Pull request description:

  Summary:

  * Reduce 4 of the `MaybeCheckNotHeld()` definitions to 2 by using a template.
  * Remove unused template parameter from `::UniqueLock`.
  * Use `MutexType` instead of `Mutex` for a template parameter name to avoid overlap/confusion with the `Mutex` class.
  * Rename `AnnotatedMixin::UniqueLock` to `AnnotatedMixin::unique_lock` to avoid overlap/confusion with the global `UniqueLock` and for consistency with `UniqueLock::reverse_lock`.

  The first commit `sync: simplify MaybeCheckNotHeld() definitions by using a template` is also part of bitcoin#25390

ACKs for top commit:
  aureleoules:
    ACK 75c3f9f - LGTM
  ryanofsky:
    Code review ACK 75c3f9f. Nice cleanups! Just suggested changes since last review: keeping UniqueLock name and fixing a missed rename in a code comment

Tree-SHA512: ec261f6a444bdfe4f06e844b57b3606fdd9b2f842647cae15266d9729970d87585c808d482fbba0b31c33a4aa03527c36e282c92b28d9052711f75a7048c96f1
@bitcoin bitcoin locked and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants