io_uring: remove unnecessary interior mutability
This commit is contained in:
parent
fe80440f38
commit
20b1f29343
4 changed files with 41 additions and 45 deletions
|
|
@ -6,33 +6,28 @@ use {
|
||||||
},
|
},
|
||||||
utils::errorfmt::ErrorFmt,
|
utils::errorfmt::ErrorFmt,
|
||||||
},
|
},
|
||||||
std::cell::Cell,
|
|
||||||
uapi::c,
|
uapi::c,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct AsyncCancelTask {
|
pub struct AsyncCancelTask {
|
||||||
id: Cell<u64>,
|
id: u64,
|
||||||
target: Cell<u64>,
|
target: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IoUringData {
|
impl IoUringData {
|
||||||
pub fn cancel_task_in_kernel(&self, target: u64) {
|
pub fn cancel_task_in_kernel(&self, target: u64) {
|
||||||
let task = self.cached_cancels.pop().unwrap_or_else(|| {
|
|
||||||
Box::new(AsyncCancelTask {
|
|
||||||
id: Cell::new(0),
|
|
||||||
target: Cell::new(0),
|
|
||||||
})
|
|
||||||
});
|
|
||||||
let id = self.id_raw();
|
let id = self.id_raw();
|
||||||
task.id.set(id);
|
let mut task = self.cached_cancels.pop().unwrap_or_default();
|
||||||
task.target.set(target);
|
task.id = id;
|
||||||
|
task.target = target;
|
||||||
self.schedule(task);
|
self.schedule(task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Task for AsyncCancelTask {
|
unsafe impl Task for AsyncCancelTask {
|
||||||
fn id(&self) -> u64 {
|
fn id(&self) -> u64 {
|
||||||
self.id.get()
|
self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
fn complete(self: Box<Self>, ring: &IoUringData, res: i32) {
|
fn complete(self: Box<Self>, ring: &IoUringData, res: i32) {
|
||||||
|
|
@ -46,7 +41,7 @@ unsafe impl Task for AsyncCancelTask {
|
||||||
|
|
||||||
fn encode(&self, sqe: &mut io_uring_sqe) {
|
fn encode(&self, sqe: &mut io_uring_sqe) {
|
||||||
sqe.opcode = IORING_OP_ASYNC_CANCEL;
|
sqe.opcode = IORING_OP_ASYNC_CANCEL;
|
||||||
sqe.u2.addr = self.target.get();
|
sqe.u2.addr = self.target;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_cancel(&self) -> bool {
|
fn is_cancel(&self) -> bool {
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,7 @@ use {
|
||||||
sys::{io_uring_sqe, IORING_OP_POLL_ADD},
|
sys::{io_uring_sqe, IORING_OP_POLL_ADD},
|
||||||
IoUring, IoUringData, IoUringError, Task, TaskResultExt,
|
IoUring, IoUringData, IoUringError, Task, TaskResultExt,
|
||||||
},
|
},
|
||||||
std::{
|
std::rc::Rc,
|
||||||
cell::{Cell, RefCell},
|
|
||||||
rc::Rc,
|
|
||||||
},
|
|
||||||
uapi::{c, OwnedFd},
|
uapi::{c, OwnedFd},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -18,12 +15,13 @@ impl IoUring {
|
||||||
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_polls.pop().unwrap_or_default();
|
let mut pw = self.ring.cached_polls.pop().unwrap_or_default();
|
||||||
pw.id.set(id.id);
|
pw.id = id.id;
|
||||||
*pw.data.borrow_mut() = Some(Data {
|
pw.fd = fd.raw() as _;
|
||||||
|
pw.events = events as _;
|
||||||
|
pw.data = Some(Data {
|
||||||
pr: pr.clone(),
|
pr: pr.clone(),
|
||||||
fd: fd.clone(),
|
_fd: fd.clone(),
|
||||||
events: events as _,
|
|
||||||
});
|
});
|
||||||
self.ring.schedule(pw);
|
self.ring.schedule(pw);
|
||||||
}
|
}
|
||||||
|
|
@ -41,34 +39,32 @@ impl IoUring {
|
||||||
|
|
||||||
struct Data {
|
struct Data {
|
||||||
pr: PendingResult,
|
pr: PendingResult,
|
||||||
fd: Rc<OwnedFd>,
|
_fd: Rc<OwnedFd>,
|
||||||
events: u16,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PollTask {
|
pub struct PollTask {
|
||||||
id: Cell<u64>,
|
id: u64,
|
||||||
data: RefCell<Option<Data>>,
|
events: u16,
|
||||||
|
fd: i32,
|
||||||
|
data: Option<Data>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Task for PollTask {
|
unsafe impl Task for PollTask {
|
||||||
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) {
|
||||||
let data = self.data.borrow_mut().take();
|
if let Some(data) = self.data.take() {
|
||||||
if let Some(data) = data {
|
|
||||||
data.pr.complete(res);
|
data.pr.complete(res);
|
||||||
}
|
}
|
||||||
ring.cached_polls.push(self);
|
ring.cached_polls.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 = data.as_ref().unwrap();
|
|
||||||
sqe.opcode = IORING_OP_POLL_ADD;
|
sqe.opcode = IORING_OP_POLL_ADD;
|
||||||
sqe.fd = data.fd.raw();
|
sqe.fd = self.fd;
|
||||||
sqe.u3.poll_events = data.events;
|
sqe.u3.poll_events = self.events;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ unsafe impl Task for RecvmsgTask {
|
||||||
|
|
||||||
fn encode(&self, sqe: &mut io_uring_sqe) {
|
fn encode(&self, sqe: &mut io_uring_sqe) {
|
||||||
sqe.opcode = IORING_OP_RECVMSG;
|
sqe.opcode = IORING_OP_RECVMSG;
|
||||||
sqe.fd = self.fd;
|
sqe.fd = self.fd as _;
|
||||||
sqe.u2.addr = &self.msghdr as *const _ as _;
|
sqe.u2.addr = &self.msghdr as *const _ as _;
|
||||||
sqe.u3.msg_flags = c::MSG_CMSG_CLOEXEC as _;
|
sqe.u3.msg_flags = c::MSG_CMSG_CLOEXEC as _;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use {
|
||||||
utils::buf::Buf,
|
utils::buf::Buf,
|
||||||
},
|
},
|
||||||
std::rc::Rc,
|
std::rc::Rc,
|
||||||
uapi::OwnedFd,
|
uapi::{c, OwnedFd},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl IoUring {
|
impl IoUring {
|
||||||
|
|
@ -27,9 +27,12 @@ impl IoUring {
|
||||||
let mut pw = self.ring.cached_writes.pop().unwrap_or_default();
|
let mut pw = self.ring.cached_writes.pop().unwrap_or_default();
|
||||||
pw.id = id.id;
|
pw.id = id.id;
|
||||||
pw.has_timeout = timeout.is_some();
|
pw.has_timeout = timeout.is_some();
|
||||||
|
pw.fd = fd.raw();
|
||||||
|
pw.buf = buf.as_ptr() as _;
|
||||||
|
pw.len = buf.len();
|
||||||
pw.data = Some(WriteTaskData {
|
pw.data = Some(WriteTaskData {
|
||||||
fd: fd.clone(),
|
_fd: fd.clone(),
|
||||||
buf,
|
_buf: buf,
|
||||||
res: pr.clone(),
|
res: pr.clone(),
|
||||||
});
|
});
|
||||||
self.ring.schedule(pw);
|
self.ring.schedule(pw);
|
||||||
|
|
@ -42,8 +45,8 @@ impl IoUring {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct WriteTaskData {
|
struct WriteTaskData {
|
||||||
fd: Rc<OwnedFd>,
|
_fd: Rc<OwnedFd>,
|
||||||
buf: Buf,
|
_buf: Buf,
|
||||||
res: PendingResult,
|
res: PendingResult,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,6 +54,9 @@ struct WriteTaskData {
|
||||||
pub struct WriteTask {
|
pub struct WriteTask {
|
||||||
id: u64,
|
id: u64,
|
||||||
has_timeout: bool,
|
has_timeout: bool,
|
||||||
|
fd: c::c_int,
|
||||||
|
buf: usize,
|
||||||
|
len: usize,
|
||||||
data: Option<WriteTaskData>,
|
data: Option<WriteTaskData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,17 +69,16 @@ unsafe impl Task for WriteTask {
|
||||||
if let Some(data) = self.data.take() {
|
if let Some(data) = self.data.take() {
|
||||||
data.res.complete(res);
|
data.res.complete(res);
|
||||||
}
|
}
|
||||||
ring.clone().cached_writes.push(self);
|
ring.cached_writes.push(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode(&self, sqe: &mut io_uring_sqe) {
|
fn encode(&self, sqe: &mut io_uring_sqe) {
|
||||||
let data = self.data.as_ref().unwrap();
|
|
||||||
sqe.opcode = IORING_OP_WRITE;
|
sqe.opcode = IORING_OP_WRITE;
|
||||||
sqe.fd = data.fd.raw();
|
sqe.fd = self.fd as _;
|
||||||
sqe.u1.off = !0;
|
sqe.u1.off = !0;
|
||||||
sqe.u2.addr = data.buf.as_ptr() as _;
|
sqe.u2.addr = self.buf as _;
|
||||||
sqe.u3.rw_flags = 0;
|
sqe.u3.rw_flags = 0;
|
||||||
sqe.len = data.buf.len() as _;
|
sqe.len = self.len as _;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_timeout(&self) -> bool {
|
fn has_timeout(&self) -> bool {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue