io-uring: add timeout argument to write
This commit is contained in:
parent
837b5592bc
commit
ad85d89641
2 changed files with 42 additions and 39 deletions
|
|
@ -3,37 +3,39 @@ use {
|
||||||
io_uring::{
|
io_uring::{
|
||||||
ops::TaskResult,
|
ops::TaskResult,
|
||||||
pending_result::PendingResult,
|
pending_result::PendingResult,
|
||||||
sys::{io_uring_sqe, IORING_OP_WRITE},
|
sys::{io_uring_sqe, IORING_OP_WRITE, IOSQE_IO_LINK},
|
||||||
IoUring, IoUringData, Task,
|
IoUring, IoUringData, Task,
|
||||||
},
|
},
|
||||||
|
time::Time,
|
||||||
utils::buf::Buf,
|
utils::buf::Buf,
|
||||||
},
|
},
|
||||||
std::{
|
std::rc::Rc,
|
||||||
cell::{Cell, RefCell},
|
|
||||||
rc::Rc,
|
|
||||||
},
|
|
||||||
uapi::OwnedFd,
|
uapi::OwnedFd,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl IoUring {
|
impl IoUring {
|
||||||
pub async fn write(&self, fd: &Rc<OwnedFd>, buf: Buf) -> TaskResult<usize> {
|
pub async fn write(
|
||||||
|
&self,
|
||||||
|
fd: &Rc<OwnedFd>,
|
||||||
|
buf: Buf,
|
||||||
|
timeout: Option<Time>,
|
||||||
|
) -> TaskResult<usize> {
|
||||||
self.ring.check_destroyed()?;
|
self.ring.check_destroyed()?;
|
||||||
let id = self.ring.id();
|
let id = self.ring.id();
|
||||||
let pr = self.ring.pending_results.acquire();
|
let pr = self.ring.pending_results.acquire();
|
||||||
{
|
{
|
||||||
let pw = self.ring.cached_writes.pop().unwrap_or_else(|| {
|
let mut pw = self.ring.cached_writes.pop().unwrap_or_default();
|
||||||
Box::new(WriteTask {
|
pw.id = id.id;
|
||||||
id: Cell::new(0),
|
pw.has_timeout = timeout.is_some();
|
||||||
data: Default::default(),
|
pw.data = Some(WriteTaskData {
|
||||||
})
|
|
||||||
});
|
|
||||||
pw.id.set(id.id);
|
|
||||||
*pw.data.borrow_mut() = Some(WriteTaskData {
|
|
||||||
fd: fd.clone(),
|
fd: fd.clone(),
|
||||||
buf,
|
buf,
|
||||||
res: pr.clone(),
|
res: pr.clone(),
|
||||||
});
|
});
|
||||||
self.ring.schedule(pw);
|
self.ring.schedule(pw);
|
||||||
|
if let Some(time) = timeout {
|
||||||
|
self.schedule_timeout(time);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(pr.await.map(|v| v as usize))
|
Ok(pr.await.map(|v| v as usize))
|
||||||
}
|
}
|
||||||
|
|
@ -45,31 +47,35 @@ struct WriteTaskData {
|
||||||
res: PendingResult,
|
res: PendingResult,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct WriteTask {
|
pub struct WriteTask {
|
||||||
id: Cell<u64>,
|
id: u64,
|
||||||
data: RefCell<Option<WriteTaskData>>,
|
has_timeout: bool,
|
||||||
|
data: Option<WriteTaskData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Task for WriteTask {
|
unsafe impl Task for WriteTask {
|
||||||
fn id(&self) -> u64 {
|
fn id(&self) -> u64 {
|
||||||
self.id.get()
|
self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
fn complete(self: Box<Self>, ring: &IoUringData, res: i32) {
|
fn complete(mut self: Box<Self>, ring: &IoUringData, res: i32) {
|
||||||
if let Some(data) = self.data.borrow_mut().take() {
|
if let Some(data) = self.data.take() {
|
||||||
data.res.complete(res);
|
data.res.complete(res);
|
||||||
}
|
}
|
||||||
ring.clone().cached_writes.push(self);
|
ring.clone().cached_writes.push(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode(&self, sqe: &mut io_uring_sqe) {
|
fn encode(&self, sqe: &mut io_uring_sqe) {
|
||||||
let data = self.data.borrow_mut();
|
let data = self.data.as_ref().unwrap();
|
||||||
let data = data.as_ref().unwrap();
|
|
||||||
sqe.opcode = IORING_OP_WRITE;
|
sqe.opcode = IORING_OP_WRITE;
|
||||||
sqe.fd = data.fd.raw();
|
sqe.fd = data.fd.raw();
|
||||||
sqe.u1.off = !0;
|
sqe.u1.off = !0;
|
||||||
sqe.u2.addr = data.buf.as_ptr() as _;
|
sqe.u2.addr = data.buf.as_ptr() as _;
|
||||||
sqe.u3.rw_flags = 0;
|
sqe.u3.rw_flags = 0;
|
||||||
sqe.len = data.buf.len() as _;
|
sqe.len = data.buf.len() as _;
|
||||||
|
if self.has_timeout {
|
||||||
|
sqe.flags = IOSQE_IO_LINK;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,10 @@ use {
|
||||||
WlSurface,
|
WlSurface,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
io_uring::{IoUring, TaskResultExt},
|
io_uring::{IoUring, IoUringError, TaskResultExt},
|
||||||
rect::Rect,
|
rect::Rect,
|
||||||
state::State,
|
state::State,
|
||||||
|
time::Time,
|
||||||
tree::ToplevelNode,
|
tree::ToplevelNode,
|
||||||
utils::{
|
utils::{
|
||||||
bitflags::BitflagsExt, buf::Buf, clonecell::CloneCell, copyhashmap::CopyHashMap,
|
bitflags::BitflagsExt, buf::Buf, clonecell::CloneCell, copyhashmap::CopyHashMap,
|
||||||
|
|
@ -59,10 +60,7 @@ use {
|
||||||
},
|
},
|
||||||
ahash::{AHashMap, AHashSet},
|
ahash::{AHashMap, AHashSet},
|
||||||
bstr::ByteSlice,
|
bstr::ByteSlice,
|
||||||
futures_util::{
|
futures_util::{select, FutureExt},
|
||||||
future::{self, Either},
|
|
||||||
pin_mut, select, FutureExt,
|
|
||||||
},
|
|
||||||
smallvec::SmallVec,
|
smallvec::SmallVec,
|
||||||
std::{
|
std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
|
|
@ -2380,24 +2378,23 @@ struct XToWaylandTransfer {
|
||||||
|
|
||||||
impl XToWaylandTransfer {
|
impl XToWaylandTransfer {
|
||||||
async fn run(mut self) {
|
async fn run(mut self) {
|
||||||
let timeout = self.state.wheel.timeout(5000);
|
let timeout = Time::in_ms(5000).unwrap();
|
||||||
pin_mut!(timeout);
|
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
while pos < self.data.len() {
|
while pos < self.data.len() {
|
||||||
let f1 = self.state.ring.write(&self.fd, self.data.slice(pos..));
|
let res = self
|
||||||
pin_mut!(f1);
|
.state
|
||||||
match future::select(f1, &mut timeout).await {
|
.ring
|
||||||
Either::Left((res, _)) => match res.merge() {
|
.write(&self.fd, self.data.slice(pos..), Some(timeout));
|
||||||
Ok(n) => pos += n,
|
match res.await.merge() {
|
||||||
Err(e) => {
|
Ok(n) => pos += n,
|
||||||
log::error!("Could not write to wayland client: {}", ErrorFmt(e));
|
Err(IoUringError::OsError(OsError(c::ECANCELED))) => {
|
||||||
break;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Either::Right(_) => {
|
|
||||||
log::error!("Transfer timed out");
|
log::error!("Transfer timed out");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Could not write to wayland client: {}", ErrorFmt(e));
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.shared.transfers.remove(&self.id);
|
self.shared.transfers.remove(&self.id);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue