1use crate::driver::life::finish;
2pub mod cancel;
3mod complete;
4mod echo;
5pub mod raw;
6pub mod storage;
7use std::{marker, mem};
8
9pub use complete::Complete;
10use dope_sys::{
11 engine::admit::Cancelled,
12 io::reactor::event::{
13 self, Report, fault,
14 kind::{self, Native, Witness},
15 },
16 io::socket::Create,
17 kernel,
18 table::token::host,
19};
20pub use echo::Echo;
21use o3::base::num::Epoch;
22use route::table;
23use table::conn;
24
25use crate::driver::{
26 self,
27 route::{
28 self,
29 indices::{SlotIndex, SlotOverflow},
30 kind::{Any, Class, Code, Kind},
31 },
32};
33
34use crate::{
35 Backend,
36 driver::{
37 receive,
38 result::poll::{self, fault::Observation},
39 },
40 io::{
41 event::net::accept,
42 fd::handles,
43 transfer::{Len, NonZero},
44 },
45};
46
47pub(crate) const INDEX_BITS: u32 = route::SLOT_BITS;
48pub(crate) const GENERATION_BITS: u32 = 32;
49
50#[derive(Clone, Copy, Debug, Eq, PartialEq)]
51#[repr(C)]
52pub(crate) struct Target {
53 epoch: Epoch,
54 slot: SlotIndex,
55 kind: Native,
56 route: route::Route,
57}
58
59impl Target {
60 fn new<Tag: route::Tag, C: Class + kind::Flight>(target: route::Operation<'_, Tag, C>) -> Self {
61 let parts = target.parts();
62 Self {
63 epoch: parts.epoch(),
64 slot: parts.slot(),
65 kind: C::FLIGHT,
66 route: Tag::ROUTE,
67 }
68 }
69
70 pub(crate) fn bind<'d, C: Class>(
71 self,
72 driver: driver::Invariant<'d>,
73 flight: Echo,
74 ) -> route::Erased<'d, C> {
75 let code = if matches!(C::KIND, Kind::Unspecified) {
76 Code::native(self.kind)
77 } else {
78 C::CODE
79 };
80 route::Erased {
81 token: route::Token::from_flight(self.epoch, self.slot, code, self.route),
82 flight: Some(flight),
83 driver,
84 class: marker::PhantomData,
85 }
86 }
87
88 pub(crate) fn bind_native<'d, K: kind::Flight>(
89 self,
90 driver: driver::Invariant<'d>,
91 flight: Echo,
92 _: Witness<K>,
93 ) -> route::Erased<'d, K> {
94 route::Erased {
95 token: route::Token::from_flight(
96 self.epoch,
97 self.slot,
98 Code::native(K::FLIGHT),
99 self.route,
100 ),
101 flight: Some(flight),
102 driver,
103 class: marker::PhantomData,
104 }
105 }
106}
107
108#[repr(transparent)]
109pub struct Arena(storage::Flights<Target, INDEX_BITS, GENERATION_BITS>);
110
111#[derive(Clone, Copy, Eq, PartialEq)]
113#[repr(transparent)]
114pub struct Capacity(usize);
115
116pub struct Need<Tags, const GROUPS: usize = 1, B = ()> {
118 caps: [Capacity; GROUPS],
119 tags: marker::PhantomData<(Tags, B)>,
120}
121
122#[derive(Clone, Copy, Eq, PartialEq, Debug)]
124pub enum ScaleError {
125 Product {
126 capacity: conn::Capacity,
127 factor: usize,
128 },
129 Limit {
130 actual: usize,
131 maximum: usize,
132 },
133}
134
135impl Capacity {
136 pub const fn fixed<const FLIGHTS: usize>() -> Self {
137 const { assert!(FLIGHTS > 0 && FLIGHTS <= route::SLOT_MASK as usize + 1) };
138 Self(FLIGHTS)
139 }
140
141 pub const fn table(capacity: table::Capacity) -> Self {
142 Self(capacity.count().get())
143 }
144
145 pub const fn connections<const PER_CONNECTION: usize>(
146 capacity: conn::Capacity,
147 ) -> Result<Self, ScaleError> {
148 const { assert!(PER_CONNECTION > 0) };
149 let Some(total) = (capacity.raw() as usize).checked_mul(PER_CONNECTION) else {
150 return Err(ScaleError::Product {
151 capacity,
152 factor: PER_CONNECTION,
153 });
154 };
155 let maximum = route::SLOT_MASK as usize + 1;
156 if total > maximum {
157 return Err(ScaleError::Limit {
158 actual: total,
159 maximum,
160 });
161 }
162 Ok(Self(total))
163 }
164
165 pub const fn capped<const MAX: usize>(capacity: table::Capacity) -> Self {
166 const { assert!(MAX > 0) };
167 let capacity = capacity.count().get();
168 Self(if capacity < MAX { capacity } else { MAX })
169 }
170
171 pub(crate) const fn get(self) -> usize {
172 self.0
173 }
174}
175
176impl<Tag: route::Tag> Need<Tag> {
177 pub const fn new(cap: Capacity) -> Self {
178 Self {
179 caps: [cap],
180 tags: marker::PhantomData,
181 }
182 }
183}
184
185impl<Tag: route::Tag> Need<Tag> {
186 pub fn with_tail<B>(self) -> Need<Tag, 1, B> {
187 Need {
188 caps: self.caps,
189 tags: marker::PhantomData,
190 }
191 }
192}
193
194impl<A: route::Tag> Need<A> {
195 pub fn pair<C: route::Tag, B>(self, other: Need<C, 1, B>) -> Need<(A, C), 2, B> {
196 let Self {
197 caps: [first],
198 tags: marker::PhantomData,
199 } = self;
200 let Need {
201 caps: [second],
202 tags: marker::PhantomData,
203 } = other;
204 Need {
205 caps: [first, second],
206 tags: marker::PhantomData,
207 }
208 }
209}
210
211impl<A: route::Tag, B: route::Tag> Need<(A, B), 2> {
212 pub fn push<C: route::Tag>(self, other: Need<C>) -> Need<(A, B, C), 3> {
213 let Self {
214 caps: [first, second],
215 tags: marker::PhantomData,
216 } = self;
217 let Need {
218 caps: [third],
219 tags: marker::PhantomData,
220 } = other;
221 Need {
222 caps: [first, second, third],
223 tags: marker::PhantomData,
224 }
225 }
226}
227
228impl<Tags, const GROUPS: usize, B> Need<Tags, GROUPS, B> {
229 const fn into_caps(self) -> [Capacity; GROUPS] {
230 self.caps
231 }
232}
233
234#[must_use = "prepared flight slots must be committed"]
235pub struct Prepared<'a, 'd, Tags, const GROUPS: usize = 1, B = ()> {
236 prepared: storage::Prepared<'a, 'd, Target, INDEX_BITS, GENERATION_BITS, GROUPS, B>,
237 driver: driver::Invariant<'d>,
238 tags: marker::PhantomData<Tags>,
239}
240
241#[must_use = "a reserved flight must be committed or released"]
242#[repr(transparent)]
243pub(crate) struct Reserved<'d, C: Class = Any> {
244 reservation: storage::Reserved<'d, Target, INDEX_BITS, GENERATION_BITS>,
245 driver: driver::Invariant<'d>,
246 class: marker::PhantomData<C>,
247}
248
249#[must_use = "a live flight must reach terminal completion or quiescence"]
252#[repr(transparent)]
253pub struct Flight<'d, C: Class = Any> {
254 flight: storage::Lease<'d, Target, INDEX_BITS, GENERATION_BITS>,
255 driver: driver::Invariant<'d>,
256 class: marker::PhantomData<C>,
257}
258
259impl Arena {
260 pub(crate) fn new() -> Self {
261 Self(storage::Flights::new())
262 }
263
264 pub(in crate::driver) fn finish(&self) {
265 self.0.finish();
266 }
267}
268
269impl<'d, Tags, const GROUPS: usize, B> Prepared<'_, 'd, Tags, GROUPS, B> {
270 fn commit(
271 self,
272 ) -> (
273 [storage::raw::Slots<'d, Target, INDEX_BITS, GENERATION_BITS>; GROUPS],
274 driver::Invariant<'d>,
275 ) {
276 let Self {
277 prepared,
278 driver,
279 tags: marker::PhantomData,
280 } = self;
281 let slots = prepared.groups();
282 (slots, driver)
283 }
284}
285
286impl<'d, Tag: route::Tag> Prepared<'_, 'd, Tag> {
287 pub fn slots(self) -> raw::Slots<'d, Tag> {
288 let ([slots], driver) = self.commit();
289 raw::Slots::new(slots, driver)
290 }
291}
292
293impl<'d, A: route::Tag, B: route::Tag> Prepared<'_, 'd, (A, B), 2> {
294 pub fn slots(self) -> (raw::Slots<'d, A>, raw::Slots<'d, B>) {
295 let ([first, second], driver) = self.commit();
296 (
297 raw::Slots::new(first, driver),
298 raw::Slots::new(second, driver),
299 )
300 }
301}
302
303impl<'d, A: route::Tag, B: route::Tag, C: route::Tag> Prepared<'_, 'd, (A, B, C), 3> {
304 pub fn slots(self) -> (raw::Slots<'d, A>, raw::Slots<'d, B>, raw::Slots<'d, C>) {
305 let ([first, second, third], driver) = self.commit();
306 (
307 raw::Slots::new(first, driver),
308 raw::Slots::new(second, driver),
309 raw::Slots::new(third, driver),
310 )
311 }
312}
313
314impl<'d, A: route::Tag, C: route::Tag, B> Prepared<'_, 'd, (A, C), 2, B> {
315 pub fn split(self) -> (raw::Slots<'d, A>, raw::Backing<'d, C, B>) {
316 let ([first, second], driver) = self.commit();
317 (
318 raw::Slots::new(first, driver),
319 raw::Backing::new(second, driver),
320 )
321 }
322}
323
324impl<'d, C: Class> From<Reserved<'d, C>> for Flight<'d, C> {
325 fn from(reserved: Reserved<'d, C>) -> Self {
326 Self {
327 flight: reserved.reservation.commit(),
328 driver: reserved.driver,
329 class: marker::PhantomData,
330 }
331 }
332}
333
334impl host::raw::Drain for Arena {
335 type Driver<'d> = driver::Reference<'d>;
336 type Key = Echo;
337 type Flight<'d> = Complete<'d>;
338 type Fault = fault::Fault<
339 Observation,
340 fault::Subject<route::Token, route::Control, fault::Echo<Echo>, SlotIndex>,
341 fault::Violation<
342 receive::Selection,
343 fault::Echo<Echo>,
344 Len,
345 NonZero,
346 <Backend as kernel::Source>::Returned,
347 SlotIndex,
348 SlotOverflow,
349 table::Capacity,
350 handles::Retirement,
351 >,
352 >;
353 type Reported = event::Reclaimed<
354 Self::Fault,
355 poll::Cause,
356 cancel::Failure,
357 route::Token,
358 handles::PhysicalSlot,
359 accept::Failure,
360 fault::Echo<Echo>,
361 Len,
362 poll::Missing,
363 handles::Reclaimed,
364 Create,
365 >;
366
367 unsafe fn bind<'d>(&'d self, key: Echo, driver: &Self::Driver<'d>) -> Option<Self::Flight<'d>> {
368 let bound = self.0.drain().bind(key.into_inner())?;
369 Some(Complete {
370 completion: bound,
371 driver: *driver,
372 })
373 }
374
375 fn is_pending(&self) -> bool {
376 self.0.is_pending()
377 }
378}
379
380impl<'d, C: Class> Flight<'d, C> {
381 pub(crate) fn is_pending(&self) -> bool {
382 self.flight.is_pending()
383 }
384
385 pub(crate) fn key(&self) -> Echo {
386 Echo::from_inner(self.flight.echo())
387 }
388
389 pub fn target(&self) -> route::Erased<'d, C> {
390 self.flight.value().bind(self.driver, self.key())
391 }
392
393 pub fn matches(&self, target: route::Erased<'d, C, impl route::Selection>) -> bool {
394 target.flight == Some(self.key())
395 }
396
397 pub(in crate::driver) fn finish(
398 mut self,
399 proof: &mut finish::Finish<'_, 'd, impl Report<Event = crate::Reported>>,
400 ) {
401 self.driver = proof.brand();
402 }
403}
404
405const _: () = {
406 assert!(mem::size_of::<Arena>() == 4 * mem::size_of::<usize>());
407 assert!(mem::align_of::<Arena>() == mem::align_of::<usize>());
408 assert!(mem::size_of::<Target>() == mem::size_of::<route::Token>());
409 assert!(mem::align_of::<Target>() == mem::align_of::<route::Token>());
410 assert!(mem::size_of::<Echo>() == mem::size_of::<usize>());
411 assert!(mem::size_of::<Flight<'static>>() == mem::size_of::<usize>());
412 assert!(mem::size_of::<Option<Flight<'static>>>() == mem::size_of::<usize>());
413 assert!(mem::size_of::<Reserved<'static>>() == mem::size_of::<usize>());
414 assert!(mem::size_of::<Complete<'static>>() == 2 * mem::size_of::<usize>());
415 assert!(mem::size_of::<raw::Slots<'static, route::KeyTag<1>>>() == mem::size_of::<usize>());
416};
417
418impl<'d, C: Class> Flight<'d, C> {
419 pub fn try_cancel(
421 &mut self,
422 driver: &mut driver::Local<'d>,
423 ) -> Result<Cancelled, cancel::Error> {
424 driver.native().try_cancel(self)
425 }
426}
427
428impl<'d, C: Class> Flight<'d, C> {
429 pub fn try_stop(
431 &mut self,
432 driver: &mut driver::Local<'d>,
433 ) -> Result<cancel::Outcome, cancel::Failure> {
434 cancel::Outcome::try_from(self.try_cancel(driver))
435 }
436}