1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::{mem, num, pin};
use dope_sys::{
engine::ring::{Backend as _, Context},
io::{
reactor::submit::Queue as _,
socket::addr,
socket::{
option,
option::tuning::{self, device},
},
},
ring::ready::Ready as _,
table::token::{self, Codec, Tuning as _},
};
use o3::depot::capacity;
use crate::{
Engine,
driver::{
Key,
tuning::{Occupied, State, Table},
},
reactor::sq,
};
pub(crate) struct Vacant<'a, T: Codec, F, const N: usize> {
state: pin::Pin<&'a mut State<Key<T>, N>>,
fd: F,
slot: sq::Fixed,
token: T,
}
impl<T: Codec, F, const N: usize> Vacant<'_, T, F, N> {
/// # Safety
/// The cursor holds exactly `count` options for this descriptor and its admitted queue.
/// Retain captured terminal owners and the pinned table through completion or quiescence.
pub(crate) unsafe fn try_submit<R, E>(
mut self,
queue: sq::raw::Queue<'_, T>,
count: capacity::Limit<N>,
options: impl option::Cursor,
last: Option<addr::Addr<'_>>,
reserve: impl FnOnce() -> Result<(Key<T>, R), E>,
) -> Result<(F, R), tuning::Error<F, E>> {
use tuning::raw::Chain as _;
let ready = match queue.try_reserve(count) {
Ok(ready) => ready,
Err(error) => return Err(tuning::Error::Full(self.fd, error)),
};
let (key, target) = match reserve() {
Ok(target) => target,
Err(error) => return Err(tuning::Error::Reserve(self.fd, error)),
};
// SAFETY: the host retains the CONNECT address through the same flight;
// it is submitted only after every option CQE has succeeded.
let terminal = last.map(|last| unsafe { sq::raw::Deferred::new(self.slot, last) });
// SAFETY: construction retains the affine slot proof and pinned table.
let values = unsafe { self.state.as_mut().begin(key, count.count(), terminal) };
// SAFETY: admission bound the vacant state, target and token together;
// the caller retains the exact cursor and terminal through completion.
unsafe { ready.submit(values, options, self.slot, self.token) };
Ok((self.fd, target))
}
pub(crate) fn try_peer<R, E>(
mut self,
queue: sq::raw::Queue<'_, T>,
reserve: impl FnOnce() -> Result<(Key<T>, R), E>,
) -> Result<(F, R), tuning::Error<F, E>> {
use dope_sys::io::reactor::submit::{Ready as _, raw::Vacancy as _};
let ready = match queue.try_one() {
Ok(ready) => ready,
Err(error) => return Err(tuning::Error::Full(self.fd, error)),
};
let (key, target) = match reserve() {
Ok(target) => target,
Err(error) => return Err(tuning::Error::Reserve(self.fd, error)),
};
// SAFETY: the affine host admits this vacant, pinned slot and retains
// its output through completion or native quiescence, including on drop.
let _ = unsafe {
self.state
.as_mut()
.begin(key, num::NonZero::<u32>::MIN, None)
};
// SAFETY: this projection initializes the query phase without moving its output.
let state = unsafe { self.state.get_unchecked_mut() };
state.status.phase = super::Phase::Peer;
let request = crate::reactor::op::sockets::peer_uid(self.slot, &mut state.values)
.user_data(self.token);
// SAFETY: request borrows only this retained pinned output; ready owns native capacity.
unsafe { ready.borrow(&request) }.push();
Ok((self.fd, target))
}
/// Consumes the host's exact backend and affine descriptor admission.
pub(crate) fn admit<'a, C>(
admitted: device::Admission<
'a,
Engine<C>,
F,
sq::Fixed,
<T::Context as token::Context>::Slot,
>,
) -> (sq::raw::Queue<'a, T>, Vacant<'a, T, F, N>)
where
C: Context<Token = T, Tuning = Table<Key<T>, N>>,
T: 'a,
{
let (backend, fd, slot, index) = admitted.into_parts();
let (ring, table) = backend.split();
// SAFETY: Admission can only be created by the unsafe host root. Its
// affine descriptor was admitted inside this backend's immutable file
// and tuning extent, and both targets came from that descriptor. The
// host retains it until a previous terminal clears the pinned state,
// so this exact entry is vacant. The exclusive backend borrow prevents
// another projection or submission until this capability is consumed.
let state = unsafe { table.admitted(slot) };
let vacant = Vacant {
state,
fd,
slot,
token: T::tuning(T::Tuning::new(index)),
};
(ring.submission(), vacant)
}
}
// SAFETY: projection never moves the values, and only the consumed terminal
// owner can clear the echo that marks their linked chain occupied.
unsafe impl<E: Copy, const N: usize> tuning::raw::State<E, N> for State<E, N> {
type Occupied<'a>
= Occupied<'a, E, N>
where
Self: 'a;
fn bind(self: pin::Pin<&mut Self>) -> Option<Self::Occupied<'_>> {
const {
assert!(
mem::size_of::<Occupied<'_, E, N>>() == mem::size_of::<(pin::Pin<&mut Self>, E)>()
);
assert!(
mem::align_of::<Occupied<'_, E, N>>()
== mem::align_of::<(pin::Pin<&mut Self>, E)>()
);
};
let target = self.as_ref().target?;
Some(Occupied {
state: self,
target,
})
}
unsafe fn values(
self: pin::Pin<&mut Self>,
target: E,
count: num::NonZero<u32>,
) -> &mut [libc::c_int; N] {
// SAFETY: the caller proves this exact option count and retains the
// pinned owner until every corresponding native CQE has arrived.
unsafe { self.begin(target, count, None) }
}
}