-
Notifications
You must be signed in to change notification settings - Fork 632
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
refactor: clean up busy waiting in prefetcher blocking get #8215
Merged
near-bulldozer
merged 14 commits into
near:master
from
pugachAG:prefetcher-blocking-get
Dec 22, 2022
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f55cded
Initial implementation
pugachAG 0627abd
Minor updates
pugachAG 3b9b27a
Avoid passing mutex guard to notify_slots_update
pugachAG 446b32e
Use explicit guard variable
pugachAG cebbd82
Introduce Monitor
pugachAG 53412e7
Remove tmp guard
pugachAG f861cd7
fmt
pugachAG 8e4a161
Reorder use
pugachAG cb59478
from_micros -> from_millis
pugachAG 2ec4546
Simplify test
pugachAG ceec97c
Use prefetch_staging_area2 async
pugachAG 965a75b
Fix comment
pugachAG 04cd64c
Fix another comment
pugachAG 9fc336f
Merge branch 'master' into prefetcher-blocking-get
pugachAG 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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
use std::sync::{Condvar, Mutex, MutexGuard}; | ||
|
||
const POISONED_LOCK_ERR: &str = "The lock was poisoned."; | ||
|
||
/// A convenience wrapper around a SharedMutex and a Condvar. | ||
/// | ||
/// It enables blocking while waiting for the underlying value to be updated. | ||
/// The implementation ensures that any modification results in all blocked | ||
/// threads being notified. | ||
pub(crate) struct Monitor<T> { | ||
cvar: Condvar, | ||
mutex: Mutex<T>, | ||
} | ||
|
||
pub(crate) struct MonitorReadGuard<'a, T> { | ||
guard: MutexGuard<'a, T>, | ||
} | ||
|
||
pub(crate) struct MonitorWriteGuard<'a, T> { | ||
guard: MutexGuard<'a, T>, | ||
cvar: &'a Condvar, | ||
} | ||
|
||
impl<T> Monitor<T> { | ||
pub fn new(t: T) -> Self { | ||
Self { mutex: Mutex::new(t), cvar: Condvar::new() } | ||
} | ||
|
||
pub fn lock(&self) -> MonitorReadGuard<'_, T> { | ||
let guard = self.mutex.lock().expect(POISONED_LOCK_ERR); | ||
MonitorReadGuard { guard } | ||
} | ||
|
||
pub fn lock_mut(&self) -> MonitorWriteGuard<'_, T> { | ||
let guard = self.mutex.lock().expect(POISONED_LOCK_ERR); | ||
MonitorWriteGuard { guard, cvar: &self.cvar } | ||
} | ||
|
||
pub fn wait<'a>(&'a self, guard: MonitorReadGuard<'a, T>) -> MonitorReadGuard<'a, T> { | ||
let guard = self.cvar.wait(guard.guard).expect(POISONED_LOCK_ERR); | ||
MonitorReadGuard { guard } | ||
} | ||
} | ||
|
||
impl<T> Deref for MonitorReadGuard<'_, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.guard.deref() | ||
} | ||
} | ||
|
||
impl<T> Deref for MonitorWriteGuard<'_, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.guard.deref() | ||
} | ||
} | ||
|
||
impl<T> DerefMut for MonitorWriteGuard<'_, T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
self.guard.deref_mut() | ||
} | ||
} | ||
|
||
impl<T> Drop for MonitorWriteGuard<'_, T> { | ||
fn drop(&mut self) { | ||
self.cvar.notify_all(); | ||
} | ||
} |
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
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.
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.
Not saying that rolling out our own implementation is problematic but curious why we didn't use something off the shelf e.g. https://github.com/reem/rust-shared-mutex.
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.
I've considered considered using
shared-mutex
and actually myMonitor
implementation is inspired by it.Unfortunately it doesn't provide the API we need here, in particular we want
SharedMutexWriteGuard
to notify condvar when dropped (see this comment).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.
Got it, thanks for the explanation.