1
0
Fork 0
forked from wry/wry

io-uring: add readable/writable

This commit is contained in:
Julian Orth 2022-05-12 20:33:58 +02:00
parent 25d817b722
commit dcdd91c0b0
31 changed files with 285 additions and 189 deletions

View file

@ -1,2 +1,21 @@
use crate::{io_uring::IoUringError, utils::oserror::OsError};
pub mod async_cancel;
pub mod poll;
pub mod write;
pub type TaskResult<T> = Result<Result<T, OsError>, IoUringError>;
pub trait TaskResultExt<T> {
fn merge(self) -> Result<T, IoUringError>;
}
impl<T> TaskResultExt<T> for TaskResult<T> {
fn merge(self) -> Result<T, IoUringError> {
match self {
Ok(Ok(t)) => Ok(t),
Ok(Err(e)) => Err(IoUringError::OsError(e)),
Err(e) => Err(e),
}
}
}

74
src/io_uring/ops/poll.rs Normal file
View file

@ -0,0 +1,74 @@
use {
crate::io_uring::{
ops::TaskResult,
pending_result::PendingResult,
sys::{io_uring_sqe, IORING_OP_POLL_ADD},
IoUring, IoUringData, IoUringError, Task, TaskResultExt,
},
std::{
cell::{Cell, RefCell},
rc::Rc,
},
uapi::{c, OwnedFd},
};
impl IoUring {
pub async fn poll(&self, fd: &Rc<OwnedFd>, events: c::c_short) -> TaskResult<c::c_short> {
self.ring.check_destroyed()?;
let id = self.ring.id();
let pr = self.ring.pending_results.acquire();
{
let pw = self.ring.cached_polls.pop().unwrap_or_default();
pw.id.set(id.id);
*pw.data.borrow_mut() = Some(Data {
pr: pr.clone(),
fd: fd.clone(),
events: events as _,
});
self.ring.schedule(pw);
}
Ok(pr.await.map(|v| v as c::c_short))
}
pub async fn readable(&self, fd: &Rc<OwnedFd>) -> Result<c::c_short, IoUringError> {
self.poll(fd, c::POLLIN).await.merge()
}
pub async fn writable(&self, fd: &Rc<OwnedFd>) -> Result<c::c_short, IoUringError> {
self.poll(fd, c::POLLOUT).await.merge()
}
}
struct Data {
pr: PendingResult,
fd: Rc<OwnedFd>,
events: u16,
}
#[derive(Default)]
pub struct PollTask {
id: Cell<u64>,
data: RefCell<Option<Data>>,
}
unsafe impl Task for PollTask {
fn id(&self) -> u64 {
self.id.get()
}
fn complete(self: Box<Self>, ring: &IoUringData, res: i32) {
let data = self.data.borrow_mut().take();
if let Some(data) = data {
data.pr.complete(res);
}
ring.cached_polls.push(self);
}
fn encode(&self, sqe: &mut io_uring_sqe) {
let data = self.data.borrow_mut();
let data = data.as_ref().unwrap();
sqe.opcode = IORING_OP_POLL_ADD;
sqe.fd = data.fd.raw();
sqe.u3.poll_events = data.events;
}
}

View file

@ -1,8 +1,9 @@
use {
crate::io_uring::{
ops::TaskResult,
pending_result::PendingResult,
sys::{io_uring_sqe, IORING_OP_WRITE},
IoUring, IoUringData, IoUringError, Task,
IoUring, IoUringData, Task,
},
std::{
cell::{Cell, RefCell},
@ -19,7 +20,7 @@ impl IoUring {
buf: &Rc<Vec<u8>>,
offset: usize,
n: usize,
) -> Result<usize, IoUringError> {
) -> TaskResult<usize> {
self.ring.check_destroyed()?;
let id = self.ring.id();
let pr = self.ring.pending_results.acquire();
@ -39,7 +40,7 @@ impl IoUring {
});
self.ring.schedule(pw);
}
Ok(pr.await? as usize)
Ok(pr.await.map(|v| v as usize))
}
}

View file

@ -1,8 +1,5 @@
use {
crate::{
io_uring::IoUringError,
utils::{numcell::NumCell, oserror::OsError, ptr_ext::PtrExt, stack::Stack},
},
crate::utils::{numcell::NumCell, oserror::OsError, ptr_ext::PtrExt, stack::Stack},
std::{
cell::Cell,
future::Future,
@ -104,13 +101,13 @@ impl Clone for PendingResult {
}
impl Future for PendingResult {
type Output = Result<i32, IoUringError>;
type Output = Result<i32, OsError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let pr = unsafe { self.pr.deref() };
if let Some(res) = pr.res.take() {
let res = if res < 0 {
Err(IoUringError::OsError(OsError::from(-res as c::c_int)))
Err(OsError::from(-res as c::c_int))
} else {
Ok(res)
};

View file

@ -2,6 +2,7 @@
use {
crate::utils::oserror::OsError,
std::mem::MaybeUninit,
uapi::{c, OwnedFd},
};
@ -23,6 +24,12 @@ pub struct io_uring_sqe {
pub __pad2: [u64; 2],
}
impl Default for io_uring_sqe {
fn default() -> Self {
unsafe { MaybeUninit::zeroed().assume_init() }
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union io_uring_sqe_union1 {