This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Conversation
This file contains 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
It looks like @mattrutherford signed our Contributor License Agreement. 👍 Many thanks, Parity Technologies CLA Bot |
tomusdrw
approved these changes
Nov 29, 2018
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
LenCachingMutex can be used in place of a Mutex, when working with collections, or other types with a len() method. When the Guard is dropped, the value returned from len() is stored into an AtomicUsize and can be queried using load_len() without needing to lock the Mutex.
3f7627f
to
545ab0d
Compare
ordian
approved these changes
Dec 4, 2018
sorpaas
reviewed
Dec 5, 2018
self.len.load(Ordering::SeqCst) | ||
} | ||
|
||
pub fn lock(&self) -> Guard<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remember to add docs for public functions in the future!
niklasad1
pushed a commit
that referenced
this pull request
Dec 16, 2018
New util LenCachingMutex can be used in place of a Mutex, when working with collections, or other types with a len() method. When the Guard is dropped, the value returned from len() is stored into an AtomicUsize and can be queried using load_len() without needing to lock the Mutex. Implementations for ```Vec``` and ```VecDeque```. Now used in [Verification](https://github.com/paritytech/parity-ethereum/blob/4ded4181a651fe9c26850f908e4ab63bf952cab6/ethcore/src/verification/queue/mod.rs#L196) so that calls to ```VerificationQueue.queue_info()``` no longer require locks.
insipx
pushed a commit
to insipx/parity-ethereum
that referenced
this pull request
Dec 17, 2018
New util LenCachingMutex can be used in place of a Mutex, when working with collections, or other types with a len() method. When the Guard is dropped, the value returned from len() is stored into an AtomicUsize and can be queried using load_len() without needing to lock the Mutex. Implementations for ```Vec``` and ```VecDeque```. Now used in [Verification](https://github.com/paritytech/parity-ethereum/blob/4ded4181a651fe9c26850f908e4ab63bf952cab6/ethcore/src/verification/queue/mod.rs#L196) so that calls to ```VerificationQueue.queue_info()``` no longer require locks.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
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.
While working on another issue I developed a way to query the size of a collection held in a
Mutex
without requiring a lock. Ultimately I found a a different way to solve the issue without needing this, but it may be useful anyway. I've included a commit to show it used inVerification
. If another thread has the lock whileload_len()
is called, the cached value may not be accurate. However this is often not too different to normal operation - stored len() may be stale at any point after the Guard is dropped.