|
| 1 | +//! Close-to-Zephyr channels |
| 2 | +//! |
| 3 | +//! This module attempts to provide a mechanism as close as possible to `crossbeam-channel` as we |
| 4 | +//! can get, directly using Zephyr primitives. |
| 5 | +//! |
| 6 | +//! The channels are built around `k_queue` in Zephyr. As is the case with most Zephyr types, |
| 7 | +//! these are typically statically allocated. Similar to the other close-to-zephyr primitives, |
| 8 | +//! this means that there is a constructor that can directly take one of these primitives. |
| 9 | +//! |
| 10 | +//! In other words, `zephyr::sys::Queue` is a Rust friendly implementation of `k_queue` in Zephyr. |
| 11 | +//! This module provides `Sender` and `Receiver`, which can be cloned and behave as if they had an |
| 12 | +//! internal `Arc` inside them, but without the overhead of an actual Arc. |
| 13 | +
|
| 14 | +extern crate alloc; |
| 15 | + |
| 16 | +use alloc::boxed::Box; |
| 17 | + |
| 18 | +use core::ffi::c_void; |
| 19 | +use core::fmt; |
| 20 | +use core::marker::PhantomData; |
| 21 | + |
| 22 | +use crate::sys::queue::Queue; |
| 23 | + |
| 24 | +mod counter; |
| 25 | + |
| 26 | +// The zephyr queue does not allocate or manage the data of the messages, so we need to handle |
| 27 | +// allocation as such as well. However, we don't need to manage anything, so it is sufficient to |
| 28 | +// simply Box the message, leak it out of the box, and give it to Zephyr, and then on receipt, wrap |
| 29 | +// it back into a Box, and give it to the recipient. |
| 30 | + |
| 31 | +/// Create a multi-producer multi-consumer channel of unbounded capacity. The messages are |
| 32 | +/// allocated individually as "Box", and the queue is managed by the underlying Zephyr queue. |
| 33 | +pub fn unbounded_from<T>(queue: Queue) -> (Sender<T>, Receiver<T>) { |
| 34 | + let (s, r) = counter::new(queue); |
| 35 | + let s = Sender { |
| 36 | + queue: s, |
| 37 | + _phantom: PhantomData, |
| 38 | + }; |
| 39 | + let r = Receiver { |
| 40 | + queue: r, |
| 41 | + _phantom: PhantomData, |
| 42 | + }; |
| 43 | + (s, r) |
| 44 | +} |
| 45 | + |
| 46 | +#[repr(C)] |
| 47 | +struct Message<T> { |
| 48 | + /// The private data used by the kernel to enqueue messages and such. |
| 49 | + _private: usize, |
| 50 | + /// The actual data being transported. |
| 51 | + data: T, |
| 52 | +} |
| 53 | + |
| 54 | +impl<T> Message<T> { |
| 55 | + fn new(data: T) -> Message<T> { |
| 56 | + Message { |
| 57 | + _private: 0, |
| 58 | + data, |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// The sending side of a channel. |
| 64 | +pub struct Sender<T> { |
| 65 | + queue: counter::Sender<Queue>, |
| 66 | + _phantom: PhantomData<T>, |
| 67 | +} |
| 68 | + |
| 69 | +unsafe impl<T: Send> Send for Sender<T> {} |
| 70 | +unsafe impl<T: Send> Sync for Sender<T> {} |
| 71 | + |
| 72 | +impl<T> Sender<T> { |
| 73 | + /// Sends a message over the given channel. This will perform an alloc of the message, which |
| 74 | + /// will have an accompanied free on the recipient side. |
| 75 | + pub fn send(&self, msg: T) -> Result<(), SendError<T>> { |
| 76 | + let msg = Box::new(Message::new(msg)); |
| 77 | + let msg = Box::into_raw(msg); |
| 78 | + unsafe { |
| 79 | + self.queue.send(msg as *mut c_void); |
| 80 | + } |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<T> Drop for Sender<T> { |
| 86 | + fn drop(&mut self) { |
| 87 | + unsafe { |
| 88 | + self.queue.release(|_| { |
| 89 | + crate::printkln!("Release"); |
| 90 | + true |
| 91 | + }) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl<T> Clone for Sender<T> { |
| 97 | + fn clone(&self) -> Self { |
| 98 | + Sender { |
| 99 | + queue: self.queue.acquire(), |
| 100 | + _phantom: PhantomData, |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl<T: fmt::Debug> fmt::Debug for Sender<T> { |
| 106 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 107 | + write!(f, "Sender {:?}", *self.queue) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/// The receiving side of a channel. |
| 112 | +pub struct Receiver<T> { |
| 113 | + queue: counter::Receiver<Queue>, |
| 114 | + _phantom: PhantomData<T>, |
| 115 | +} |
| 116 | + |
| 117 | +unsafe impl<T: Send> Send for Receiver<T> {} |
| 118 | +unsafe impl<T: Send> Sync for Receiver<T> {} |
| 119 | + |
| 120 | +impl<T> Receiver<T> { |
| 121 | + /// Blocks the current thread until a message is received or the channel is empty and |
| 122 | + /// disconnected. |
| 123 | + /// |
| 124 | + /// If the channel is empty and not disconnected, this call will block until the receive |
| 125 | + /// operation can proceed. If the channel is empty and becomes disconnected, this call will |
| 126 | + /// wake up and return an error. |
| 127 | + pub fn recv(&self) -> Result<T, RecvError> { |
| 128 | + let msg = unsafe { |
| 129 | + self.queue.recv() |
| 130 | + }; |
| 131 | + let msg = msg as *mut Message<T>; |
| 132 | + let msg = unsafe { Box::from_raw(msg) }; |
| 133 | + Ok(msg.data) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl<T> Drop for Receiver<T> { |
| 138 | + fn drop(&mut self) { |
| 139 | + unsafe { |
| 140 | + self.queue.release(|_| { |
| 141 | + crate::printkln!("Release"); |
| 142 | + true |
| 143 | + }) |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl<T> Clone for Receiver<T> { |
| 149 | + fn clone(&self) -> Self { |
| 150 | + Receiver { |
| 151 | + queue: self.queue.acquire(), |
| 152 | + _phantom: PhantomData, |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl<T> fmt::Debug for Receiver<T> { |
| 158 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 159 | + write!(f, "Sender {:?}", *self.queue) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// TODO: Move to err |
| 164 | + |
| 165 | +/// An error returned from the [`send`] method. |
| 166 | +/// |
| 167 | +/// The message could not be sent because the channel is disconnected. |
| 168 | +/// |
| 169 | +/// The error contains the message so it can be recovered. |
| 170 | +#[derive(PartialEq, Eq, Clone, Copy)] |
| 171 | +pub struct SendError<T>(pub T); |
| 172 | + |
| 173 | +impl<T> fmt::Debug for SendError<T> { |
| 174 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 175 | + "SendError(..)".fmt(f) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +/// An error returned from the [`recv`] method. |
| 180 | +/// |
| 181 | +/// A message could not be received because the channel is empty and disconnected. |
| 182 | +#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
| 183 | +pub struct RecvError; |
0 commit comments