Skip to content

Commit

Permalink
feat: Add critical-section based implementation
Browse files Browse the repository at this point in the history
The current no-std implementation is somewhat ineffecient, potentially
broken and relies on some slow data structures. The main reason why we
use this implementation is because we don't have a way to "lock" the
linked list, since there's no way to lock things in no-std. However,
many embedded platforms have a concept of a "critical section" that can
be used to exclusively lock something.

This PR adds an option to the "std" implementation that replaces the
existing Mutex with a usage of the critical-section crate. This is
enabled with the "critical-section" feature. This allows us to have the
advantages of the std-based implementation without needing to rely on
std for platforms that don't have it.

Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Nov 17, 2024
1 parent 633b2c6 commit afd9d5c
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 84 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
run: cargo hack build --all --no-dev-deps
- run: cargo hack build --all --target thumbv7m-none-eabi --no-default-features --no-dev-deps
- run: cargo hack build --target thumbv7m-none-eabi --no-default-features --no-dev-deps --features portable-atomic
- run: cargo hack build --target thumbv7m-none-eabi --no-default-features --no-dev-deps --features critical-section
#- name: Install wasm-pack
# uses: taiki-e/install-action@wasm-pack
#- run: wasm-pack test --node
Expand Down Expand Up @@ -93,6 +94,7 @@ jobs:
uses: taiki-e/install-action@cargo-hack
- run: cargo hack build --all --rust-version
- run: cargo hack build --all --no-default-features --rust-version
- run: cargo hack build --all --no-default-features --rust-version --features critical-section

clippy:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)'] }

[dependencies]
concurrent-queue = { version = "2.4.0", default-features = false }
critical-section = { version = "1.2.0", default-features = false, optional = true }
pin-project-lite = "0.2.12"
portable-atomic-util = { version = "0.2.0", default-features = false, optional = true, features = ["alloc"] }

Expand All @@ -45,6 +46,7 @@ default-features = false
optional = true

[dev-dependencies]
critical-section = { version = "1.2.0", features = ["std"] }
futures-lite = "2.0.0"
try-lock = "0.2.5"
waker-fn = "1"
Expand Down
4 changes: 2 additions & 2 deletions examples/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This mutex exposes both blocking and async methods for acquiring a lock.
#[cfg(not(target_family = "wasm"))]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
mod example {
#![allow(dead_code)]

Expand Down Expand Up @@ -171,7 +171,7 @@ mod example {
}
}

#[cfg(target_family = "wasm")]
#[cfg(any(target_family = "wasm", not(feature = "std")))]
mod example {
pub(super) fn entry() {
println!("This example is not supported on wasm yet.");
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std as alloc;

#[cfg_attr(feature = "std", path = "std.rs")]
#[cfg_attr(not(feature = "std"), path = "no_std.rs")]
#[cfg_attr(any(feature = "std", feature = "critical-section"), path = "std.rs")]
#[cfg_attr(
not(any(feature = "std", feature = "critical-section")),
path = "no_std.rs"
)]
mod sys;

mod notify;
Expand Down
Loading

0 comments on commit afd9d5c

Please sign in to comment.