diff --git a/src/sqe.rs b/src/sqe.rs index 7b6f70a..108d32f 100644 --- a/src/sqe.rs +++ b/src/sqe.rs @@ -4,7 +4,6 @@ use std::ffi::CStr; use std::ops::{Deref, DerefMut}; use std::os::unix::io::RawFd; use std::ptr; -use std::slice; use crate::registrar::{UringFd, UringReadBuf, UringWriteBuf}; @@ -22,7 +21,7 @@ use crate::Personality; /// Can be configured with a set of [`SubmissionFlags`](crate::sqe::SubmissionFlags). /// pub struct SQE<'a> { - sqe: &'a mut uring_sys::io_uring_sqe, + pub sqe: &'a mut uring_sys::io_uring_sqe, } impl<'a> SQE<'a> { @@ -624,13 +623,19 @@ bitflags::bitflags! { /// A sequence of [`SQE`]s from the [`SubmissionQueue`][crate::SubmissionQueue]. pub struct SQEs<'ring> { - sqes: slice::IterMut<'ring, uring_sys::io_uring_sqe>, + sq: &'ring mut uring_sys::io_uring_sq, + first: u32, + count: u32, + consumed: u32, } impl<'ring> SQEs<'ring> { - pub(crate) fn new(slice: &'ring mut [uring_sys::io_uring_sqe]) -> SQEs<'ring> { + pub(crate) fn new(sq: &'ring mut uring_sys::io_uring_sq, first: u32, count: u32) -> SQEs<'ring> { SQEs { - sqes: slice.iter_mut(), + sq, + first, + count, + consumed: 0, } } @@ -660,14 +665,20 @@ impl<'ring> SQEs<'ring> { /// Remaining [`SQE`]s that can be modified. pub fn remaining(&self) -> u32 { - self.sqes.len() as u32 + (self.count - self.consumed) as u32 } fn consume(&mut self) -> Option> { - self.sqes.next().map(|sqe| { - unsafe { uring_sys::io_uring_prep_nop(sqe) } - SQE { sqe } - }) + if self.consumed < self.count { + unsafe { + let sqe = self.sq.sqes.offset(((self.first + self.consumed) & *self.sq.kring_mask) as isize); + uring_sys::io_uring_prep_nop(sqe); + self.consumed += 1; + Some(SQE { sqe: &mut *sqe }) + } + } else { + None + } } } diff --git a/src/submission_queue.rs b/src/submission_queue.rs index 7a72a72..bf4b15c 100644 --- a/src/submission_queue.rs +++ b/src/submission_queue.rs @@ -2,7 +2,6 @@ use std::fmt; use std::io; use std::ptr::NonNull; use std::marker::PhantomData; -use std::slice; use std::time::Duration; use std::sync::atomic::{self, Ordering}; @@ -152,7 +151,7 @@ pub(crate) unsafe fn prepare_sqe<'a>(ring: &mut uring_sys::io_uring) -> Option(sq: &mut uring_sys::io_uring_sq, count: u32) +pub(crate) unsafe fn prepare_sqes<'a>(sq: &'a mut uring_sys::io_uring_sq, count: u32) -> Option> { atomic::fence(Ordering::Acquire); @@ -161,9 +160,9 @@ pub(crate) unsafe fn prepare_sqes<'a>(sq: &mut uring_sys::io_uring_sq, count: u3 let next: u32 = sq.sqe_tail + count; if next - head <= *sq.kring_entries { - let sqe = sq.sqes.offset((sq.sqe_tail & *sq.kring_mask) as isize); + let first = sq.sqe_tail; sq.sqe_tail = next; - Some(SQEs::new(slice::from_raw_parts_mut(sqe, count as usize))) + Some(SQEs::new(sq, first, count)) } else { None }