-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,154 @@ | ||
#[cfg(loom)] | ||
pub use loom_impl::DefaultMutex; | ||
|
||
#[cfg(all(not(loom), feature = "std"))] | ||
pub use std_impl::DefaultMutex; | ||
|
||
#[cfg(all(not(loom), not(feature = "std"), feature = "critical-section"))] | ||
pub use cs_impl::DefaultMutex; | ||
|
||
#[cfg(all(not(loom), not(feature = "std"), not(feature = "critical-section")))] | ||
pub use spin_impl::DefaultMutex; | ||
|
||
#[cfg(loom)] | ||
mod loom_impl { | ||
#[cfg(feature = "tracing")] | ||
use core::panic::Location; | ||
use mutex_traits::ScopedRawMutex; | ||
|
||
pub struct DefaultMutex(loom::sync::Mutex<()>); | ||
|
||
unsafe impl ScopedRawMutex for DefaultMutex { | ||
#[track_caller] | ||
fn with_lock<R>(&self, f: impl FnOnce() -> R) -> R { | ||
#[cfg(feature = "tracing")] | ||
let location = Location::caller(); | ||
#[cfg(feature = "tracing")] | ||
tracing::debug!(%location, "DefaultMutex::with_lock()"); | ||
|
||
let guard = self.0.lock(); | ||
tracing::debug!(%location, "DefaultMutex::with_lock() -> locked"); | ||
|
||
let result = f(); | ||
drop(guard); | ||
|
||
#[cfg(feature = "tracing")] | ||
tracing::debug!(%location, "DefaultMutex::with_lock() -> unlocked"); | ||
|
||
result | ||
} | ||
|
||
#[track_caller] | ||
fn try_with_lock<R>(&self, f: impl FnOnce() -> R) -> Option<R> { | ||
#[cfg(feature = "tracing")] | ||
let location = Location::caller(); | ||
#[cfg(feature = "tracing")] | ||
tracing::debug!(%location, "DefaultMutex::try_with_lock()"); | ||
|
||
match self.0.try_lock() { | ||
Ok(guard) => { | ||
#[cfg(feature = "tracing")] | ||
tracing::debug!(%location, "DefaultMutex::try_with_lock() -> locked"); | ||
|
||
let result = f(); | ||
drop(guard); | ||
|
||
#[cfg(feature = "tracing")] | ||
tracing::debug!(%location, "DefaultMutex::try_with_lock() -> unlocked"); | ||
|
||
Some(result) | ||
} | ||
None => { | ||
#[cfg(feature = "tracing")] | ||
tracing::debug!(%location, "DefaultMutex::try_with_lock() -> already locked"); | ||
|
||
None | ||
} | ||
} | ||
} | ||
|
||
fn is_locked(&self) -> bool { | ||
self.0.try_lock().is_none() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(all(not(loom), feature = "std"))] | ||
mod std_impl { | ||
use mutex_traits::ScopedRawMutex; | ||
|
||
pub struct DefaultMutex(std::sync::Mutex<()>); | ||
|
||
unsafe impl ScopedRawMutex for DefaultMutex { | ||
#[track_caller] | ||
#[inline] | ||
fn with_lock<R>(&self, f: impl FnOnce() -> R) -> R { | ||
let _guard = self.0.lock().unwrap(); | ||
f() | ||
} | ||
|
||
#[track_caller] | ||
fn try_with_lock<R>(&self, f: impl FnOnce() -> R) -> Option<R> { | ||
let _guard = self.0.try_lock().ok()?; | ||
Some(f()) | ||
} | ||
|
||
fn is_locked(&self) -> bool { | ||
self.0.try_lock().is_none() | ||
Check failure on line 97 in maitake-sync/src/blocking/default_mutex.rs GitHub Actions / cargo check (host)
Check failure on line 97 in maitake-sync/src/blocking/default_mutex.rs GitHub Actions / docs
|
||
} | ||
} | ||
} | ||
|
||
#[cfg(all(not(loom), not(feature = "std"), feature = "critical-section"))] | ||
mod cs_impl { | ||
use crate::spin::Spinlock; | ||
use mutex_traits::ScopedRawMutex; | ||
|
||
pub struct DefaultMutex(Spinlock); | ||
|
||
unsafe impl ScopedRawMutex for DefaultMutex { | ||
#[track_caller] | ||
#[inline] | ||
fn with_lock<R>(&self, f: impl FnOnce() -> R) -> R { | ||
self.0.with_lock(|| critical_section::with(|| f())) | ||
} | ||
|
||
#[track_caller] | ||
#[inline] | ||
fn try_with_lock<R>(&self, f: impl FnOnce() -> R) -> Option<R> { | ||
self.0.try_with_lock(|| critical_section::with(|| f())) | ||
} | ||
|
||
#[inline] | ||
fn is_locked(&self) -> bool { | ||
self.0.is_locked() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(all(not(loom), not(feature = "std"), not(feature = "critical-section")))] | ||
mod spin_impl { | ||
use crate::spin::Spinlock; | ||
use mutex_traits::ScopedRawMutex; | ||
|
||
pub struct DefaultMutex(Spinlock); | ||
|
||
unsafe impl ScopedRawMutex for DefaultMutex { | ||
#[track_caller] | ||
#[inline] | ||
fn with_lock<R>(&self, f: impl FnOnce() -> R) -> R { | ||
self.0.with_lock(|| critical_section::with(|| f())) | ||
Check failure on line 140 in maitake-sync/src/blocking/default_mutex.rs GitHub Actions / clippy
|
||
} | ||
|
||
#[track_caller] | ||
#[inline] | ||
fn try_with_lock<R>(&self, f: impl FnOnce() -> R) -> Option<R> { | ||
self.0.try_with_lock(|| critical_section::with(|| f())) | ||
Check failure on line 146 in maitake-sync/src/blocking/default_mutex.rs GitHub Actions / clippy
|
||
} | ||
|
||
#[inline] | ||
fn is_locked(&self) -> bool { | ||
self.0.is_locked() | ||
} | ||
} | ||
} |
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