Skip to content

Commit

Permalink
Use spin crate for mutex implementation (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed Feb 23, 2024
1 parent a374a43 commit ee02415
Show file tree
Hide file tree
Showing 55 changed files with 502 additions and 37 deletions.
10 changes: 10 additions & 0 deletions crates/prelude/Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/runner-host/Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/runner-nordic/Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/scheduler/Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/sync/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## 0.1.0-git

<!-- Increment to skip CHANGELOG.md test: 5 -->
<!-- Increment to skip CHANGELOG.md test: 6 -->
10 changes: 10 additions & 0 deletions crates/sync/Cargo.lock

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

1 change: 1 addition & 0 deletions crates/sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["concurrency", "no-std"]

[dependencies]
portable-atomic = { version = "1.6.0", default-features = false }
spin = { version = "0.9.8", default-features = false, features = ["portable_atomic", "spin_mutex"] }

[features]
unsafe-assume-single-core = ["portable-atomic/unsafe-assume-single-core"]
40 changes: 4 additions & 36 deletions crates/sync/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use core::cell::UnsafeCell;
use core::ops::{Deref, DerefMut};

use portable_atomic::AtomicBool;
use portable_atomic::Ordering::{Acquire, Relaxed, Release};

/// Non-blocking non-reentrant mutex.
///
/// Locking this mutex will panic if it is already locked. In particular, it will not block.
pub struct Mutex<T> {
lock: AtomicBool,
data: UnsafeCell<T>,
}
pub struct Mutex<T>(spin::Mutex<T>);

/// Locks a mutex and provides access to its content until dropped.
pub struct MutexGuard<'a, T>(&'a Mutex<T>);

unsafe impl<T: Send> Sync for Mutex<T> {}
pub type MutexGuard<'a, T> = spin::MutexGuard<'a, T>;

impl<T> Mutex<T> {
/// Creates a new mutex.
pub const fn new(data: T) -> Self {
Mutex { lock: AtomicBool::new(false), data: UnsafeCell::new(data) }
Mutex(spin::Mutex::new(data))
}

/// Tries to lock the mutex.
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
self.lock.compare_exchange(false, true, Acquire, Relaxed).ok()?;
Some(MutexGuard(self))
self.0.try_lock()
}

/// Locks the mutex.
Expand All @@ -54,26 +42,6 @@ impl<T> Mutex<T> {
}
}

impl<'a, T> Deref for MutexGuard<'a, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
unsafe { &*self.0.data.get() }
}
}

impl<'a, T> DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.0.data.get() }
}
}

impl<'a, T> Drop for MutexGuard<'a, T> {
fn drop(&mut self) {
self.0.lock.store(false, Release);
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
10 changes: 10 additions & 0 deletions examples/rust/blink/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/blink_periodic/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/button/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/button_abort/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/ccm/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/ctap/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/ec_test/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/echo/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/exercises/client/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/rust/exercises/interface/Cargo.lock

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

Loading

0 comments on commit ee02415

Please sign in to comment.