Skip to content

Commit 7ac4c94

Browse files
committed
zephyr: sys: queue: Add timeout to queue recv
Add an argument to the `recv` method on Queue to allow a timeout to be specified. This will allow channels to have timeout variants available. Signed-off-by: David Brown <[email protected]>
1 parent 2326717 commit 7ac4c94

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

zephyr/src/sync/channel.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use core::marker::PhantomData;
2222
use core::mem::MaybeUninit;
2323

2424
use crate::sys::queue::Queue;
25+
use crate::time::Forever;
2526

2627
mod counter;
2728

@@ -130,7 +131,7 @@ impl<T> Sender<T> {
130131
}
131132
SenderFlavor::Bounded(chan) => {
132133
// Retrieve a message buffer from the free list.
133-
let buf = unsafe { chan.free.recv() };
134+
let buf = unsafe { chan.free.recv(Forever) };
134135
let buf = buf as *mut Message<T>;
135136
unsafe {
136137
buf.write(Message::new(msg));
@@ -217,15 +218,15 @@ impl<T> Receiver<T> {
217218
match &self.flavor {
218219
ReceiverFlavor::Unbounded { queue, .. } => {
219220
let msg = unsafe {
220-
queue.recv()
221+
queue.recv(Forever)
221222
};
222223
let msg = msg as *mut Message<T>;
223224
let msg = unsafe { Box::from_raw(msg) };
224225
Ok(msg.data)
225226
}
226227
ReceiverFlavor::Bounded(chan) => {
227228
let rawbuf = unsafe {
228-
chan.chan.recv()
229+
chan.chan.recv(Forever)
229230
};
230231
let buf = rawbuf as *mut Message<T>;
231232
let msg: Message<T> = unsafe { buf.read() };

zephyr/src/sys/queue.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use zephyr_sys::{
1818

1919
#[cfg(CONFIG_RUST_ALLOC)]
2020
use crate::error::Result;
21-
use crate::sys::K_FOREVER;
2221
use crate::object::{Fixed, StaticKernelObject, Wrapped};
22+
use crate::time::Timeout;
2323

2424
/// A wrapper around a Zephyr `k_queue` object.
2525
pub struct Queue {
@@ -62,8 +62,14 @@ impl Queue {
6262
/// Get an element from a queue.
6363
///
6464
/// This routine removes the first data item from the [`Queue`].
65-
pub unsafe fn recv(&self) -> *mut c_void {
66-
k_queue_get(self.item.get(), K_FOREVER)
65+
/// The timeout value can be [`Forever`] to block until there is a message, [`NoWait`] to check
66+
/// and immediately return if there is no message, or a [`Duration`] to indicate a specific
67+
/// timeout.
68+
pub unsafe fn recv<T>(&self, timeout: T) -> *mut c_void
69+
where T: Into<Timeout>
70+
{
71+
let timeout: Timeout = timeout.into();
72+
k_queue_get(self.item.get(), timeout.0)
6773
}
6874
}
6975

0 commit comments

Comments
 (0)