Skip to main content

dope_uring/driver/tuning/
progress.rs

1use dope_sys::{
2    io::{reactor::submit::Queue as _, socket::option::tuning},
3    table::token::{self, Codec, Tuning as _},
4};
5use o3::base::num::bounded;
6
7use crate::{
8    driver::{Key, tuning::Table},
9    reactor::sq,
10};
11
12/// Native option completions retain their host flight until every submitted
13/// scalar has completed and, when requested, the separate connect has finished.
14pub trait Progress<T: Codec> {
15    fn next(&mut self, queue: sq::raw::Queue<'_, T>) -> Option<Option<T::Tuning>>;
16    fn has_pending(&self) -> bool;
17    fn cancel_all(&mut self);
18    fn cancel_at(&mut self, slot: sq::Fixed, target: Option<Key<T>>) -> Option<&mut bool>;
19}
20
21impl<T: Codec, const N: usize> Progress<T> for Table<Key<T>, N> {
22    fn next(&mut self, queue: sq::raw::Queue<'_, T>) -> Option<Option<T::Tuning>> {
23        let slot = self.ready?;
24        // SAFETY: `ready` holds a slot admitted by `get`; only scalar state
25        // and the deferred request move, retaining every pinned option value.
26        let state = unsafe { self.admitted(slot).get_unchecked_mut() };
27        let stopped = state.status.stopped;
28        if !stopped {
29            let vacancy = queue.try_acquire().ok()?;
30            // SAFETY: `complete` links only an initialized, unread CONNECT.
31            // This consumes it once; its host retains every encoded address
32            // until the terminal CQE, including after this link is released.
33            let entry = unsafe { state.connect.assume_init_read() };
34            entry.submit(vacancy, T::tuning(tune::<T>(slot)));
35        }
36        self.ready = state.next.take();
37        if self.ready.is_none() {
38            self.tail = None;
39        }
40        Some(if stopped { Some(tune::<T>(slot)) } else { None })
41    }
42
43    fn has_pending(&self) -> bool {
44        self.ready.is_some()
45    }
46
47    fn cancel_all(&mut self) {
48        // SAFETY: changing cancellation intent moves no pinned scalar.
49        for state in unsafe { self.states.as_mut().get_unchecked_mut() } {
50            if state.target.is_some() {
51                state.status.stopped = true;
52            }
53        }
54    }
55
56    fn cancel_at(&mut self, slot: sq::Fixed, target: Option<Key<T>>) -> Option<&mut bool> {
57        let state = tuning::Table::get(self, slot.index())?;
58        // SAFETY: returning the cancellation field cannot move pinned values.
59        let state = unsafe { state.get_unchecked_mut() };
60        let current = state.target?;
61        if target.is_some() && target != Some(current) {
62            return None;
63        }
64        Some(&mut state.status.stopped)
65    }
66}
67
68fn tune<T: Codec>(slot: sq::Fixed) -> T::Tuning {
69    let index: <T::Context as token::Context>::Slot =
70        bounded::U32::clamp(slot.index() as usize).into();
71    T::Tuning::new(index)
72}