Skip to content

Commit 180126e

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 347ffdc commit 180126e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

zephyr/src/sync/channel.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ pub struct Message<T> {
7171
}
7272

7373
impl<T> Message<T> {
74-
fn new(data: T) -> Message<T> {
74+
/// Construct a new message from the data.
75+
///
76+
/// This is safe in itself, but sending them is unsafe.
77+
pub fn new(data: T) -> Message<T> {
7578
Message {
7679
_private: 0,
7780
data,
@@ -99,6 +102,16 @@ impl<T> Sender<T> {
99102
}
100103
Ok(())
101104
}
105+
106+
/// Sends a message that has already been boxed. The box will be dropped upon receipt. This is
107+
/// safe to call from interrupt context, and presumably the box will be allocate from a thread.
108+
pub unsafe fn send_boxed(&self, msg: Box<Message<T>>) -> Result<(), SendError<T>> {
109+
let msg = Box::into_raw(msg);
110+
unsafe {
111+
self.queue.send(msg as *mut c_void);
112+
}
113+
Ok(())
114+
}
102115
}
103116

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

0 commit comments

Comments
 (0)