1use core::cell::RefCell;
2
3use alloc::collections::vec_deque::VecDeque;
4
5use async_task::Runnable;
6use embassy_sync::blocking_mutex::{
7 raw::{CriticalSectionRawMutex, RawMutex},
8 Mutex,
9};
10
11extern crate alloc;
12
13pub trait ExecutorQueue: Sized {
21 fn new() -> Self;
23
24 fn push(&self, runnable: Runnable);
26
27 fn pop(&self) -> Option<Runnable>;
31}
32
33pub struct UnboundQueue<M: RawMutex = CriticalSectionRawMutex>(
39 Mutex<M, RefCell<VecDeque<Runnable>>>,
40);
41
42impl<M: RawMutex> Default for UnboundQueue<M> {
43 fn default() -> Self {
44 Self::new()
45 }
46}
47
48impl<M: RawMutex> UnboundQueue<M> {
49 pub const fn new() -> Self {
51 Self(Mutex::new(RefCell::new(VecDeque::new())))
52 }
53
54 pub fn with_capacity(capacity: usize) -> Self {
56 Self(Mutex::new(RefCell::new(VecDeque::with_capacity(capacity))))
57 }
58}
59
60impl<M: RawMutex> ExecutorQueue for UnboundQueue<M>
61where
62 M: RawMutex,
63{
64 fn new() -> Self {
65 Self::new()
66 }
67
68 fn push(&self, runnable: Runnable) {
69 self.0.lock(|rc| {
70 let mut queue = rc.borrow_mut();
71
72 queue.push_back(runnable);
73 })
74 }
75
76 fn pop(&self) -> Option<Runnable> {
77 self.0.lock(|rc| {
78 let mut queue = rc.borrow_mut();
79
80 queue.pop_front()
81 })
82 }
83}
84
85pub struct BoundQueue<const C: usize = 64, M: RawMutex = CriticalSectionRawMutex>(
87 Mutex<M, RefCell<heapless::Deque<Runnable, C>>>,
88);
89
90impl<const C: usize, M: RawMutex> Default for BoundQueue<C, M> {
91 fn default() -> Self {
92 Self::new()
93 }
94}
95
96impl<const C: usize, M: RawMutex> BoundQueue<C, M> {
97 pub const fn new() -> Self {
99 Self(Mutex::new(RefCell::new(heapless::Deque::new())))
100 }
101}
102
103impl<const C: usize, M: RawMutex> ExecutorQueue for BoundQueue<C, M> {
104 fn new() -> Self {
105 Self::new()
106 }
107
108 fn push(&self, runnable: Runnable) {
109 self.0.lock(|rc| {
110 let mut queue = rc.borrow_mut();
111
112 queue
113 .push_back(runnable)
114 .expect("BoundQueue capacity exceeded");
115 })
116 }
117
118 fn pop(&self) -> Option<Runnable> {
119 self.0.lock(|rc| {
120 let mut queue = rc.borrow_mut();
121
122 queue.pop_front()
123 })
124 }
125}