dope_core/driver/schedule/ready/wake/
queue.rs1use std::{mem, num};
2
3use o3::depot::{
4 self, capacity,
5 queue::resident::{
6 self,
7 fifo::{Index, Scope},
8 },
9};
10
11use crate::{
12 driver,
13 driver::schedule::{
14 app,
15 ready::{
16 self, wake,
17 wake::{
18 Drain, Once, Waker,
19 fanout::storage::Step,
20 resident::{drain, one},
21 },
22 },
23 },
24};
25
26pub struct Queue<'d, L = num::NonZero<usize>> {
28 driver: driver::Reference<'d>,
29 inner: resident::Lanes<ready::Retained<'d>, L>,
30}
31
32#[repr(transparent)]
34pub struct Registration<'target, 'd: 'target, L = num::NonZero<usize>> {
35 pub(super) inner: resident::Lease<'target, ready::Retained<'d>, L>,
36}
37
38#[derive(Clone, Copy, Debug, Eq, PartialEq)]
39#[must_use = "a rejected wake registration must be handled"]
40pub enum Error {
41 Lane(resident::Bounds<num::NonZero<u32>>),
42 Active { position: wake::Position },
43 Draining { position: wake::Position },
44 Full { position: wake::Position },
45}
46
47impl From<resident::Reject> for Error {
48 fn from(error: resident::Reject) -> Self {
49 match error {
50 resident::Reject::Active { index } => Self::Active {
51 position: wake::Position::new(index),
52 },
53 resident::Reject::Draining { index } => Self::Draining {
54 position: wake::Position::new(index),
55 },
56 resident::Reject::Full { index } => Self::Full {
57 position: wake::Position::new(index),
58 },
59 }
60 }
61}
62
63impl<'d, L> Queue<'d, L> {
64 pub fn try_new(
65 driver: driver::Reference<'d>,
66 capacity: usize,
67 lanes: L,
68 ) -> Result<Self, resident::Error>
69 where
70 L: resident::Extent,
71 {
72 Ok(Self {
73 driver,
74 inner: resident::Lanes::try_new(capacity, lanes)?,
75 })
76 }
77}
78
79impl<'d> Queue<'d> {
80 pub fn wake_one(source: depot::raw::Proof<(&Self, usize), capacity::Physical>) -> Once {
82 let (queue, index) = source.into_inner();
83 let lane =
85 resident::raw::View::lane(unsafe { depot::raw::Proof::new((&queue.inner, index)) });
86 one(queue.driver, lane.take_one())
87 }
88
89 pub fn try_with<S, R>(
91 driver: driver::Reference<'d>,
92 capacity: usize,
93 lanes: num::NonZero<usize>,
94 source: S,
95 run: impl for<'scope> FnOnce(&'scope S, Queue<'d, Scope<'scope>>) -> R,
96 ) -> Result<R, resident::Error> {
97 resident::Lanes::try_with(capacity, lanes, source, |source, inner| {
98 run(source, Queue { driver, inner })
99 })
100 }
101
102 pub fn try_wake_one(
103 &self,
104 position: usize,
105 ) -> Result<Once, resident::Bounds<num::NonZero<u32>>> {
106 let lane = self.inner.try_lane(position)?;
107 Ok(one(self.driver, lane.take_one()))
108 }
109}
110
111impl<'scope, 'd> Queue<'d, Scope<'scope>> {
112 pub fn try_index(
113 &self,
114 position: usize,
115 ) -> Result<Index<'scope>, resident::Bounds<num::NonZero<u32>>> {
116 self.inner.try_index(position)
117 }
118
119 pub fn wake_one(&self, index: Index<'scope>) -> Once {
120 one(self.driver, self.inner.lane(index).take_one())
121 }
122}
123
124impl<'d, const N: usize> Queue<'d, resident::Fixed<N>> {
125 pub fn wake_one<const I: usize>(&self) -> Once {
126 one(self.driver, self.inner.lane::<I>().take_one())
127 }
128}
129
130impl<'d, L> Queue<'d, L> {
131 pub fn cancel(&self, slot: &mut Registration<'_, 'd, L>) -> Once {
134 one(self.driver, slot.inner.cancel())
135 }
136
137 pub fn wake_first(&self) -> Once {
139 one(self.driver, self.inner.first().take_one())
140 }
141
142 pub fn wake_next(&self) -> Once {
143 one(self.driver, self.inner.take_next())
144 }
145
146 pub fn has_waiters(&self) -> bool {
147 self.inner.has_waiters()
148 }
149
150 pub fn begin_drain(&self) {
151 self.inner.begin_drain();
152 }
153
154 pub fn is_draining(&self) -> bool {
155 self.inner.is_draining()
156 }
157
158 pub fn drain_one(&self, work: app::Work<'_, 'd>) -> Drain {
159 drain::one(
160 self.driver,
161 work,
162 || self.inner.is_draining() && self.inner.has_waiters(),
163 || self.inner.drain_next().map(Step::Item),
164 )
165 }
166}
167
168impl<'target, 'd> Registration<'target, 'd> {
169 pub fn try_keep(
171 &mut self,
172 source: depot::raw::Proof<(&'target Queue<'d>, usize), capacity::Physical>,
173 wake: &Waker<'d>,
174 ) -> Result<(), resident::Reject> {
175 let (queue, index) = source.into_inner();
176 let lane =
178 resident::raw::View::lane(unsafe { depot::raw::Proof::new((&queue.inner, index)) });
179 lane.try_keep(&mut self.inner, *wake.key())
180 }
181
182 pub fn try_register(
183 &mut self,
184 queue: &'target Queue<'d>,
185 position: usize,
186 wake: &Waker<'d>,
187 ) -> Result<(), Error> {
188 let lane = queue.inner.try_lane(position).map_err(Error::Lane)?;
189 lane.try_keep(&mut self.inner, *wake.key())
190 .map_err(Error::from)
191 }
192}
193
194impl<'target, 'scope, 'd> Registration<'target, 'd, Scope<'scope>> {
195 pub fn try_register(
196 &mut self,
197 queue: &'target Queue<'d, Scope<'scope>>,
198 index: Index<'scope>,
199 wake: &Waker<'d>,
200 ) -> Result<(), resident::Reject> {
201 queue
202 .inner
203 .lane(index)
204 .try_keep(&mut self.inner, *wake.key())
205 }
206}
207
208impl<'target, 'd, const N: usize> Registration<'target, 'd, resident::Fixed<N>> {
209 pub fn try_register<const I: usize>(
210 &mut self,
211 queue: &'target Queue<'d, resident::Fixed<N>>,
212 wake: &Waker<'d>,
213 ) -> Result<(), resident::Reject> {
214 queue
215 .inner
216 .lane::<I>()
217 .try_keep(&mut self.inner, *wake.key())
218 }
219}
220
221impl<'target, 'd, L> Registration<'target, 'd, L> {
222 pub fn try_first(
224 &mut self,
225 queue: &'target Queue<'d, L>,
226 wake: &Waker<'d>,
227 ) -> Result<(), resident::Reject> {
228 queue.inner.first().try_keep(&mut self.inner, *wake.key())
229 }
230}
231
232impl<L> Default for Registration<'_, '_, L> {
233 fn default() -> Self {
234 Self::new()
235 }
236}
237
238impl<L> Registration<'_, '_, L> {
239 pub const fn new() -> Self {
240 Self {
241 inner: resident::Lease::new(),
242 }
243 }
244
245 pub fn is_active(&self) -> bool {
246 self.inner.is_active()
247 }
248
249 pub fn clear(&mut self) {
250 *self = Self::new();
251 }
252}
253
254const _: () = {
255 assert!(mem::size_of::<Registration<'static, 'static>>() == 2 * mem::size_of::<usize>());
256 assert!(mem::align_of::<Registration<'static, 'static>>() == mem::align_of::<usize>());
257 assert!(
258 size_of::<Registration<'static, 'static, resident::Fixed<1>>>() == 2 * size_of::<usize>()
259 );
260 assert!(size_of::<Queue<'static, resident::Fixed<1>>>() == size_of::<Queue<'static>>());
261 assert!(size_of::<Queue<'static, Scope<'static>>>() == size_of::<Queue<'static>>());
262 assert!(size_of::<Index<'static>>() == size_of::<u32>());
263 assert!(size_of::<Registration<'static, 'static, Scope<'static>>>() == 2 * size_of::<usize>());
264};