Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix prepare_sqes #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/sqe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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> {
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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<SQE<'ring>> {
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
}
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/submission_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -152,7 +151,7 @@ pub(crate) unsafe fn prepare_sqe<'a>(ring: &mut uring_sys::io_uring) -> Option<S
}
}

pub(crate) unsafe fn prepare_sqes<'a>(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<SQEs<'a>>
{
atomic::fence(Ordering::Acquire);
Expand All @@ -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
}
Expand Down