-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the
lock_api
dependency optional. (#11)
* Make the `lock_api` dependency optional. Without `lock_api`, this crate only provides a small number of types such as `RawMutex` and `RawRwLock`, but if someone needs just those, it's reasonable to provide them without depending on `lock_api`. * Change `RawCondvar` to a `pub use`. Using a `pub use` instead of a `pub type` alias gives it better rustdoc documentation.
- Loading branch information
1 parent
0c6475b
commit 884a549
Showing
5 changed files
with
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//! Polyfills for `lock_api` traits for when we don't have the real `lock_api` | ||
//! crate. | ||
/// Polyfill for [`lock_api::GuardNoSend`]. | ||
/// | ||
/// [`lock_api::GuardNoSend`]: https://docs.rs/lock_api/*/lock_api/struct.GuardNoSend.html | ||
pub struct GuardNoSend(()); | ||
|
||
/// Polyfill for [`lock_api::RawMutex`]. | ||
/// | ||
/// [`lock_api::RawMutex`]: https://docs.rs/lock_api/*/lock_api/trait.RawMutex.html | ||
pub unsafe trait RawMutex { | ||
type GuardMarker; | ||
|
||
const INIT: Self; | ||
|
||
fn lock(&self); | ||
fn try_lock(&self) -> bool; | ||
unsafe fn unlock(&self); | ||
|
||
#[inline] | ||
fn is_locked(&self) -> bool { | ||
let acquired_lock = self.try_lock(); | ||
if acquired_lock { | ||
unsafe { | ||
self.unlock(); | ||
} | ||
} | ||
!acquired_lock | ||
} | ||
} | ||
|
||
/// Polyfill for [`lock_api::RawRwLock`]. | ||
/// | ||
/// [`lock_api::RawRwLock`]: https://docs.rs/lock_api/*/lock_api/trait.RawRwLock.html | ||
pub unsafe trait RawRwLock { | ||
type GuardMarker; | ||
|
||
const INIT: Self; | ||
|
||
fn lock_shared(&self); | ||
fn try_lock_shared(&self) -> bool; | ||
unsafe fn unlock_shared(&self); | ||
fn lock_exclusive(&self); | ||
fn try_lock_exclusive(&self) -> bool; | ||
unsafe fn unlock_exclusive(&self); | ||
|
||
#[inline] | ||
fn is_locked(&self) -> bool { | ||
let acquired_lock = self.try_lock_exclusive(); | ||
if acquired_lock { | ||
unsafe { | ||
self.unlock_exclusive(); | ||
} | ||
} | ||
!acquired_lock | ||
} | ||
|
||
fn is_locked_exclusive(&self) -> bool { | ||
let acquired_lock = self.try_lock_shared(); | ||
if acquired_lock { | ||
unsafe { | ||
self.unlock_shared(); | ||
} | ||
} | ||
!acquired_lock | ||
} | ||
} |