Skip to content

Commit b47f4a7

Browse files
committed
zephyr: sync: channel: Allow sending of raw messages
Add an unsafe entry to the channel code that allows the message to be allocated previously. This can be useful to send messages from interrupt context, although the message has to be pre-allocate and given to the interrupt handler. Signed-off-by: David Brown <[email protected]>
1 parent a7fff5d commit b47f4a7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

zephyr/src/sync/channel.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ pub struct Message<T> {
5656
}
5757

5858
impl<T> Message<T> {
59-
fn new(data: T) -> Message<T> {
59+
/// Construct a new message from the data.
60+
///
61+
/// This is safe in itself, but sending them is unsafe.
62+
pub fn new(data: T) -> Message<T> {
6063
Message {
6164
_private: 0,
6265
data,
@@ -84,6 +87,16 @@ impl<T> Sender<T> {
8487
}
8588
Ok(())
8689
}
90+
91+
/// Sends a message that has already been boxed. The box will be dropped upon receipt. This is
92+
/// safe to call from interrupt context, and presumably the box will be allocate from a thread.
93+
pub unsafe fn send_boxed(&self, msg: Box<Message<T>>) -> Result<(), SendError<T>> {
94+
let msg = Box::into_raw(msg);
95+
unsafe {
96+
self.queue.send(msg as *mut c_void);
97+
}
98+
Ok(())
99+
}
87100
}
88101

89102
impl<T> Drop for Sender<T> {

0 commit comments

Comments
 (0)