Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: handle std
Mutex
poisoning in a shim (#2872)
As tokio does not rely on poisoning, we can avoid always unwrapping when locking by handling the `PoisonError` in the Mutex shim. Signed-off-by: Zahari Dichev <zaharidichev@gmail.com>
- Loading branch information
1 parent
cf025ba
commit 4446606
Showing
17 changed files
with
127 additions
and
88 deletions.
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
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
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,31 @@ | ||
use std::sync::{self, MutexGuard, TryLockError}; | ||
|
||
/// Adapter for `std::Mutex` that removes the poisoning aspects | ||
// from its api | ||
#[derive(Debug)] | ||
pub(crate) struct Mutex<T: ?Sized>(sync::Mutex<T>); | ||
|
||
#[allow(dead_code)] | ||
impl<T> Mutex<T> { | ||
#[inline] | ||
pub(crate) fn new(t: T) -> Mutex<T> { | ||
Mutex(sync::Mutex::new(t)) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn lock(&self) -> MutexGuard<'_, T> { | ||
match self.0.lock() { | ||
Ok(guard) => guard, | ||
Err(p_err) => p_err.into_inner(), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn try_lock(&self) -> Option<MutexGuard<'_, T>> { | ||
match self.0.try_lock() { | ||
Ok(guard) => Some(guard), | ||
Err(TryLockError::Poisoned(p_err)) => Some(p_err.into_inner()), | ||
Err(TryLockError::WouldBlock) => None, | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.