forked from driftluo/tentacle
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add parking lot compatable api
- Loading branch information
Showing
7 changed files
with
97 additions
and
7 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
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,9 @@ | ||
#![allow(dead_code)] | ||
|
||
#[cfg(feature = "parking_lot")] | ||
pub use parking_lot::{const_fair_mutex, const_mutex, const_rwlock, FairMutex, Mutex, RwLock}; | ||
#[cfg(not(feature = "parking_lot"))] | ||
pub mod native; | ||
|
||
#[cfg(not(feature = "parking_lot"))] | ||
pub use native::{Mutex, RwLock}; |
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,75 @@ | ||
use std::sync::{self, MutexGuard, RwLockReadGuard, RwLockWriteGuard, TryLockError}; | ||
|
||
/// Adapter for `std::Mutex` that removes the poisoning aspects | ||
// from its api | ||
#[derive(Debug)] | ||
pub struct Mutex<T: ?Sized>(sync::Mutex<T>); | ||
|
||
impl<T> Mutex<T> { | ||
#[inline] | ||
pub fn new(t: T) -> Mutex<T> { | ||
Mutex(sync::Mutex::new(t)) | ||
} | ||
|
||
#[inline] | ||
pub fn lock(&self) -> MutexGuard<'_, T> { | ||
match self.0.lock() { | ||
Ok(guard) => guard, | ||
Err(p_err) => p_err.into_inner(), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub 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, | ||
} | ||
} | ||
} | ||
|
||
/// Adapter for `std::RwLock` that removes the poisoning aspects | ||
// from its api | ||
pub struct RwLock<T: ?Sized>(sync::RwLock<T>); | ||
|
||
impl<T> RwLock<T> { | ||
#[inline] | ||
pub fn new(t: T) -> RwLock<T> { | ||
RwLock(sync::RwLock::new(t)) | ||
} | ||
|
||
#[inline] | ||
pub fn read(&self) -> RwLockReadGuard<'_, T> { | ||
match self.0.read() { | ||
Ok(guard) => guard, | ||
Err(p_err) => p_err.into_inner(), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> { | ||
match self.0.try_read() { | ||
Ok(guard) => Some(guard), | ||
Err(TryLockError::Poisoned(p_err)) => Some(p_err.into_inner()), | ||
Err(TryLockError::WouldBlock) => None, | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn write(&self) -> RwLockWriteGuard<'_, T> { | ||
match self.0.write() { | ||
Ok(guard) => guard, | ||
Err(p_err) => p_err.into_inner(), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> { | ||
match self.0.try_write() { | ||
Ok(guard) => Some(guard), | ||
Err(TryLockError::Poisoned(p_err)) => Some(p_err.into_inner()), | ||
Err(TryLockError::WouldBlock) => None, | ||
} | ||
} | ||
} |