Skip to main content

compio_driver/sys/op/general/
poll.rs

1use rustix::io::{pread, preadv, pwrite, pwritev, read, readv, write, writev};
2
3use crate::{Decision, OpType, PollOpCode as OpCode, sys::op::*};
4
5unsafe impl<T: IoBufMut, S: AsFd> OpCode for ReadAt<T, S> {
6    type Control = AioControl;
7
8    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
9        ctrl.init_mut(&self.fd, &mut self.buffer, self.offset);
10    }
11
12    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
13        ctrl.decide_read()
14    }
15
16    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
17        ctrl.op_type()
18    }
19
20    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
21        poll_io(|| {
22            // SAFETY: The kernel only writes initialized bytes.
23            pread(
24                self.fd.as_fd(),
25                unsafe { self.buffer.as_uninit() },
26                self.offset,
27            )
28            .map(|(init, _)| init.len())
29        })
30    }
31}
32
33unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for ReadVectoredAt<T, S> {
34    type Control = AioControl<VectoredControl>;
35
36    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
37        ctrl.init_vec_mut(&self.fd, &mut self.buffer, self.offset);
38    }
39
40    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
41        ctrl.decide_read()
42    }
43
44    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
45        ctrl.op_type()
46    }
47
48    fn operate(&mut self, ctrl: &mut Self::Control) -> Poll<io::Result<usize>> {
49        poll_io(|| preadv(self.fd.as_fd(), ctrl.io_slices_mut(), self.offset))
50    }
51}
52
53unsafe impl<T: IoBuf, S: AsFd> OpCode for WriteAt<T, S> {
54    type Control = AioControl;
55
56    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
57        ctrl.init(&self.fd, &self.buffer, self.offset);
58    }
59
60    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
61        ctrl.decide_write()
62    }
63
64    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
65        ctrl.op_type()
66    }
67
68    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
69        poll_io(|| pwrite(self.fd.as_fd(), self.buffer.as_init(), self.offset))
70    }
71}
72
73unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for WriteVectoredAt<T, S> {
74    type Control = AioControl<VectoredControl>;
75
76    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
77        ctrl.init_vec(&self.fd, &self.buffer, self.offset);
78    }
79
80    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
81        ctrl.decide_write()
82    }
83
84    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
85        ctrl.op_type()
86    }
87
88    fn operate(&mut self, ctrl: &mut Self::Control) -> Poll<io::Result<usize>> {
89        poll_io(|| pwritev(self.fd.as_fd(), ctrl.io_slices(), self.offset))
90    }
91}
92
93unsafe impl<T: IoBufMut, S: AsFd> OpCode for Read<T, S> {
94    type Control = ();
95
96    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
97        Ok(Decision::wait_readable(self.fd.as_fd().as_raw_fd()))
98    }
99
100    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
101        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
102    }
103
104    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
105        poll_io(|| {
106            // SAFETY: The kernel only writes initialized bytes.
107            read(self.fd.as_fd(), unsafe { self.buffer.as_uninit() }).map(|(init, _)| init.len())
108        })
109    }
110}
111
112unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for ReadVectored<T, S> {
113    type Control = VectoredControl;
114
115    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
116        ctrl.slices = self.buffer.sys_slices_mut();
117    }
118
119    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
120        Ok(Decision::wait_readable(self.fd.as_fd().as_raw_fd()))
121    }
122
123    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
124        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
125    }
126
127    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
128        poll_io(|| readv(self.fd.as_fd(), control.io_slices_mut()))
129    }
130}
131
132unsafe impl<T: IoBuf, S: AsFd> OpCode for Write<T, S> {
133    type Control = ();
134
135    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
136        Ok(Decision::wait_writable(self.fd.as_fd().as_raw_fd()))
137    }
138
139    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
140        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
141    }
142
143    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
144        poll_io(|| write(self.fd.as_fd(), self.buffer.as_init()))
145    }
146}
147
148unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for WriteVectored<T, S> {
149    type Control = VectoredControl;
150
151    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
152        ctrl.slices = self.buffer.sys_slices();
153    }
154
155    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
156        Ok(Decision::wait_writable(self.fd.as_fd().as_raw_fd()))
157    }
158
159    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
160        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
161    }
162
163    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
164        poll_io(|| writev(self.fd.as_fd(), control.io_slices()))
165    }
166}
167
168unsafe impl<S: AsFd> OpCode for PollOnce<S> {
169    type Control = ();
170
171    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
172        Ok(Decision::wait_for(
173            self.fd.as_fd().as_raw_fd(),
174            self.interest,
175        ))
176    }
177
178    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
179        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
180    }
181
182    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
183        Poll::Ready(Ok(0))
184    }
185}
186
187unsafe impl OpCode for Pipe {
188    type Control = ();
189
190    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
191        Ok(Decision::Blocking)
192    }
193
194    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
195        Poll::Ready(self.call())
196    }
197}