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

Pop the op later. #85

Merged
merged 2 commits into from
Oct 7, 2023
Merged
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
3 changes: 1 addition & 2 deletions compio-driver/src/iocp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ impl<T> Overlapped<T> {
}
}

#[doc(hidden)]
pub struct RawOp(NonNull<Overlapped<dyn OpCode>>);
pub(crate) struct RawOp(NonNull<Overlapped<dyn OpCode>>);

impl RawOp {
pub(crate) fn new(user_data: usize, op: impl OpCode + 'static) -> Self {
Expand Down
3 changes: 1 addition & 2 deletions compio-driver/src/iour/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use slab::Slab;
use crate::Entry;

pub(crate) mod op;
#[doc(hidden)]
pub use crate::unix::RawOp;
pub(crate) use crate::unix::RawOp;

/// Abstraction of io-uring operations.
pub trait OpCode {
Expand Down
7 changes: 1 addition & 6 deletions compio-driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,13 @@ impl Operation {
Self { op, user_data }
}

#[doc(hidden)]
pub fn into_inner(self) -> RawOp {
self.op
}

/// Restore the original operation.
///
/// # Safety
///
/// The caller should guarantee that the type is right.
pub unsafe fn into_op<T: OpCode>(self) -> T {
self.into_inner().into_inner()
self.op.into_inner()
}

/// The same user_data when the operation is pushed into the driver.
Expand Down
3 changes: 1 addition & 2 deletions compio-driver/src/poll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use slab::Slab;
use crate::{syscall, Entry};

pub(crate) mod op;
#[doc(hidden)]
pub use crate::unix::RawOp;
pub(crate) use crate::unix::RawOp;

/// Abstraction of operations.
pub trait OpCode {
Expand Down
3 changes: 1 addition & 2 deletions compio-driver/src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use std::{mem::ManuallyDrop, pin::Pin, ptr::NonNull};

use crate::OpCode;

#[doc(hidden)]
pub struct RawOp(NonNull<dyn OpCode>);
pub(crate) struct RawOp(NonNull<dyn OpCode>);

impl RawOp {
pub(crate) fn new(_user_data: usize, op: impl OpCode + 'static) -> Self {
Expand Down
22 changes: 11 additions & 11 deletions compio-runtime/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ impl Runtime {
let mut op_runtime = self.op_runtime.borrow_mut();
if op_runtime.has_result(*user_data) {
let op = op_runtime.remove(*user_data);
Poll::Ready(BufResult(op.result.unwrap(), unsafe {
op.op
.expect("`poll_task` called on dummy Op")
.into_inner::<T>()
}))
let res = self
.driver
.borrow_mut()
.pop(&mut op.entry.into_iter())
.next()
.expect("the result should have come");
Poll::Ready(res.map_buffer(|op| unsafe { op.into_op::<T>() }))
} else {
op_runtime.update_waker(*user_data, cx.waker().clone());
Poll::Pending
Expand Down Expand Up @@ -150,12 +152,10 @@ impl Runtime {
let mut driver = self.driver.borrow_mut();
match driver.poll(timeout, &mut entries) {
Ok(_) => {
for BufResult(res, op) in driver.pop(&mut entries.into_iter()) {
self.op_runtime.borrow_mut().update_result(
op.user_data(),
op.into_inner(),
res,
);
for entry in entries {
self.op_runtime
.borrow_mut()
.update_result(entry.user_data(), entry);
}
}
Err(e) => match e.kind() {
Expand Down
13 changes: 5 additions & 8 deletions compio-runtime/src/runtime/op.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use std::{
collections::HashMap,
future::Future,
io,
pin::Pin,
task::{Context, Poll, Waker},
};

use compio_buf::BufResult;
use compio_driver::{OpCode, RawOp};
use compio_driver::{Entry, OpCode};

use crate::key::Key;

#[derive(Default)]
pub(crate) struct RegisteredOp {
pub op: Option<RawOp>,
pub waker: Option<Waker>,
pub result: Option<io::Result<usize>>,
pub entry: Option<Entry>,
pub cancelled: bool,
}

Expand All @@ -29,13 +27,12 @@ impl OpRuntime {
self.ops.entry(key).or_default().waker = Some(waker);
}

pub fn update_result(&mut self, key: usize, raw_op: RawOp, result: io::Result<usize>) {
pub fn update_result(&mut self, key: usize, entry: Entry) {
let op = self.ops.entry(key).or_default();
if let Some(waker) = op.waker.take() {
waker.wake();
}
op.op = Some(raw_op);
op.result = Some(result);
op.entry = Some(entry);
if op.cancelled {
self.remove(key);
}
Expand All @@ -44,7 +41,7 @@ impl OpRuntime {
pub fn has_result(&mut self, key: usize) -> bool {
self.ops
.get_mut(&key)
.map(|op| op.result.is_some())
.map(|op| op.entry.is_some())
.unwrap_or_default()
}

Expand Down