Skip to content

Commit 8725b81

Browse files
authored
rt: refactor drop logic (#157)
rt: refactor drop logic This shouldn't change any functionality, but it does use ManuallyDrop to cleanup the drop logic a fair bit. This is much nicer than relying on drop order.
1 parent 507186d commit 8725b81

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/runtime/mod.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::driver::Driver;
22

33
use std::future::Future;
44
use std::io;
5+
use std::mem::ManuallyDrop;
56
use std::os::unix::io::{AsRawFd, RawFd};
67
use tokio::io::unix::AsyncFd;
78
use tokio::task::LocalSet;
@@ -20,19 +21,10 @@ pub struct Runtime {
2021
uring_fd: RawFd,
2122

2223
/// LocalSet for !Send tasks
23-
local: LocalSet,
24+
local: ManuallyDrop<LocalSet>,
2425

2526
/// Tokio runtime, always current-thread
26-
rt: tokio::runtime::Runtime,
27-
28-
/// This is here for drop order reasons.
29-
///
30-
/// We can't unset the driver in the runtime drop method, because the inner runtime needs to
31-
/// be dropped first so that there are no tasks running.
32-
///
33-
/// The rust drop order rules guarantee that the inner runtime will be dropped first, and
34-
/// this last.
35-
_driver_guard: RuntimeContextGuard,
27+
rt: ManuallyDrop<tokio::runtime::Runtime>,
3628
}
3729

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

80-
let local = LocalSet::new();
72+
let rt = ManuallyDrop::new(rt);
73+
74+
let local = ManuallyDrop::new(LocalSet::new());
8175

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

@@ -89,7 +83,6 @@ impl Runtime {
8983
uring_fd: driver_fd,
9084
local,
9185
rt,
92-
_driver_guard: RuntimeContextGuard,
9386
})
9487
}
9588

@@ -124,10 +117,16 @@ impl Runtime {
124117
}
125118
}
126119

127-
struct RuntimeContextGuard;
128-
129-
impl Drop for RuntimeContextGuard {
120+
impl Drop for Runtime {
130121
fn drop(&mut self) {
122+
// drop tasks
123+
unsafe {
124+
ManuallyDrop::drop(&mut self.local);
125+
ManuallyDrop::drop(&mut self.rt);
126+
}
127+
128+
// once tasks are dropped, we can unset the driver
129+
// this will block until all completions are received
131130
CONTEXT.with(|rc| rc.unset_driver())
132131
}
133132
}

0 commit comments

Comments
 (0)