aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/src
diff options
context:
space:
mode:
authorMatteo Meluzzi <[email protected]>2025-10-24 15:48:34 +0200
committerMatteo Meluzzi <[email protected]>2025-10-24 15:48:34 +0200
commit7976f950b0de72c521f92efa350c67ccd197fab9 (patch)
tree8759312eb000de09b92a4921f476d5c16b7e7342 /embassy-sync/src
parent828a8df18d04877df1f55f04354980b28ff2f2f8 (diff)
Merge branch 'main' into 17-add-support-for-boot-protocol
Diffstat (limited to 'embassy-sync/src')
-rw-r--r--embassy-sync/src/channel.rs30
-rw-r--r--embassy-sync/src/lib.rs1
-rw-r--r--embassy-sync/src/mutex.rs4
-rw-r--r--embassy-sync/src/once_lock.rs2
-rw-r--r--embassy-sync/src/pipe.rs2
-rw-r--r--embassy-sync/src/priority_channel.rs32
-rw-r--r--embassy-sync/src/pubsub/mod.rs2
-rw-r--r--embassy-sync/src/ring_buffer.rs6
-rw-r--r--embassy-sync/src/rwlock.rs4
-rw-r--r--embassy-sync/src/semaphore.rs4
-rw-r--r--embassy-sync/src/signal.rs4
-rw-r--r--embassy-sync/src/waitqueue/atomic_waker.rs2
-rw-r--r--embassy-sync/src/watch.rs4
-rw-r--r--embassy-sync/src/zerocopy_channel.rs10
14 files changed, 54 insertions, 53 deletions
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs
index de437cc52..dbd24a6c7 100644
--- a/embassy-sync/src/channel.rs
+++ b/embassy-sync/src/channel.rs
@@ -50,8 +50,8 @@ use core::task::{Context, Poll};
50 50
51use heapless::Deque; 51use heapless::Deque;
52 52
53use crate::blocking_mutex::raw::RawMutex;
54use crate::blocking_mutex::Mutex; 53use crate::blocking_mutex::Mutex;
54use crate::blocking_mutex::raw::RawMutex;
55use crate::waitqueue::WakerRegistration; 55use crate::waitqueue::WakerRegistration;
56 56
57/// Send-only access to a [`Channel`]. 57/// Send-only access to a [`Channel`].
@@ -1112,11 +1112,13 @@ mod tests {
1112 static CHANNEL: StaticCell<Channel<CriticalSectionRawMutex, u32, 3>> = StaticCell::new(); 1112 static CHANNEL: StaticCell<Channel<CriticalSectionRawMutex, u32, 3>> = StaticCell::new();
1113 let c = &*CHANNEL.init(Channel::new()); 1113 let c = &*CHANNEL.init(Channel::new());
1114 let c2 = c; 1114 let c2 = c;
1115 assert!(executor 1115 assert!(
1116 .spawn(async move { 1116 executor
1117 assert!(c2.try_send(1).is_ok()); 1117 .spawn(async move {
1118 }) 1118 assert!(c2.try_send(1).is_ok());
1119 .is_ok()); 1119 })
1120 .is_ok()
1121 );
1120 assert_eq!(c.receive().await, 1); 1122 assert_eq!(c.receive().await, 1);
1121 } 1123 }
1122 1124
@@ -1143,13 +1145,15 @@ mod tests {
1143 // However, I've used the debugger to observe that the send does indeed wait. 1145 // However, I've used the debugger to observe that the send does indeed wait.
1144 Delay::new(Duration::from_millis(500)).await; 1146 Delay::new(Duration::from_millis(500)).await;
1145 assert_eq!(c.receive().await, 1); 1147 assert_eq!(c.receive().await, 1);
1146 assert!(executor 1148 assert!(
1147 .spawn(async move { 1149 executor
1148 loop { 1150 .spawn(async move {
1149 c.receive().await; 1151 loop {
1150 } 1152 c.receive().await;
1151 }) 1153 }
1152 .is_ok()); 1154 })
1155 .is_ok()
1156 );
1153 send_task_1.unwrap().await; 1157 send_task_1.unwrap().await;
1154 send_task_2.unwrap().await; 1158 send_task_2.unwrap().await;
1155 } 1159 }
diff --git a/embassy-sync/src/lib.rs b/embassy-sync/src/lib.rs
index 5d91b4d9c..1cfde8b10 100644
--- a/embassy-sync/src/lib.rs
+++ b/embassy-sync/src/lib.rs
@@ -1,6 +1,7 @@
1#![cfg_attr(not(feature = "std"), no_std)] 1#![cfg_attr(not(feature = "std"), no_std)]
2#![allow(async_fn_in_trait)] 2#![allow(async_fn_in_trait)]
3#![allow(clippy::new_without_default)] 3#![allow(clippy::new_without_default)]
4#![allow(unsafe_op_in_unsafe_fn)]
4#![doc = include_str!("../README.md")] 5#![doc = include_str!("../README.md")]
5#![warn(missing_docs)] 6#![warn(missing_docs)]
6 7
diff --git a/embassy-sync/src/mutex.rs b/embassy-sync/src/mutex.rs
index aea682899..96b834f02 100644
--- a/embassy-sync/src/mutex.rs
+++ b/embassy-sync/src/mutex.rs
@@ -2,13 +2,13 @@
2//! 2//!
3//! This module provides a mutex that can be used to synchronize data between asynchronous tasks. 3//! This module provides a mutex that can be used to synchronize data between asynchronous tasks.
4use core::cell::{RefCell, UnsafeCell}; 4use core::cell::{RefCell, UnsafeCell};
5use core::future::{poll_fn, Future}; 5use core::future::{Future, poll_fn};
6use core::ops::{Deref, DerefMut}; 6use core::ops::{Deref, DerefMut};
7use core::task::Poll; 7use core::task::Poll;
8use core::{fmt, mem}; 8use core::{fmt, mem};
9 9
10use crate::blocking_mutex::raw::RawMutex;
11use crate::blocking_mutex::Mutex as BlockingMutex; 10use crate::blocking_mutex::Mutex as BlockingMutex;
11use crate::blocking_mutex::raw::RawMutex;
12use crate::waitqueue::WakerRegistration; 12use crate::waitqueue::WakerRegistration;
13 13
14/// Error returned by [`Mutex::try_lock`] 14/// Error returned by [`Mutex::try_lock`]
diff --git a/embassy-sync/src/once_lock.rs b/embassy-sync/src/once_lock.rs
index 73edfea9a..2af19ca20 100644
--- a/embassy-sync/src/once_lock.rs
+++ b/embassy-sync/src/once_lock.rs
@@ -2,7 +2,7 @@
2 2
3use core::cell::Cell; 3use core::cell::Cell;
4use core::fmt::{Debug, Formatter}; 4use core::fmt::{Debug, Formatter};
5use core::future::{poll_fn, Future}; 5use core::future::{Future, poll_fn};
6use core::mem::MaybeUninit; 6use core::mem::MaybeUninit;
7use core::sync::atomic::{AtomicBool, Ordering}; 7use core::sync::atomic::{AtomicBool, Ordering};
8use core::task::Poll; 8use core::task::Poll;
diff --git a/embassy-sync/src/pipe.rs b/embassy-sync/src/pipe.rs
index 6d624979a..215a556d9 100644
--- a/embassy-sync/src/pipe.rs
+++ b/embassy-sync/src/pipe.rs
@@ -7,8 +7,8 @@ use core::ops::Range;
7use core::pin::Pin; 7use core::pin::Pin;
8use core::task::{Context, Poll}; 8use core::task::{Context, Poll};
9 9
10use crate::blocking_mutex::raw::RawMutex;
11use crate::blocking_mutex::Mutex; 10use crate::blocking_mutex::Mutex;
11use crate::blocking_mutex::raw::RawMutex;
12use crate::ring_buffer::RingBuffer; 12use crate::ring_buffer::RingBuffer;
13use crate::waitqueue::WakerRegistration; 13use crate::waitqueue::WakerRegistration;
14 14
diff --git a/embassy-sync/src/priority_channel.rs b/embassy-sync/src/priority_channel.rs
index 715a20e86..1af7d9221 100644
--- a/embassy-sync/src/priority_channel.rs
+++ b/embassy-sync/src/priority_channel.rs
@@ -8,11 +8,11 @@ use core::future::Future;
8use core::pin::Pin; 8use core::pin::Pin;
9use core::task::{Context, Poll}; 9use core::task::{Context, Poll};
10 10
11pub use heapless::binary_heap::{Kind, Max, Min};
12use heapless::BinaryHeap; 11use heapless::BinaryHeap;
12pub use heapless::binary_heap::{Kind, Max, Min};
13 13
14use crate::blocking_mutex::raw::RawMutex;
15use crate::blocking_mutex::Mutex; 14use crate::blocking_mutex::Mutex;
15use crate::blocking_mutex::raw::RawMutex;
16use crate::channel::{DynamicChannel, DynamicReceiver, DynamicSender, TryReceiveError, TrySendError}; 16use crate::channel::{DynamicChannel, DynamicReceiver, DynamicSender, TryReceiveError, TrySendError};
17use crate::waitqueue::WakerRegistration; 17use crate::waitqueue::WakerRegistration;
18 18
@@ -799,11 +799,13 @@ mod tests {
799 static CHANNEL: StaticCell<PriorityChannel<CriticalSectionRawMutex, u32, Max, 3>> = StaticCell::new(); 799 static CHANNEL: StaticCell<PriorityChannel<CriticalSectionRawMutex, u32, Max, 3>> = StaticCell::new();
800 let c = &*CHANNEL.init(PriorityChannel::new()); 800 let c = &*CHANNEL.init(PriorityChannel::new());
801 let c2 = c; 801 let c2 = c;
802 assert!(executor 802 assert!(
803 .spawn(async move { 803 executor
804 assert!(c2.try_send(1).is_ok()); 804 .spawn(async move {
805 }) 805 assert!(c2.try_send(1).is_ok());
806 .is_ok()); 806 })
807 .is_ok()
808 );
807 assert_eq!(c.receive().await, 1); 809 assert_eq!(c.receive().await, 1);
808 } 810 }
809 811
@@ -830,13 +832,15 @@ mod tests {
830 // However, I've used the debugger to observe that the send does indeed wait. 832 // However, I've used the debugger to observe that the send does indeed wait.
831 Delay::new(Duration::from_millis(500)).await; 833 Delay::new(Duration::from_millis(500)).await;
832 assert_eq!(c.receive().await, 1); 834 assert_eq!(c.receive().await, 1);
833 assert!(executor 835 assert!(
834 .spawn(async move { 836 executor
835 loop { 837 .spawn(async move {
836 c.receive().await; 838 loop {
837 } 839 c.receive().await;
838 }) 840 }
839 .is_ok()); 841 })
842 .is_ok()
843 );
840 send_task_1.unwrap().await; 844 send_task_1.unwrap().await;
841 send_task_2.unwrap().await; 845 send_task_2.unwrap().await;
842 } 846 }
diff --git a/embassy-sync/src/pubsub/mod.rs b/embassy-sync/src/pubsub/mod.rs
index ad9402f5a..127a208f1 100644
--- a/embassy-sync/src/pubsub/mod.rs
+++ b/embassy-sync/src/pubsub/mod.rs
@@ -10,8 +10,8 @@ use heapless::Deque;
10 10
11use self::publisher::{ImmediatePub, Pub}; 11use self::publisher::{ImmediatePub, Pub};
12use self::subscriber::Sub; 12use self::subscriber::Sub;
13use crate::blocking_mutex::raw::RawMutex;
14use crate::blocking_mutex::Mutex; 13use crate::blocking_mutex::Mutex;
14use crate::blocking_mutex::raw::RawMutex;
15use crate::waitqueue::MultiWakerRegistration; 15use crate::waitqueue::MultiWakerRegistration;
16 16
17pub mod publisher; 17pub mod publisher;
diff --git a/embassy-sync/src/ring_buffer.rs b/embassy-sync/src/ring_buffer.rs
index f03b7dd8f..608447cd6 100644
--- a/embassy-sync/src/ring_buffer.rs
+++ b/embassy-sync/src/ring_buffer.rs
@@ -95,11 +95,7 @@ impl<const N: usize> RingBuffer<N> {
95 95
96 fn wrap(&self, n: usize) -> usize { 96 fn wrap(&self, n: usize) -> usize {
97 assert!(n <= N); 97 assert!(n <= N);
98 if n == N { 98 if n == N { 0 } else { n }
99 0
100 } else {
101 n
102 }
103 } 99 }
104} 100}
105 101
diff --git a/embassy-sync/src/rwlock.rs b/embassy-sync/src/rwlock.rs
index e43388c4d..918a6aa41 100644
--- a/embassy-sync/src/rwlock.rs
+++ b/embassy-sync/src/rwlock.rs
@@ -3,12 +3,12 @@
3//! This module provides a read-write lock that can be used to synchronize data between asynchronous tasks. 3//! This module provides a read-write lock that can be used to synchronize data between asynchronous tasks.
4use core::cell::{RefCell, UnsafeCell}; 4use core::cell::{RefCell, UnsafeCell};
5use core::fmt; 5use core::fmt;
6use core::future::{poll_fn, Future}; 6use core::future::{Future, poll_fn};
7use core::ops::{Deref, DerefMut}; 7use core::ops::{Deref, DerefMut};
8use core::task::Poll; 8use core::task::Poll;
9 9
10use crate::blocking_mutex::raw::RawMutex;
11use crate::blocking_mutex::Mutex as BlockingMutex; 10use crate::blocking_mutex::Mutex as BlockingMutex;
11use crate::blocking_mutex::raw::RawMutex;
12use crate::waitqueue::WakerRegistration; 12use crate::waitqueue::WakerRegistration;
13 13
14/// Error returned by [`RwLock::try_read`] and [`RwLock::try_write`] when the lock is already held. 14/// Error returned by [`RwLock::try_read`] and [`RwLock::try_write`] when the lock is already held.
diff --git a/embassy-sync/src/semaphore.rs b/embassy-sync/src/semaphore.rs
index 4e82b0fcd..8d2413931 100644
--- a/embassy-sync/src/semaphore.rs
+++ b/embassy-sync/src/semaphore.rs
@@ -1,13 +1,13 @@
1//! A synchronization primitive for controlling access to a pool of resources. 1//! A synchronization primitive for controlling access to a pool of resources.
2use core::cell::{Cell, RefCell}; 2use core::cell::{Cell, RefCell};
3use core::convert::Infallible; 3use core::convert::Infallible;
4use core::future::{poll_fn, Future}; 4use core::future::{Future, poll_fn};
5use core::task::{Poll, Waker}; 5use core::task::{Poll, Waker};
6 6
7use heapless::Deque; 7use heapless::Deque;
8 8
9use crate::blocking_mutex::raw::RawMutex;
10use crate::blocking_mutex::Mutex; 9use crate::blocking_mutex::Mutex;
10use crate::blocking_mutex::raw::RawMutex;
11use crate::waitqueue::WakerRegistration; 11use crate::waitqueue::WakerRegistration;
12 12
13/// An asynchronous semaphore. 13/// An asynchronous semaphore.
diff --git a/embassy-sync/src/signal.rs b/embassy-sync/src/signal.rs
index 229b1fa99..cc02228cf 100644
--- a/embassy-sync/src/signal.rs
+++ b/embassy-sync/src/signal.rs
@@ -1,10 +1,10 @@
1//! A synchronization primitive for passing the latest value to a task. 1//! A synchronization primitive for passing the latest value to a task.
2use core::cell::Cell; 2use core::cell::Cell;
3use core::future::{poll_fn, Future}; 3use core::future::{Future, poll_fn};
4use core::task::{Context, Poll, Waker}; 4use core::task::{Context, Poll, Waker};
5 5
6use crate::blocking_mutex::raw::RawMutex;
7use crate::blocking_mutex::Mutex; 6use crate::blocking_mutex::Mutex;
7use crate::blocking_mutex::raw::RawMutex;
8 8
9/// Single-slot signaling primitive for a _single_ consumer. 9/// Single-slot signaling primitive for a _single_ consumer.
10/// 10///
diff --git a/embassy-sync/src/waitqueue/atomic_waker.rs b/embassy-sync/src/waitqueue/atomic_waker.rs
index 5a9910e7f..d2bf890e5 100644
--- a/embassy-sync/src/waitqueue/atomic_waker.rs
+++ b/embassy-sync/src/waitqueue/atomic_waker.rs
@@ -1,8 +1,8 @@
1use core::cell::Cell; 1use core::cell::Cell;
2use core::task::Waker; 2use core::task::Waker;
3 3
4use crate::blocking_mutex::raw::{CriticalSectionRawMutex, RawMutex};
5use crate::blocking_mutex::Mutex; 4use crate::blocking_mutex::Mutex;
5use crate::blocking_mutex::raw::{CriticalSectionRawMutex, RawMutex};
6 6
7/// Utility struct to register and wake a waker. 7/// Utility struct to register and wake a waker.
8/// If a waker is registered, registering another waker will replace the previous one without waking it. 8/// If a waker is registered, registering another waker will replace the previous one without waking it.
diff --git a/embassy-sync/src/watch.rs b/embassy-sync/src/watch.rs
index 332ab5405..0f8a8d679 100644
--- a/embassy-sync/src/watch.rs
+++ b/embassy-sync/src/watch.rs
@@ -1,13 +1,13 @@
1//! A synchronization primitive for passing the latest value to **multiple** receivers. 1//! A synchronization primitive for passing the latest value to **multiple** receivers.
2 2
3use core::cell::RefCell; 3use core::cell::RefCell;
4use core::future::{poll_fn, Future}; 4use core::future::{Future, poll_fn};
5use core::marker::PhantomData; 5use core::marker::PhantomData;
6use core::ops::{Deref, DerefMut}; 6use core::ops::{Deref, DerefMut};
7use core::task::{Context, Poll}; 7use core::task::{Context, Poll};
8 8
9use crate::blocking_mutex::raw::RawMutex;
10use crate::blocking_mutex::Mutex; 9use crate::blocking_mutex::Mutex;
10use crate::blocking_mutex::raw::RawMutex;
11use crate::waitqueue::MultiWakerRegistration; 11use crate::waitqueue::MultiWakerRegistration;
12 12
13/// The `Watch` is a single-slot signaling primitive that allows _multiple_ (`N`) receivers to concurrently await 13/// The `Watch` is a single-slot signaling primitive that allows _multiple_ (`N`) receivers to concurrently await
diff --git a/embassy-sync/src/zerocopy_channel.rs b/embassy-sync/src/zerocopy_channel.rs
index b3f7dbe8c..c572592b8 100644
--- a/embassy-sync/src/zerocopy_channel.rs
+++ b/embassy-sync/src/zerocopy_channel.rs
@@ -15,12 +15,12 @@
15//! another message will result in an error being returned. 15//! another message will result in an error being returned.
16 16
17use core::cell::RefCell; 17use core::cell::RefCell;
18use core::future::{poll_fn, Future}; 18use core::future::{Future, poll_fn};
19use core::marker::PhantomData; 19use core::marker::PhantomData;
20use core::task::{Context, Poll}; 20use core::task::{Context, Poll};
21 21
22use crate::blocking_mutex::raw::RawMutex;
23use crate::blocking_mutex::Mutex; 22use crate::blocking_mutex::Mutex;
23use crate::blocking_mutex::raw::RawMutex;
24use crate::waitqueue::WakerRegistration; 24use crate::waitqueue::WakerRegistration;
25 25
26/// A bounded zero-copy channel for communicating between asynchronous tasks 26/// A bounded zero-copy channel for communicating between asynchronous tasks
@@ -296,11 +296,7 @@ struct State {
296 296
297impl State { 297impl State {
298 fn increment(&self, i: usize) -> usize { 298 fn increment(&self, i: usize) -> usize {
299 if i + 1 == self.capacity { 299 if i + 1 == self.capacity { 0 } else { i + 1 }
300 0
301 } else {
302 i + 1
303 }
304 } 300 }
305 301
306 fn clear(&mut self) { 302 fn clear(&mut self) {