Skip to content

Commit 8140fdc

Browse files
committed
attempt!
1 parent 67b4df9 commit 8140fdc

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

crates/matrix-sdk-common/src/cross_process_lock.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ pub struct CrossProcessLockGuard {
117117
num_holders: Arc<AtomicU32>,
118118
}
119119

120+
impl CrossProcessLockGuard {
121+
fn new(num_holders: Arc<AtomicU32>) -> Self {
122+
Self { num_holders }
123+
}
124+
}
125+
120126
impl Drop for CrossProcessLockGuard {
121127
fn drop(&mut self) {
122128
self.num_holders.fetch_sub(1, Ordering::SeqCst);
@@ -242,7 +248,9 @@ where
242248

243249
// The lock is already poisoned? Let's stop here.
244250
if self.is_poisoned() {
245-
return Err(CrossProcessLockError::LockPoisoned);
251+
return Err(CrossProcessLockError::LockPoisoned(CrossProcessLockGuard::new(
252+
self.num_holders.clone(),
253+
)));
246254
}
247255

248256
// If another thread obtained the lock, make sure to only superficially increase
@@ -280,7 +288,9 @@ where
280288
);
281289
self.is_poisoned.store(true, Ordering::SeqCst);
282290

283-
return Err(CrossProcessLockError::LockPoisoned);
291+
return Err(CrossProcessLockError::LockPoisoned(CrossProcessLockGuard::new(
292+
self.num_holders.clone(),
293+
)));
284294
}
285295

286296
// This was the same generation, no problem.
@@ -404,8 +414,7 @@ where
404414

405415
self.num_holders.fetch_add(1, Ordering::SeqCst);
406416

407-
let guard = CrossProcessLockGuard { num_holders: self.num_holders.clone() };
408-
Ok(Some(guard))
417+
Ok(Some(CrossProcessLockGuard::new(self.num_holders.clone())))
409418
}
410419

411420
/// Attempt to take the lock, with exponential backoff if the lock has
@@ -474,7 +483,7 @@ pub enum CrossProcessLockError {
474483
/// The lock has been poisoned, it means an holder took it before the lock
475484
/// was acquired or renewed.
476485
#[error("a lock has been poisoned")]
477-
LockPoisoned,
486+
LockPoisoned(CrossProcessLockGuard),
478487

479488
#[error(transparent)]
480489
#[cfg(not(target_family = "wasm"))]

0 commit comments

Comments
 (0)