gpu_async_reactor/host/
uring.rs1use io_uring::{IoUring, opcode, types};
8
9use crate::core::line::CompletionLine;
10use crate::core::reactor::Idle;
11use crate::core::time::{Clock, DurNs, MonoNs};
12use crate::host::std_host::{StdClock, StdIdle};
13
14const ETIME: i32 = 62;
16
17pub struct UringIdle {
19 ring: IoUring,
20 clock: StdClock,
21 park: StdIdle,
22 failed_timeouts: u64,
23}
24
25impl UringIdle {
26 pub fn new(clock: StdClock, park: StdIdle) -> std::io::Result<Self> {
32 Ok(Self {
33 ring: IoUring::new(8)?,
34 clock,
35 park,
36 failed_timeouts: 0,
37 })
38 }
39
40 #[must_use]
43 pub fn failed_timeouts(&self) -> u64 {
44 self.failed_timeouts
45 }
46
47 pub fn probe() -> std::io::Result<()> {
52 IoUring::new(2).map(drop)
53 }
54
55 fn timeout(&mut self, ns: u64) -> std::io::Result<()> {
56 let ts = types::Timespec::new()
57 .sec(ns / 1_000_000_000)
58 .nsec(u32::try_from(ns % 1_000_000_000).unwrap_or(0));
59 let sqe = opcode::Timeout::new(&raw const ts).build();
60 unsafe { self.ring.submission().push(&sqe) }
65 .map_err(|_| std::io::Error::other("submission queue full"))?;
66 self.ring.submit_and_wait(1)?;
67 let cqe = self.ring.completion().next();
68 match cqe.map(|c| c.result()) {
69 Some(r) if r == -ETIME || r >= 0 => Ok(()),
70 Some(r) => Err(std::io::Error::from_raw_os_error(-r)),
71 None => Ok(()),
72 }
73 }
74}
75
76impl Idle for UringIdle {
77 fn sleep_until(&mut self, at: MonoNs) {
78 let left = at.since(self.clock.monotonic_ns());
79 if left.0 > 0 && self.timeout(left.0).is_err() {
80 self.failed_timeouts += 1;
81 self.park.sleep_until(at);
82 }
83 }
84
85 fn park_line<R: Fn(&CompletionLine) -> bool>(
86 &mut self,
87 line: &CompletionLine,
88 ready: R,
89 bound: DurNs,
90 ) -> bool {
91 self.park.park_line(line, ready, bound)
92 }
93}