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 drop logic #157

Merged
merged 2 commits into from
Oct 30, 2022
Merged
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: 15 additions & 16 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::driver::Driver;

use std::future::Future;
use std::io;
use std::mem::ManuallyDrop;
use std::os::unix::io::{AsRawFd, RawFd};
use tokio::io::unix::AsyncFd;
use tokio::task::LocalSet;
Expand All @@ -20,19 +21,10 @@ pub struct Runtime {
uring_fd: RawFd,

/// LocalSet for !Send tasks
local: LocalSet,
local: ManuallyDrop<LocalSet>,

/// Tokio runtime, always current-thread
rt: tokio::runtime::Runtime,

/// This is here for drop order reasons.
///
/// We can't unset the driver in the runtime drop method, because the inner runtime needs to
/// be dropped first so that there are no tasks running.
///
/// The rust drop order rules guarantee that the inner runtime will be dropped first, and
/// this last.
_driver_guard: RuntimeContextGuard,
rt: ManuallyDrop<tokio::runtime::Runtime>,
}

/// Spawns a new asynchronous task, returning a [`JoinHandle`] for it.
Expand Down Expand Up @@ -77,7 +69,9 @@ impl Runtime {
.enable_all()
.build()?;

let local = LocalSet::new();
let rt = ManuallyDrop::new(rt);

let local = ManuallyDrop::new(LocalSet::new());

let driver = Driver::new(b)?;

Expand All @@ -89,7 +83,6 @@ impl Runtime {
uring_fd: driver_fd,
local,
rt,
_driver_guard: RuntimeContextGuard,
})
}

Expand Down Expand Up @@ -124,10 +117,16 @@ impl Runtime {
}
}

struct RuntimeContextGuard;

impl Drop for RuntimeContextGuard {
impl Drop for Runtime {
fn drop(&mut self) {
// drop tasks
unsafe {
ManuallyDrop::drop(&mut self.local);
ManuallyDrop::drop(&mut self.rt);
}

// once tasks are dropped, we can unset the driver
// this will block until all completions are received
Noah-Kennedy marked this conversation as resolved.
Show resolved Hide resolved
CONTEXT.with(|rc| rc.unset_driver())
}
}