Skip to content

Commit 3563142

Browse files
committed
refactor(port_std): replace try_mutex::TryMutex with spin::Mutex
They are functionally equivalent, but `spin::Mutex` supports no-std targets. I will need `spin::Mutex` for an upcoming example app, and if we are going to use `spin::Mutex` in one package, we might as well use it in all packages to keep the package count in `Cargo.lock` stable. `spin` is also the most popular of the packages providing similar functionalities.
1 parent 375a9e4 commit 3563142

File tree

4 files changed

+14
-31
lines changed

4 files changed

+14
-31
lines changed

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/r3_port_std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ r3 = { version = "0.1.1", path = "../r3" }
1414
atomic_ref = { version = "0.2.0" }
1515
env_logger = { version = "0.8.4" }
1616
once_cell = { version = "1.4.0" }
17-
try-mutex = { version = "0.3.0" }
17+
spin = { version = "0.9.2", default-features = false, features = ["spin_mutex"] }
1818
log = { version = "0.4.8" }
1919

2020
[dev-dependencies.r3_test_suite]

src/r3_port_std/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use r3::{
1616
},
1717
prelude::*,
1818
};
19+
use spin::Mutex as SpinMutex;
1920
use std::{
2021
cell::Cell,
2122
sync::mpsc,
2223
time::{Duration, Instant},
2324
};
24-
use try_mutex::TryMutex;
2525

2626
#[cfg(doc)]
2727
#[doc = include_str!("../CHANGELOG.md")]
@@ -42,8 +42,6 @@ mod sched;
4242
mod ums;
4343
mod utils;
4444

45-
use self::utils::LockConsuming;
46-
4745
/// Used by `use_port!`
4846
#[doc(hidden)]
4947
pub extern crate r3;
@@ -89,7 +87,7 @@ pub unsafe trait PortInstance: Kernel + Port<PortTaskState = TaskState> {
8987
#[doc(hidden)]
9088
pub struct State {
9189
thread_group: OnceCell<ums::ThreadGroup<sched::SchedState>>,
92-
timer_cmd_send: TryMutex<Option<mpsc::Sender<TimerCmd>>>,
90+
timer_cmd_send: SpinMutex<Option<mpsc::Sender<TimerCmd>>>,
9391
origin: AtomicRef<'static, Instant>,
9492
}
9593

@@ -98,10 +96,10 @@ pub struct TaskState {
9896
/// The task's state in the task state machine.
9997
///
10098
/// This field is expected to be accessed with CPU Lock or a scheduler lock,
101-
/// so `TryMutex` is sufficient (no real mutexes are necessary). It could be
99+
/// so `SpinMutex` is sufficient (no real mutexes are necessary). It could be
102100
/// even `UnsafeCell`, but we'd like to avoid unsafe code whenever possible.
103101
/// The runtime performance is not a concern in `r3_port_std`.
104-
tsm: TryMutex<Tsm>,
102+
tsm: SpinMutex<Tsm>,
105103
}
106104

107105
impl Init for TaskState {
@@ -147,7 +145,7 @@ thread_local! {
147145
impl TaskState {
148146
pub const fn new() -> Self {
149147
Self {
150-
tsm: TryMutex::new(Tsm::Uninit),
148+
tsm: SpinMutex::new(Tsm::Uninit),
151149
}
152150
}
153151

@@ -198,7 +196,7 @@ impl State {
198196
pub const fn new() -> Self {
199197
Self {
200198
thread_group: OnceCell::new(),
201-
timer_cmd_send: TryMutex::new(None),
199+
timer_cmd_send: SpinMutex::new(None),
202200
origin: AtomicRef::new(None),
203201
}
204202
}

src/r3_port_std/src/utils.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,3 @@ impl HasAtomicEquivalent for isize {
4848
}
4949

5050
pub type Atomic<T> = <T as HasAtomicEquivalent>::AtomicEquivalent;
51-
52-
pub trait LockConsuming {
53-
type LockGuard;
54-
55-
fn lock(self) -> Self::LockGuard;
56-
}
57-
58-
impl<'a, T> LockConsuming for &'a try_mutex::TryMutex<T> {
59-
type LockGuard = try_mutex::TryMutexGuard<'a, T>;
60-
61-
#[inline]
62-
fn lock(self) -> Self::LockGuard {
63-
self.try_lock().unwrap()
64-
}
65-
}

0 commit comments

Comments
 (0)