|
22 | 22 | //! assert_eq!(rb.dequeue(), Some(0)); |
23 | 23 | //! ``` |
24 | 24 | //! |
25 | | -//! - `Queue` can be `split` and then be used in Single Producer Single Consumer mode |
| 25 | +//! - `Queue` can be `split` and then be used in Single Producer Single Consumer mode. |
26 | 26 | //! |
27 | | -//! ``` |
28 | | -//! use heapless::spsc::Queue; |
| 27 | +//! "no alloc" applications can create a `&'static mut` reference to a `Queue` -- using a static |
| 28 | +//! variable -- and then `split` it: this consumes the static reference. The resulting `Consumer` |
| 29 | +//! and `Producer` can then be moved into different execution contexts (threads, interrupt handlers, |
| 30 | +//! etc.) |
29 | 31 | //! |
30 | | -//! // Notice, type signature needs to be explicit for now. |
31 | | -//! // (min_const_eval, does not allow for default type assignments) |
32 | | -//! static mut Q: Queue<Event, 4> = Queue::new(); |
| 32 | +//! ``` |
| 33 | +//! use heapless::spsc::{Producer, Queue}; |
33 | 34 | //! |
34 | 35 | //! enum Event { A, B } |
35 | 36 | //! |
36 | 37 | //! fn main() { |
37 | | -//! // NOTE(unsafe) beware of aliasing the `consumer` end point |
38 | | -//! let mut consumer = unsafe { Q.split().1 }; |
| 38 | +//! let queue: &'static mut Queue<Event, 4> = { |
| 39 | +//! static mut Q: Queue<Event, 4> = Queue::new(); |
| 40 | +//! unsafe { &mut Q } |
| 41 | +//! }; |
| 42 | +//! |
| 43 | +//! let (producer, mut consumer) = queue.split(); |
| 44 | +//! |
| 45 | +//! // `producer` can be moved into `interrupt_handler` using a static mutex or the mechanism |
| 46 | +//! // provided by the concurrency framework you are using (e.g. a resource in RTIC) |
39 | 47 | //! |
40 | 48 | //! loop { |
41 | | -//! // `dequeue` is a lockless operation |
42 | 49 | //! match consumer.dequeue() { |
43 | 50 | //! Some(Event::A) => { /* .. */ }, |
44 | 51 | //! Some(Event::B) => { /* .. */ }, |
|
49 | 56 | //! } |
50 | 57 | //! |
51 | 58 | //! // this is a different execution context that can preempt `main` |
52 | | -//! fn interrupt_handler() { |
53 | | -//! // NOTE(unsafe) beware of aliasing the `producer` end point |
54 | | -//! let mut producer = unsafe { Q.split().0 }; |
| 59 | +//! fn interrupt_handler(producer: &mut Producer<'static, Event, 4>) { |
55 | 60 | //! # let condition = true; |
56 | 61 | //! |
57 | 62 | //! // .. |
|
0 commit comments