Skip to content

Basic implementation using parking_lot::RawMutex #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions source/mutex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ optional = true
[dependencies.lock_api-0_4]
package = "lock_api"
version = "0.4"
default_features = false
default-features = false
optional = true

[dependencies.parking_lot]
version = "0.12.3"
optional = true
default-features = false

[features]
default = [
"impl-critical-section",
Expand All @@ -42,4 +47,6 @@ impl-lock_api-0_4 = ["dep:lock_api-0_4"]
#
# These can be disabled when minimizing binary size is important.
fmt = []
std = []
std = [
"dep:parking_lot",
]
52 changes: 52 additions & 0 deletions source/mutex/src/raw_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@

// ================

#[cfg(all(feature = "impl-unsafe-cortex-m-single-core", cortex_m))]

Check warning on line 142 in source/mutex/src/raw_impls.rs

View workflow job for this annotation

GitHub Actions / clippy

warning: unexpected `cfg` condition name: `cortex_m` --> source/mutex/src/raw_impls.rs:142:57 | 142 | #[cfg(all(feature = "impl-unsafe-cortex-m-single-core", cortex_m))] | ^^^^^^^^ | = help: expected names are: `clippy`, `debug_assertions`, `doc`, `docsrs`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows` = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(cortex_m)'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(cortex_m)");` to the top of the `build.rs` = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default
pub mod single_core_thread_mode {
//! A single-core safe implementation that does not require a critical section

Expand Down Expand Up @@ -269,3 +269,55 @@
}
}
}

#[cfg(feature = "std")]
pub mod std {
//! Std implementation of the [RawMutex] trait
//!
//! Currently based on [`parking_lot::RawMutex], but subject to change

use mutex_traits::{ConstInit, RawMutex};
use parking_lot::{lock_api::RawMutex as _, RawMutex as PLRawMutex};

/// Std implementation of the [RawMutex] trait
///
/// Currently based on [`parking_lot::RawMutex], but subject to change
#[cfg_attr(feature = "fmt", derive(Debug))]
pub struct StdRawMutex {
inner: PLRawMutex,
}

impl ConstInit for StdRawMutex {
const INIT: Self = StdRawMutex {
inner: PLRawMutex::INIT,
};
}

unsafe impl RawMutex for StdRawMutex {
type GuardMarker = <parking_lot::RawMutex as parking_lot::lock_api::RawMutex>::GuardMarker;

#[inline]
#[track_caller]
fn lock(&self) {
self.inner.lock();
}

#[inline]
#[track_caller]
fn try_lock(&self) -> bool {
self.inner.try_lock()
}

#[inline]
#[track_caller]
unsafe fn unlock(&self) {
self.inner.unlock()
}

#[inline]
#[track_caller]
fn is_locked(&self) -> bool {
self.inner.is_locked()
}
}
}
Loading