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

rt: refactor runtime to avoid Rc<RefCell<...>> #142

Merged
merged 6 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
45 changes: 8 additions & 37 deletions src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,11 @@ mod write;
mod writev;

use io_uring::IoUring;
use scoped_tls::scoped_thread_local;
use slab::Slab;
use std::cell::RefCell;
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
use std::rc::Rc;

pub(crate) struct Driver {
inner: Handle,
}

type Handle = Rc<RefCell<Inner>>;

pub(crate) struct Inner {
/// In-flight operations
ops: Ops,

Expand All @@ -67,45 +58,25 @@ struct Ops {
lifecycle: Slab<op::Lifecycle>,
}

scoped_thread_local!(pub(crate) static CURRENT: Rc<RefCell<Inner>>);

impl Driver {
pub(crate) fn new(b: &crate::Builder) -> io::Result<Driver> {
let uring = b.urb.build(b.entries)?;

let inner = Rc::new(RefCell::new(Inner {
Ok(Driver {
ops: Ops::new(),
uring,
}));

Ok(Driver { inner })
})
}

/// Enter the driver context. This enables using uring types.
pub(crate) fn with<R>(&self, f: impl FnOnce() -> R) -> R {
CURRENT.set(&self.inner, f)
fn wait(&mut self) -> io::Result<usize> {
Noah-Kennedy marked this conversation as resolved.
Show resolved Hide resolved
self.uring.submit_and_wait(1)
}

pub(crate) fn tick(&self) {
let mut inner = self.inner.borrow_mut();
inner.tick();
fn num_operations(&mut self) -> usize {
Noah-Kennedy marked this conversation as resolved.
Show resolved Hide resolved
self.ops.lifecycle.len()
}

fn wait(&self) -> io::Result<usize> {
let mut inner = self.inner.borrow_mut();
let inner = &mut *inner;

inner.uring.submit_and_wait(1)
}

fn num_operations(&self) -> usize {
let inner = self.inner.borrow();
inner.ops.lifecycle.len()
}
}

impl Inner {
fn tick(&mut self) {
pub(crate) fn tick(&mut self) {
let mut cq = self.uring.completion();
cq.sync();

Expand Down Expand Up @@ -143,7 +114,7 @@ impl Inner {

impl AsRawFd for Driver {
fn as_raw_fd(&self) -> RawFd {
self.inner.borrow().uring.as_raw_fd()
self.uring.as_raw_fd()
}
}

Expand Down
Loading