diff options
| author | Karun Koppula <[email protected]> | 2024-03-07 15:20:29 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-07 15:20:29 -0500 |
| commit | 54751b7a5093b7960e42cee1bc9a850f9c4b7a8f (patch) | |
| tree | df08df503f2b441c5b62306d50532403fe4c19c9 /embassy-sync | |
| parent | 3b1d87050e2a30b598e92979b6f202b67664a29c (diff) | |
| parent | b2d236ee390081ec6aeef1a27da06098f9febbf9 (diff) | |
Merge branch 'main' into karun/main_octospi_implementation
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/src/channel.rs | 22 | ||||
| -rw-r--r-- | embassy-sync/src/priority_channel.rs | 2 | ||||
| -rw-r--r-- | embassy-sync/src/ring_buffer.rs | 22 | ||||
| -rw-r--r-- | embassy-sync/src/zerocopy_channel.rs | 7 |
4 files changed, 35 insertions, 18 deletions
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs index ff7129303..01db0d09a 100644 --- a/embassy-sync/src/channel.rs +++ b/embassy-sync/src/channel.rs | |||
| @@ -507,6 +507,16 @@ where | |||
| 507 | Receiver { channel: self } | 507 | Receiver { channel: self } |
| 508 | } | 508 | } |
| 509 | 509 | ||
| 510 | /// Get a sender for this channel using dynamic dispatch. | ||
| 511 | pub fn dyn_sender(&self) -> DynamicSender<'_, T> { | ||
| 512 | DynamicSender { channel: self } | ||
| 513 | } | ||
| 514 | |||
| 515 | /// Get a receiver for this channel using dynamic dispatch. | ||
| 516 | pub fn dyn_receiver(&self) -> DynamicReceiver<'_, T> { | ||
| 517 | DynamicReceiver { channel: self } | ||
| 518 | } | ||
| 519 | |||
| 510 | /// Send a value, waiting until there is capacity. | 520 | /// Send a value, waiting until there is capacity. |
| 511 | /// | 521 | /// |
| 512 | /// Sending completes when the value has been pushed to the channel's queue. | 522 | /// Sending completes when the value has been pushed to the channel's queue. |
| @@ -648,7 +658,7 @@ mod tests { | |||
| 648 | } | 658 | } |
| 649 | 659 | ||
| 650 | #[test] | 660 | #[test] |
| 651 | fn dynamic_dispatch() { | 661 | fn dynamic_dispatch_into() { |
| 652 | let c = Channel::<NoopRawMutex, u32, 3>::new(); | 662 | let c = Channel::<NoopRawMutex, u32, 3>::new(); |
| 653 | let s: DynamicSender<'_, u32> = c.sender().into(); | 663 | let s: DynamicSender<'_, u32> = c.sender().into(); |
| 654 | let r: DynamicReceiver<'_, u32> = c.receiver().into(); | 664 | let r: DynamicReceiver<'_, u32> = c.receiver().into(); |
| @@ -657,6 +667,16 @@ mod tests { | |||
| 657 | assert_eq!(r.try_receive().unwrap(), 1); | 667 | assert_eq!(r.try_receive().unwrap(), 1); |
| 658 | } | 668 | } |
| 659 | 669 | ||
| 670 | #[test] | ||
| 671 | fn dynamic_dispatch_constructor() { | ||
| 672 | let c = Channel::<NoopRawMutex, u32, 3>::new(); | ||
| 673 | let s = c.dyn_sender(); | ||
| 674 | let r = c.dyn_receiver(); | ||
| 675 | |||
| 676 | assert!(s.try_send(1).is_ok()); | ||
| 677 | assert_eq!(r.try_receive().unwrap(), 1); | ||
| 678 | } | ||
| 679 | |||
| 660 | #[futures_test::test] | 680 | #[futures_test::test] |
| 661 | async fn receiver_receives_given_try_send_async() { | 681 | async fn receiver_receives_given_try_send_async() { |
| 662 | let executor = ThreadPool::new().unwrap(); | 682 | let executor = ThreadPool::new().unwrap(); |
diff --git a/embassy-sync/src/priority_channel.rs b/embassy-sync/src/priority_channel.rs index bd75c0135..e77678c24 100644 --- a/embassy-sync/src/priority_channel.rs +++ b/embassy-sync/src/priority_channel.rs | |||
| @@ -325,7 +325,7 @@ where | |||
| 325 | /// | 325 | /// |
| 326 | /// Sent data may be reordered based on their priorty within the channel. | 326 | /// Sent data may be reordered based on their priorty within the channel. |
| 327 | /// For example, in a [`Max`](heapless::binary_heap::Max) [`PriorityChannel`] | 327 | /// For example, in a [`Max`](heapless::binary_heap::Max) [`PriorityChannel`] |
| 328 | /// containing `u32`'s, data sent in the following order `[1, 2, 3]` will be recieved as `[3, 2, 1]`. | 328 | /// containing `u32`'s, data sent in the following order `[1, 2, 3]` will be received as `[3, 2, 1]`. |
| 329 | pub struct PriorityChannel<M, T, K, const N: usize> | 329 | pub struct PriorityChannel<M, T, K, const N: usize> |
| 330 | where | 330 | where |
| 331 | T: Ord, | 331 | T: Ord, |
diff --git a/embassy-sync/src/ring_buffer.rs b/embassy-sync/src/ring_buffer.rs index d95ffa7c9..81e60c42b 100644 --- a/embassy-sync/src/ring_buffer.rs +++ b/embassy-sync/src/ring_buffer.rs | |||
| @@ -3,7 +3,7 @@ use core::ops::Range; | |||
| 3 | pub struct RingBuffer<const N: usize> { | 3 | pub struct RingBuffer<const N: usize> { |
| 4 | start: usize, | 4 | start: usize, |
| 5 | end: usize, | 5 | end: usize, |
| 6 | empty: bool, | 6 | full: bool, |
| 7 | } | 7 | } |
| 8 | 8 | ||
| 9 | impl<const N: usize> RingBuffer<N> { | 9 | impl<const N: usize> RingBuffer<N> { |
| @@ -11,13 +11,13 @@ impl<const N: usize> RingBuffer<N> { | |||
| 11 | Self { | 11 | Self { |
| 12 | start: 0, | 12 | start: 0, |
| 13 | end: 0, | 13 | end: 0, |
| 14 | empty: true, | 14 | full: false, |
| 15 | } | 15 | } |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | pub fn push_buf(&mut self) -> Range<usize> { | 18 | pub fn push_buf(&mut self) -> Range<usize> { |
| 19 | if self.start == self.end && !self.empty { | 19 | if self.is_full() { |
| 20 | trace!(" ringbuf: push_buf empty"); | 20 | trace!(" ringbuf: push_buf full"); |
| 21 | return 0..0; | 21 | return 0..0; |
| 22 | } | 22 | } |
| 23 | 23 | ||
| @@ -38,11 +38,11 @@ impl<const N: usize> RingBuffer<N> { | |||
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | self.end = self.wrap(self.end + n); | 40 | self.end = self.wrap(self.end + n); |
| 41 | self.empty = false; | 41 | self.full = self.start == self.end; |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | pub fn pop_buf(&mut self) -> Range<usize> { | 44 | pub fn pop_buf(&mut self) -> Range<usize> { |
| 45 | if self.empty { | 45 | if self.is_empty() { |
| 46 | trace!(" ringbuf: pop_buf empty"); | 46 | trace!(" ringbuf: pop_buf empty"); |
| 47 | return 0..0; | 47 | return 0..0; |
| 48 | } | 48 | } |
| @@ -64,20 +64,20 @@ impl<const N: usize> RingBuffer<N> { | |||
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | self.start = self.wrap(self.start + n); | 66 | self.start = self.wrap(self.start + n); |
| 67 | self.empty = self.start == self.end; | 67 | self.full = false; |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | pub fn is_full(&self) -> bool { | 70 | pub fn is_full(&self) -> bool { |
| 71 | self.start == self.end && !self.empty | 71 | self.full |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | pub fn is_empty(&self) -> bool { | 74 | pub fn is_empty(&self) -> bool { |
| 75 | self.empty | 75 | self.start == self.end && !self.full |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | #[allow(unused)] | 78 | #[allow(unused)] |
| 79 | pub fn len(&self) -> usize { | 79 | pub fn len(&self) -> usize { |
| 80 | if self.empty { | 80 | if self.is_empty() { |
| 81 | 0 | 81 | 0 |
| 82 | } else if self.start < self.end { | 82 | } else if self.start < self.end { |
| 83 | self.end - self.start | 83 | self.end - self.start |
| @@ -89,7 +89,7 @@ impl<const N: usize> RingBuffer<N> { | |||
| 89 | pub fn clear(&mut self) { | 89 | pub fn clear(&mut self) { |
| 90 | self.start = 0; | 90 | self.start = 0; |
| 91 | self.end = 0; | 91 | self.end = 0; |
| 92 | self.empty = true; | 92 | self.full = false; |
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | fn wrap(&self, n: usize) -> usize { | 95 | fn wrap(&self, n: usize) -> usize { |
diff --git a/embassy-sync/src/zerocopy_channel.rs b/embassy-sync/src/zerocopy_channel.rs index f704cbd5d..cfce9a571 100644 --- a/embassy-sync/src/zerocopy_channel.rs +++ b/embassy-sync/src/zerocopy_channel.rs | |||
| @@ -1,10 +1,7 @@ | |||
| 1 | //! A zero-copy queue for sending values between asynchronous tasks. | 1 | //! A zero-copy queue for sending values between asynchronous tasks. |
| 2 | //! | 2 | //! |
| 3 | //! It can be used concurrently by multiple producers (senders) and multiple | 3 | //! It can be used concurrently by a producer (sender) and a |
| 4 | //! consumers (receivers), i.e. it is an "MPMC channel". | 4 | //! consumer (receiver), i.e. it is an "SPSC channel". |
| 5 | //! | ||
| 6 | //! Receivers are competing for messages. So a message that is received by | ||
| 7 | //! one receiver is not received by any other. | ||
| 8 | //! | 5 | //! |
| 9 | //! This queue takes a Mutex type so that various | 6 | //! This queue takes a Mutex type so that various |
| 10 | //! targets can be attained. For example, a ThreadModeMutex can be used | 7 | //! targets can be attained. For example, a ThreadModeMutex can be used |
