Skip to content

Commit f29d85c

Browse files
committed
Deduplicate a lot of code
1 parent e57f340 commit f29d85c

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

trin-core/src/lib.rs

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,28 @@ pub mod utils;
1010
pub const ACQUIRE_TIMEOUT_MS: u64 = 100;
1111
pub const HOLD_TIMEOUT_MS: u64 = 100;
1212

13-
pub struct TimedRwReadGuard<'a, T: ?Sized> {
14-
pub inner: tokio::sync::RwLockReadGuard<'a, T>,
13+
pub struct TimedGuard<T> {
14+
pub inner: T,
1515
pub acquisition_line: u32,
1616
pub acquisition_file: &'static str,
1717
pub acquisition_time: std::time::Instant,
1818
pub sleep_task: tokio::task::JoinHandle<()>,
1919
}
2020

21-
async fn sleep_then_log(file: &'static str, line: u32) {
22-
tokio::time::sleep(std::time::Duration::from_millis(HOLD_TIMEOUT_MS)).await;
23-
log::warn!(
24-
"[{}:{}] lock held for over {}ms, not yet released",
25-
file,
26-
line,
27-
HOLD_TIMEOUT_MS.to_string()
28-
);
29-
}
30-
31-
impl<'a, T: ?Sized> TimedRwReadGuard<'a, T> {
21+
impl<T> TimedGuard<T> {
3222
pub async fn new(
33-
inner: tokio::sync::RwLockReadGuard<'a, T>,
23+
inner: T,
3424
acquisition_line: u32,
3525
acquisition_file: &'static str,
36-
) -> TimedRwReadGuard<'a, T> {
26+
) -> TimedGuard<T> {
3727
let now = std::time::Instant::now();
3828
let move_line = acquisition_line;
3929
let move_file = acquisition_file;
4030
let handle = tokio::spawn(async move {
4131
sleep_then_log(move_file, move_line).await;
4232
});
4333

44-
TimedRwReadGuard {
34+
TimedGuard {
4535
inner,
4636
acquisition_line,
4737
acquisition_file,
@@ -51,15 +41,21 @@ impl<'a, T: ?Sized> TimedRwReadGuard<'a, T> {
5141
}
5242
}
5343

54-
impl<'a, T> std::ops::Deref for TimedRwReadGuard<'a, T> {
55-
type Target = tokio::sync::RwLockReadGuard<'a, T>;
44+
impl<T> std::ops::Deref for TimedGuard<T> {
45+
type Target = T;
5646

5747
fn deref(&self) -> &Self::Target {
5848
&self.inner
5949
}
6050
}
6151

62-
impl<'a, T: ?Sized> Drop for TimedRwReadGuard<'a, T> {
52+
impl<T> std::ops::DerefMut for TimedGuard<T> {
53+
fn deref_mut(&mut self) -> &mut Self::Target {
54+
&mut self.inner
55+
}
56+
}
57+
58+
impl<T> Drop for TimedGuard<T> {
6359
fn drop(&mut self) {
6460
self.sleep_task.abort();
6561
let held_for = self.acquisition_time.elapsed().as_millis();
@@ -74,9 +70,19 @@ impl<'a, T: ?Sized> Drop for TimedRwReadGuard<'a, T> {
7470
}
7571
}
7672

73+
async fn sleep_then_log(file: &'static str, line: u32) {
74+
tokio::time::sleep(std::time::Duration::from_millis(HOLD_TIMEOUT_MS)).await;
75+
log::warn!(
76+
"[{}:{}] lock held for over {}ms, not yet released",
77+
file,
78+
line,
79+
HOLD_TIMEOUT_MS.to_string()
80+
);
81+
}
82+
7783
#[macro_export]
78-
macro_rules! rw_read {
79-
($lock_name:expr) => {{
84+
macro_rules! inner_rw {
85+
($meth: ident, $lock_name:expr) => {{
8086
let acquire_timeout = std::time::Duration::from_millis($crate::ACQUIRE_TIMEOUT_MS);
8187
let sleep = tokio::time::sleep(acquire_timeout);
8288
tokio::pin!(sleep);
@@ -95,7 +101,7 @@ macro_rules! rw_read {
95101
);
96102
did_log = true;
97103
}
98-
guard = $lock_name.read() => {
104+
guard = $lock_name.$meth() => {
99105
if did_log {
100106
let wait_time = now.elapsed().as_millis();
101107
log::warn!(
@@ -106,7 +112,7 @@ macro_rules! rw_read {
106112
);
107113
}
108114

109-
let wrapped = $crate::TimedRwReadGuard::new(
115+
let wrapped = $crate::TimedGuard::new(
110116
guard, std::line!(), std::file!(),
111117
).await;
112118
break wrapped;
@@ -119,24 +125,13 @@ macro_rules! rw_read {
119125
#[macro_export]
120126
macro_rules! rw_write {
121127
($lock_name:expr) => {{
122-
let acquire_timeout = std::time::Duration::from_millis($crate::ACQUIRE_TIMEOUT_MS);
123-
let sleep = tokio::time::sleep(acquire_timeout);
124-
tokio::pin!(sleep);
128+
$crate::inner_rw!(write, $lock_name)
129+
}}
130+
}
125131

126-
loop {
127-
tokio::select! {
128-
_ = &mut sleep, if !sleep.is_elapsed() => {
129-
log::warn!(
130-
"[{}:{}] took more than {} ms to acquire lock",
131-
std::file!(),
132-
std::line!(),
133-
$crate::ACQUIRE_TIMEOUT_MS,
134-
);
135-
}
136-
guard = $lock_name.write() => {
137-
break guard;
138-
}
139-
}
140-
}
141-
}};
132+
#[macro_export]
133+
macro_rules! rw_read {
134+
($lock_name:expr) => {{
135+
$crate::inner_rw!(read, $lock_name)
136+
}}
142137
}

0 commit comments

Comments
 (0)