Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Use fair unlocking when locking the guard tracking mutex guard #2066

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/locksmith/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ lazy_static! {

type GuardsMap = HashMap<ProcessUniqueId, GuardTracker>;

pub(crate) fn guards_guard<'a>() -> MutexGuard<'a, GuardsMap> {
fn guards_guard<'a>() -> MutexGuard<'a, GuardsMap> {
GUARDS
.try_lock_for(Duration::from_secs(20))
.expect("Guard-tracking mutex has been locked up for 20 seconds!")
}

pub(crate) fn with_guards_guard<'a, T, F: FnOnce(&mut MutexGuard<'a, GuardsMap>) -> T>(f: F) -> T {
let mut g = guards_guard();
let val = f(&mut g);
MutexGuard::unlock_fair(g);
val
}
15 changes: 9 additions & 6 deletions crates/locksmith/src/guard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{common::guards_guard, error::LockType, tracker::GuardTracker};
use crate::{common::with_guards_guard, error::LockType, tracker::GuardTracker};
use parking_lot::{MutexGuard, RwLockReadGuard, RwLockWriteGuard};
use snowflake::ProcessUniqueId;
use std::{
Expand All @@ -17,7 +17,9 @@ macro_rules! guard_struct {
impl<'a, T: ?Sized> $HcGuard<'a, T> {
pub fn new(inner: $Guard<'a, T>) -> Self {
let puid = ProcessUniqueId::new();
guards_guard().insert(puid, GuardTracker::new(puid, LockType::$lock_type));
with_guards_guard(|g| {
g.insert(puid, GuardTracker::new(puid, LockType::$lock_type))
});
Self {
puid,
inner: Some(inner),
Expand All @@ -28,9 +30,10 @@ macro_rules! guard_struct {
/// Add some context which will output in the case that this guard
/// lives to be an immortal
pub fn annotate<S: Into<String>>(self, annotation: S) -> Self {
guards_guard()
.entry(self.puid)
.and_modify(|g| g.annotation = Some(annotation.into()));
with_guards_guard(|g| {
g.entry(self.puid)
.and_modify(|g| g.annotation = Some(annotation.into()));
});
self
}

Expand All @@ -56,7 +59,7 @@ macro_rules! guard_struct {

impl<'a, T: ?Sized> Drop for $HcGuard<'a, T> {
fn drop(&mut self) {
guards_guard().remove(&self.puid);
with_guards_guard(|g| g.remove(&self.puid));
if self.fair_unlocking {
self._unlock_fair();
}
Expand Down
13 changes: 6 additions & 7 deletions crates/locksmith/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ Backtrace at the moment of guard creation follows:
}

lazy_static! {
static ref GUARDS: Mutex<HashMap<ProcessUniqueId, GuardTracker>> = Mutex::new(HashMap::new());
static ref PENDING_LOCKS: Mutex<HashMap<ProcessUniqueId, (LockType, Instant, Backtrace)>> =
Mutex::new(HashMap::new());
}
Expand All @@ -152,10 +151,10 @@ pub fn spawn_locksmith_guard_watcher() {
))
.spawn(move || loop {
let mut reports: Vec<(i64, String)> = {
guards_guard()
with_guards_guard(|g| g
.values_mut()
.filter_map(|gt| gt.report_and_update())
.collect();
.collect());
};
if reports.len() > 0 {
reports.sort_unstable_by_key(|(elapsed, _)| -*elapsed);
Expand Down Expand Up @@ -206,21 +205,21 @@ macro_rules! guard_struct {
impl<'a, T: ?Sized> $HcGuard<'a, T> {
pub fn new(inner: $Guard<'a, T>) -> Self {
let puid = ProcessUniqueId::new();
guards_guard().insert(puid, GuardTracker::new(puid, LockType::$lock_type));
with_guards_guard(|g| g.insert(puid, GuardTracker::new(puid, LockType::$lock_type)));
Self { puid, inner }
}

pub fn annotate<S: Into<String>>(self, annotation: S) -> Self {
guards_guard()
with_guards_guard(|g| g
.entry(self.puid)
.and_modify(|g| g.annotation = Some(annotation.into()));
.and_modify(|g| g.annotation = Some(annotation.into())));
self
}
}

impl<'a, T: ?Sized> Drop for $HcGuard<'a, T> {
fn drop(&mut self) {
guards_guard().remove(&self.puid);
with_guards_guard(|g| g.remove(&self.puid));
}
}
};
Expand Down
11 changes: 6 additions & 5 deletions crates/locksmith/src/tracker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
common::{
guards_guard, ACTIVE_GUARD_MIN_ELAPSED, ACTIVE_GUARD_NO_ACTIVITY_INTERVAL,
with_guards_guard, ACTIVE_GUARD_MIN_ELAPSED, ACTIVE_GUARD_NO_ACTIVITY_INTERVAL,
GUARD_WATCHER_POLL_INTERVAL, IMMORTAL_TIMEOUT,
},
error::LockType,
Expand Down Expand Up @@ -101,10 +101,11 @@ pub fn spawn_locksmith_guard_watcher() {
let mut inactive_for = Duration::from_millis(0);
loop {
let mut reports: Vec<(i64, String)> = {
guards_guard()
.values_mut()
.filter_map(|gt| gt.report_and_update())
.collect()
with_guards_guard(|g| {
g.values_mut()
.filter_map(|gt| gt.report_and_update())
.collect()
})
};
if reports.len() > 0 {
inactive_for = Duration::from_millis(0);
Expand Down