diff options
| author | James Munns <[email protected]> | 2025-07-15 13:33:51 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2025-09-11 14:45:06 +0200 |
| commit | 20b56b0fe0570f0d1e8c61d23d067627a4dfc165 (patch) | |
| tree | 19f6c64f773f013d870c0f0feec8080adc2fe910 | |
| parent | cf171ad6d9c0a7487400beb9e4a436e5c1b64e19 (diff) | |
Update to use critical-section::Mutex instead of mutex::BlockingMutex
This allows the scheduler to better collaborate with existing critical sections
| -rw-r--r-- | embassy-executor/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-executor/src/raw/run_queue.rs | 28 |
2 files changed, 17 insertions, 15 deletions
diff --git a/embassy-executor/Cargo.toml b/embassy-executor/Cargo.toml index 01c028704..290e67bce 100644 --- a/embassy-executor/Cargo.toml +++ b/embassy-executor/Cargo.toml | |||
| @@ -81,10 +81,6 @@ avr-device = { version = "0.7.0", optional = true } | |||
| 81 | version = "0.3.4" | 81 | version = "0.3.4" |
| 82 | features = ["no-cache-pad"] | 82 | features = ["no-cache-pad"] |
| 83 | 83 | ||
| 84 | # Note: this is ONLY a dependency when the target does NOT have atomics | ||
| 85 | [target.'cfg(not(target_has_atomic="ptr"))'.dependencies.mutex] | ||
| 86 | version = "1.0" | ||
| 87 | |||
| 88 | [dev-dependencies] | 84 | [dev-dependencies] |
| 89 | critical-section = { version = "1.1", features = ["std"] } | 85 | critical-section = { version = "1.1", features = ["std"] } |
| 90 | trybuild = "1.0" | 86 | trybuild = "1.0" |
diff --git a/embassy-executor/src/raw/run_queue.rs b/embassy-executor/src/raw/run_queue.rs index b4b22819f..9acb9dd28 100644 --- a/embassy-executor/src/raw/run_queue.rs +++ b/embassy-executor/src/raw/run_queue.rs | |||
| @@ -70,8 +70,12 @@ impl RunQueue { | |||
| 70 | /// | 70 | /// |
| 71 | /// `item` must NOT be already enqueued in any queue. | 71 | /// `item` must NOT be already enqueued in any queue. |
| 72 | #[inline(always)] | 72 | #[inline(always)] |
| 73 | pub(crate) unsafe fn enqueue(&self, task: TaskRef, _: super::state::Token) -> bool { | 73 | pub(crate) unsafe fn enqueue(&self, task: TaskRef, _tok: super::state::Token) -> bool { |
| 74 | self.stack.push_was_empty(task) | 74 | self.stack.push_was_empty( |
| 75 | task, | ||
| 76 | #[cfg(not(target_has_atomic = "ptr"))] | ||
| 77 | _tok, | ||
| 78 | ) | ||
| 75 | } | 79 | } |
| 76 | 80 | ||
| 77 | /// # Standard atomic runqueue | 81 | /// # Standard atomic runqueue |
| @@ -153,26 +157,28 @@ fn run_dequeue(taskref: &TaskRef) { | |||
| 153 | /// A wrapper type that acts like TransferStack by wrapping a normal Stack in a CS mutex | 157 | /// A wrapper type that acts like TransferStack by wrapping a normal Stack in a CS mutex |
| 154 | #[cfg(not(target_has_atomic = "ptr"))] | 158 | #[cfg(not(target_has_atomic = "ptr"))] |
| 155 | struct MutexTransferStack<T: Linked<cordyceps::stack::Links<T>>> { | 159 | struct MutexTransferStack<T: Linked<cordyceps::stack::Links<T>>> { |
| 156 | inner: mutex::BlockingMutex<mutex::raw_impls::cs::CriticalSectionRawMutex, cordyceps::Stack<T>>, | 160 | inner: critical_section::Mutex<core::cell::RefCell<cordyceps::Stack<T>>>, |
| 157 | } | 161 | } |
| 158 | 162 | ||
| 159 | #[cfg(not(target_has_atomic = "ptr"))] | 163 | #[cfg(not(target_has_atomic = "ptr"))] |
| 160 | impl<T: Linked<cordyceps::stack::Links<T>>> MutexTransferStack<T> { | 164 | impl<T: Linked<cordyceps::stack::Links<T>>> MutexTransferStack<T> { |
| 161 | const fn new() -> Self { | 165 | const fn new() -> Self { |
| 162 | Self { | 166 | Self { |
| 163 | inner: mutex::BlockingMutex::new(cordyceps::Stack::new()), | 167 | inner: critical_section::Mutex::new(core::cell::RefCell::new(cordyceps::Stack::new())), |
| 164 | } | 168 | } |
| 165 | } | 169 | } |
| 166 | 170 | ||
| 167 | fn push_was_empty(&self, item: T::Handle) -> bool { | 171 | fn push_was_empty(&self, item: T::Handle, token: super::state::Token) -> bool { |
| 168 | self.inner.with_lock(|stack| { | 172 | let mut guard = self.inner.borrow_ref_mut(token); |
| 169 | let is_empty = stack.is_empty(); | 173 | let is_empty = guard.is_empty(); |
| 170 | stack.push(item); | 174 | guard.push(item); |
| 171 | is_empty | 175 | is_empty |
| 172 | }) | ||
| 173 | } | 176 | } |
| 174 | 177 | ||
| 175 | fn take_all(&self) -> cordyceps::Stack<T> { | 178 | fn take_all(&self) -> cordyceps::Stack<T> { |
| 176 | self.inner.with_lock(|stack| stack.take_all()) | 179 | critical_section::with(|cs| { |
| 180 | let mut guard = self.inner.borrow_ref_mut(cs); | ||
| 181 | guard.take_all() | ||
| 182 | }) | ||
| 177 | } | 183 | } |
| 178 | } | 184 | } |
