Skip to content

Commit 021a9d6

Browse files
committed
attempt!
1 parent dbea547 commit 021a9d6

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 dirtied? Let's stop here.
244250
if self.is_dirty() {
245-
return Err(CrossProcessLockError::LockDirtied);
251+
return Err(CrossProcessLockError::LockDirtied(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_dirty.store(true, Ordering::SeqCst);
282290

283-
return Err(CrossProcessLockError::LockDirtied);
291+
return Err(CrossProcessLockError::LockDirtied(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 dirtied, i.e. it means another process took the lock
475484
/// while this process was holding it.
476485
#[error("a lock has been dirtied")]
477-
LockDirtied,
486+
LockDirtied(CrossProcessLockGuard),
478487

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

0 commit comments

Comments
 (0)