Skip to content

Commit c71ed4b

Browse files
committed
zephyr: Add sys::Queue
`sys::Queue` is a thin wrapper on Zephyr's `k_queue` with support for static allocation. Signed-off-by: David Brown <[email protected]>
1 parent fb98014 commit c71ed4b

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

zephyr/src/object.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ macro_rules! _kobj_rule {
233233
($v:vis, $name:ident, [ThreadStack<{$size:expr}>; $asize:expr]) => {
234234
$crate::_kobj_stack!($v, $name, $size, $asize);
235235
};
236+
237+
// Queues.
238+
($v:vis, $name: ident, StaticQueue) => {
239+
#[link_section = concat!("._k_queue.static.", stringify!($name), ".", file!(), line!())]
240+
$v static $name: $crate::sys::queue::StaticQueue =
241+
unsafe { ::core::mem::zeroed() };
242+
};
243+
244+
($v:vis, $name: ident, [StaticQueue; $size:expr]) => {
245+
#[link_section = concat!("._k_queue.static.", stringify!($name), ".", file!(), line!())]
246+
$v static $name: [$crate::sys::queue::StaticQueue; $size] =
247+
unsafe { ::core::mem::zeroed() };
248+
};
236249
}
237250

238251
#[doc(hidden)]

zephyr/src/sys.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
pub mod sync;
1313
pub mod thread;
14+
pub mod queue;
1415

1516
use zephyr_sys::k_timeout_t;
1617

zephyr/src/sys/queue.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Lightweight wrapper around Zephyr's `k_queue`.
2+
//!
3+
//! The underlying operations on the `k_queue` are all unsafe, as the model does not match the
4+
//! borrowing model that Rust expects. This module is mainly intended to be used by the
5+
//! implementation of `zephyr::sys::channel`, which can be used without needing unsafe.
6+
7+
use core::ffi::c_void;
8+
9+
use zephyr_sys::{
10+
k_queue,
11+
k_queue_init,
12+
k_queue_append,
13+
k_queue_get,
14+
};
15+
16+
use crate::sys::K_FOREVER;
17+
use crate::object::{KobjInit, StaticKernelObject};
18+
19+
#[derive(Clone, Debug)]
20+
pub struct Queue {
21+
pub item: *mut k_queue,
22+
}
23+
24+
unsafe impl Sync for StaticKernelObject<k_queue> { }
25+
26+
unsafe impl Sync for Queue { }
27+
unsafe impl Send for Queue { }
28+
29+
impl Queue {
30+
pub unsafe fn send(&self, data: *mut c_void) {
31+
k_queue_append(self.item, data)
32+
}
33+
34+
pub unsafe fn recv(&self) -> *mut c_void {
35+
k_queue_get(self.item, K_FOREVER)
36+
}
37+
}
38+
39+
impl KobjInit<k_queue, Queue> for StaticKernelObject<k_queue> {
40+
fn wrap(ptr: *mut k_queue) -> Queue {
41+
Queue { item: ptr }
42+
}
43+
}
44+
45+
pub type StaticQueue = StaticKernelObject<k_queue>;
46+
47+
impl StaticQueue {
48+
pub fn init(&self) {
49+
self.init_help(|raw| {
50+
unsafe {
51+
k_queue_init(raw);
52+
}
53+
})
54+
}
55+
}

0 commit comments

Comments
 (0)