Skip to content

Commit

Permalink
[WIP] impl uring cmd feature
Browse files Browse the repository at this point in the history
  • Loading branch information
SidongYang committed Dec 22, 2024
1 parent cf5417f commit 22f1fbf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::buf::fixed::FixedBuf;
use crate::buf::{BoundedBuf, BoundedBufMut, IoBuf, IoBufMut, Slice};
use crate::fs::OpenOptions;
use crate::io::cmd::UnsubmittedCmd;
use crate::io::SharedFd;

use crate::runtime::driver::op::{Op, Submit};
Expand All @@ -11,6 +12,7 @@ use std::fmt;
use std::io;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::path::Path;
use std::pin::Pin;

/// A reference to an open file on the filesystem.
///
Expand Down Expand Up @@ -879,6 +881,11 @@ impl File {
pub async fn close(mut self) -> io::Result<()> {
self.fd.close().await
}

#[allow(missing_docs)]
pub fn cmd<T>(&self, op: u32, cmd: [u8; 16], data: Pin<Box<T>>) -> UnsubmittedCmd<T> {
UnsubmittedCmd::cmd(&self.fd, op, cmd, data)
}
}

impl FromRawFd for File {
Expand Down
45 changes: 45 additions & 0 deletions src/io/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::marker::PhantomData;
use std::pin::Pin;

use io_uring::cqueue::Entry;
use io_uring::{opcode, types};

use crate::io::SharedFd;

use crate::{OneshotOutputTransform, UnsubmittedOneshot};

/// An unsubmitted read operation.
pub type UnsubmittedCmd<T> = UnsubmittedOneshot<CmdData<T>, CmdTransform<T>>;

impl<T> UnsubmittedCmd<T> {
pub(crate) fn cmd(fd: &SharedFd, op: u32, cmd: [u8; 16], data: Pin<Box<T>>) -> Self {
Self::new(
CmdData { data },
CmdTransform {
_phantom: PhantomData,
},
opcode::UringCmd16::new(types::Fd(fd.raw_fd()), op)
.cmd(cmd)
.build(),
)
}
}

#[allow(missing_docs)]
pub struct CmdData<T> {
data: Pin<Box<T>>,
}

#[allow(missing_docs)]
pub struct CmdTransform<B> {
_phantom: PhantomData<B>,
}

impl<T> OneshotOutputTransform for CmdTransform<T> {
type Output = (i32, Pin<Box<T>>);
type StoredData = CmdData<T>;

fn transform_oneshot_output(self, data: Self::StoredData, cqe: Entry) -> Self::Output {
(cqe.result(), data.data)
}
}
2 changes: 2 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ pub(crate) mod writev;

mod writev_all;
pub(crate) use writev_all::writev_at_all;

pub(crate) mod cmd;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub mod buf;
pub mod fs;
pub mod net;

pub use io::cmd::*;
pub use io::read::*;
pub use io::readv::*;
pub use io::write::*;
Expand Down

0 comments on commit 22f1fbf

Please sign in to comment.