Skip to content

Commit 40b8ba1

Browse files
wllenyjjiangliu
authored andcommitted
async_runtime: remove thread_local of Runtime
If put Runtime into thread_local, the order of Drop calls is platform-dependent. This will case panic in the `tokio-uring` Runtime Drop function. The pr tokio-rs/tokio-uring#173 will fix. We will revert this, if it is meraged. Signed-off-by: wanglei01 <[email protected]>
1 parent 5870f09 commit 40b8ba1

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/common/async_runtime.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ impl Runtime {
4949
Runtime::Uring(rt) => rt.lock().unwrap().block_on(f),
5050
}
5151
}
52+
53+
/// Spawns a new asynchronous task, returning a [`JoinHandle`] for it.
54+
///
55+
/// Spawning a task enables the task to execute concurrently to other tasks.
56+
/// There is no guarantee that a spawned task will execute to completion. When a
57+
/// runtime is shutdown, all outstanding tasks are dropped, regardless of the
58+
/// lifecycle of that task.
59+
///
60+
/// This function must be called from the context of a `tokio-uring` runtime.
61+
///
62+
/// [`JoinHandle`]: tokio::task::JoinHandle
63+
pub fn spawn<T: std::future::Future + 'static>(
64+
&self,
65+
task: T,
66+
) -> tokio::task::JoinHandle<T::Output> {
67+
match self {
68+
Runtime::Tokio(_) => tokio::task::spawn_local(task),
69+
#[cfg(target_os = "linux")]
70+
Runtime::Uring(_) => tokio_uring::spawn(task),
71+
}
72+
}
5273
}
5374

5475
/// Start an async runtime.
@@ -62,39 +83,33 @@ impl Default for Runtime {
6283
}
6384
}
6485

65-
std::thread_local! {
66-
pub(crate) static CURRENT_RUNTIME: Runtime = Runtime::new();
67-
}
68-
6986
/// Run a callback with the default `Runtime` object.
7087
pub fn with_runtime<F, R>(f: F) -> R
7188
where
7289
F: FnOnce(&Runtime) -> R,
7390
{
74-
CURRENT_RUNTIME.with(f)
91+
let rt = Runtime::new();
92+
f(&rt)
7593
}
7694

7795
/// Run a future to completion with the default `Runtime` object.
7896
pub fn block_on<F: Future>(f: F) -> F::Output {
79-
CURRENT_RUNTIME.with(|rt| rt.block_on(f))
97+
Runtime::new().block_on(f)
8098
}
8199

82-
/// Spawns a new asynchronous task, returning a [`JoinHandle`] for it.
100+
/// Spawns a new asynchronous task with the defualt `Runtime`, returning a [`JoinHandle`] for it.
83101
///
84102
/// Spawning a task enables the task to execute concurrently to other tasks.
85103
/// There is no guarantee that a spawned task will execute to completion. When a
86104
/// runtime is shutdown, all outstanding tasks are dropped, regardless of the
87105
/// lifecycle of that task.
88106
///
89-
/// This function must be called from the context of a `tokio-uring` runtime.
107+
/// This will create a new Runtime to run spawn.
90108
///
91109
/// [`JoinHandle`]: tokio::task::JoinHandle
92110
pub fn spawn<T: std::future::Future + 'static>(task: T) -> tokio::task::JoinHandle<T::Output> {
93-
CURRENT_RUNTIME.with(|rt| match rt {
94-
Runtime::Tokio(_) => tokio::task::spawn_local(task),
95-
#[cfg(target_os = "linux")]
96-
Runtime::Uring(_) => tokio_uring::spawn(task),
97-
})
111+
let rt = Runtime::new();
112+
rt.spawn(task)
98113
}
99114

100115
#[cfg(test)]

0 commit comments

Comments
 (0)