5
5
//! implementation of `zephyr::sys::channel`, which can be used without needing unsafe.
6
6
7
7
use core:: ffi:: c_void;
8
+ use core:: fmt;
9
+ use core:: mem;
8
10
9
11
use zephyr_sys:: {
10
12
k_queue,
@@ -13,13 +15,13 @@ use zephyr_sys::{
13
15
k_queue_get,
14
16
} ;
15
17
18
+ use crate :: error:: Result ;
16
19
use crate :: sys:: K_FOREVER ;
17
- use crate :: object:: { StaticKernelObject , Wrapped } ;
20
+ use crate :: object:: { Fixed , StaticKernelObject , Wrapped } ;
18
21
19
22
/// A wrapper around a Zephyr `k_queue` object.
20
- #[ derive( Clone , Debug ) ]
21
23
pub struct Queue {
22
- item : * mut k_queue ,
24
+ item : Fixed < k_queue > ,
23
25
}
24
26
25
27
unsafe impl Sync for StaticKernelObject < k_queue > { }
@@ -28,6 +30,21 @@ unsafe impl Sync for Queue { }
28
30
unsafe impl Send for Queue { }
29
31
30
32
impl Queue {
33
+ /// Create a new Queue, dynamically allocated.
34
+ ///
35
+ /// This Queue can only be used from system threads.
36
+ ///
37
+ /// **Note**: When a Queue is dropped, any messages that have been added to the queue will be
38
+ /// leaked.
39
+ #[ cfg( CONFIG_RUST_ALLOC ) ]
40
+ pub fn new ( ) -> Result < Queue > {
41
+ let item: Fixed < k_queue > = Fixed :: new ( unsafe { mem:: zeroed ( ) } ) ;
42
+ unsafe {
43
+ k_queue_init ( item. get ( ) ) ;
44
+ }
45
+ Ok ( Queue { item } )
46
+ }
47
+
31
48
/// Append an element to the end of a queue.
32
49
///
33
50
/// This adds an element to the given [`Queue`]. Zephyr requires the
@@ -37,14 +54,14 @@ impl Queue {
37
54
///
38
55
/// [`Message`]: crate::sync::channel::Message
39
56
pub unsafe fn send ( & self , data : * mut c_void ) {
40
- k_queue_append ( self . item , data)
57
+ k_queue_append ( self . item . get ( ) , data)
41
58
}
42
59
43
60
/// Get an element from a queue.
44
61
///
45
62
/// This routine removes the first data item from the [`Queue`].
46
63
pub unsafe fn recv ( & self ) -> * mut c_void {
47
- k_queue_get ( self . item , K_FOREVER )
64
+ k_queue_get ( self . item . get ( ) , K_FOREVER )
48
65
}
49
66
}
50
67
@@ -59,7 +76,7 @@ impl Wrapped for StaticKernelObject<k_queue> {
59
76
k_queue_init ( ptr) ;
60
77
}
61
78
Queue {
62
- item : ptr,
79
+ item : Fixed :: Static ( ptr) ,
63
80
}
64
81
}
65
82
}
@@ -77,3 +94,9 @@ impl Wrapped for StaticKernelObject<k_queue> {
77
94
/// my_queue.send(...);
78
95
/// ```
79
96
pub type StaticQueue = StaticKernelObject < k_queue > ;
97
+
98
+ impl fmt:: Debug for Queue {
99
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
100
+ write ! ( f, "sys::Queue {:?}" , self . item. get( ) )
101
+ }
102
+ }
0 commit comments