@@ -49,6 +49,27 @@ impl Runtime {
49
49
Runtime :: Uring ( rt) => rt. lock ( ) . unwrap ( ) . block_on ( f) ,
50
50
}
51
51
}
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
+ }
52
73
}
53
74
54
75
/// Start an async runtime.
@@ -62,39 +83,33 @@ impl Default for Runtime {
62
83
}
63
84
}
64
85
65
- std:: thread_local! {
66
- pub ( crate ) static CURRENT_RUNTIME : Runtime = Runtime :: new( ) ;
67
- }
68
-
69
86
/// Run a callback with the default `Runtime` object.
70
87
pub fn with_runtime < F , R > ( f : F ) -> R
71
88
where
72
89
F : FnOnce ( & Runtime ) -> R ,
73
90
{
74
- CURRENT_RUNTIME . with ( f)
91
+ let rt = Runtime :: new ( ) ;
92
+ f ( & rt)
75
93
}
76
94
77
95
/// Run a future to completion with the default `Runtime` object.
78
96
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)
80
98
}
81
99
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.
83
101
///
84
102
/// Spawning a task enables the task to execute concurrently to other tasks.
85
103
/// There is no guarantee that a spawned task will execute to completion. When a
86
104
/// runtime is shutdown, all outstanding tasks are dropped, regardless of the
87
105
/// lifecycle of that task.
88
106
///
89
- /// This function must be called from the context of a `tokio-uring` runtime .
107
+ /// This will create a new Runtime to run spawn .
90
108
///
91
109
/// [`JoinHandle`]: tokio::task::JoinHandle
92
110
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)
98
113
}
99
114
100
115
#[ cfg( test) ]
0 commit comments