-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UnsubmittedRead to follow new API for read part.
Currently, there's no unsubmitted API change in read part yet. Add the new API following write part. Add simple link operation test cases to use read/write submittable operations.
- Loading branch information
Showing
9 changed files
with
112 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ pub(crate) use noop::NoOp; | |
|
||
mod open; | ||
|
||
mod read; | ||
pub(crate) mod read; | ||
|
||
mod read_fixed; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,69 @@ | ||
use io_uring::cqueue::Entry; | ||
|
||
use crate::buf::BoundedBufMut; | ||
use crate::io::SharedFd; | ||
use crate::BufResult; | ||
use crate::{BufResult, OneshotOutputTransform, UnsubmittedOneshot}; | ||
|
||
use crate::runtime::driver::op::{Completable, CqeResult, Op}; | ||
use crate::runtime::CONTEXT; | ||
use std::io; | ||
use std::marker::PhantomData; | ||
|
||
/// An unsubmitted read operation. | ||
pub type UnsubmittedRead<T> = UnsubmittedOneshot<ReadData<T>, ReadTransform<T>>; | ||
|
||
pub(crate) struct Read<T> { | ||
#[allow(missing_docs)] | ||
pub struct ReadData<T> { | ||
/// Holds a strong ref to the FD, preventing the file from being closed | ||
/// while the operation is in-flight. | ||
#[allow(dead_code)] | ||
fd: SharedFd, | ||
_fd: SharedFd, | ||
|
||
/// Reference to the in-flight buffer. | ||
pub(crate) buf: T, | ||
buf: T, | ||
} | ||
|
||
impl<T: BoundedBufMut> Op<Read<T>> { | ||
pub(crate) fn read_at(fd: &SharedFd, buf: T, offset: u64) -> io::Result<Op<Read<T>>> { | ||
use io_uring::{opcode, types}; | ||
|
||
CONTEXT.with(|x| { | ||
x.handle().expect("Not in a runtime context").submit_op( | ||
Read { | ||
fd: fd.clone(), | ||
buf, | ||
}, | ||
|read| { | ||
// Get raw buffer info | ||
let ptr = read.buf.stable_mut_ptr(); | ||
let len = read.buf.bytes_total(); | ||
opcode::Read::new(types::Fd(fd.raw_fd()), ptr, len as _) | ||
.offset(offset as _) | ||
.build() | ||
}, | ||
) | ||
}) | ||
} | ||
#[allow(missing_docs)] | ||
pub struct ReadTransform<T> { | ||
_phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T> Completable for Read<T> | ||
impl<T> OneshotOutputTransform for ReadTransform<T> | ||
where | ||
T: BoundedBufMut, | ||
{ | ||
type Output = BufResult<usize, T>; | ||
type StoredData = ReadData<T>; | ||
|
||
fn complete(self, cqe: CqeResult) -> Self::Output { | ||
// Convert the operation result to `usize` | ||
let res = cqe.result.map(|v| v as usize); | ||
// Recover the buffer | ||
let mut buf = self.buf; | ||
|
||
// If the operation was successful, advance the initialized cursor. | ||
if let Ok(n) = res { | ||
fn transform_oneshot_output(self, mut data: Self::StoredData, cqe: Entry) -> Self::Output { | ||
let n = cqe.result(); | ||
let res = if n >= 0 { | ||
// Safety: the kernel wrote `n` bytes to the buffer. | ||
unsafe { | ||
buf.set_init(n); | ||
} | ||
} | ||
unsafe { data.buf.set_init(n as usize) }; | ||
Ok(n as usize) | ||
} else { | ||
Err(io::Error::from_raw_os_error(-n)) | ||
}; | ||
|
||
(res, data.buf) | ||
} | ||
} | ||
|
||
impl<T: BoundedBufMut> UnsubmittedRead<T> { | ||
pub(crate) fn read_at(fd: &SharedFd, mut buf: T, offset: u64) -> Self { | ||
use io_uring::{opcode, types}; | ||
|
||
// Get raw buffer info | ||
let ptr = buf.stable_mut_ptr(); | ||
let len = buf.bytes_total(); | ||
|
||
(res, buf) | ||
Self::new( | ||
ReadData { | ||
_fd: fd.clone(), | ||
buf, | ||
}, | ||
ReadTransform { | ||
_phantom: PhantomData, | ||
}, | ||
opcode::Read::new(types::Fd(fd.raw_fd()), ptr, len as _) | ||
.offset(offset as _) | ||
.build(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ fn complete_ops_on_drop() { | |
}, | ||
25 * 1024 * 1024, | ||
) | ||
.submit() | ||
.await | ||
.0 | ||
.unwrap(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters