11use std:: cell:: RefCell ;
22use std:: future:: Future ;
3- use std:: task:: { Context , Poll } ;
43
5- static GLOBAL_EXECUTOR : once_cell:: sync:: Lazy < multitask:: Executor > = once_cell:: sync:: Lazy :: new ( multitask:: Executor :: new) ;
6-
7- struct Executor {
8- local_executor : multitask:: LocalExecutor ,
9- parker : async_io:: parking:: Parker ,
10- }
4+ static GLOBAL_EXECUTOR : once_cell:: sync:: Lazy < async_executor:: Executor > = once_cell:: sync:: Lazy :: new ( async_executor:: Executor :: new) ;
115
126thread_local ! {
13- static EXECUTOR : RefCell <Executor > = RefCell :: new( {
14- let ( parker, unparker) = async_io:: parking:: pair( ) ;
15- let local_executor = multitask:: LocalExecutor :: new( move || unparker. unpark( ) ) ;
16- Executor { local_executor, parker }
17- } ) ;
7+ static EXECUTOR : RefCell <async_executor:: LocalExecutor > = RefCell :: new( async_executor:: LocalExecutor :: new( ) ) ;
188}
199
20- pub ( crate ) fn spawn < F , T > ( future : F ) -> multitask :: Task < T >
10+ pub ( crate ) fn spawn < F , T > ( future : F ) -> async_executor :: Task < T >
2111where
2212 F : Future < Output = T > + Send + ' static ,
2313 T : Send + ' static ,
@@ -26,35 +16,26 @@ where
2616}
2717
2818#[ cfg( feature = "unstable" ) ]
29- pub ( crate ) fn local < F , T > ( future : F ) -> multitask :: Task < T >
19+ pub ( crate ) fn local < F , T > ( future : F ) -> async_executor :: Task < T >
3020where
3121 F : Future < Output = T > + ' static ,
3222 T : ' static ,
3323{
34- EXECUTOR . with ( |executor| executor. borrow ( ) . local_executor . spawn ( future) )
24+ EXECUTOR . with ( |executor| executor. borrow ( ) . spawn ( future) )
3525}
3626
3727pub ( crate ) fn run < F , T > ( future : F ) -> T
3828where
3929 F : Future < Output = T > ,
4030{
41- enter ( || EXECUTOR . with ( |executor| {
42- let executor = executor. borrow ( ) ;
43- let unparker = executor. parker . unparker ( ) ;
44- let global_ticker = GLOBAL_EXECUTOR . ticker ( move || unparker. unpark ( ) ) ;
45- let unparker = executor. parker . unparker ( ) ;
46- let waker = async_task:: waker_fn ( move || unparker. unpark ( ) ) ;
47- let cx = & mut Context :: from_waker ( & waker) ;
48- pin_utils:: pin_mut!( future) ;
49- loop {
50- if let Poll :: Ready ( res) = future. as_mut ( ) . poll ( cx) {
51- return res;
52- }
53- if let Ok ( false ) = std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || executor. local_executor . tick ( ) || global_ticker. tick ( ) ) ) {
54- executor. parker . park ( ) ;
55- }
56- }
57- } ) )
31+ EXECUTOR . with ( |executor| enter ( || GLOBAL_EXECUTOR . enter ( || executor. borrow ( ) . run ( future) ) ) )
32+ }
33+
34+ pub ( crate ) fn run_global < F , T > ( future : F ) -> T
35+ where
36+ F : Future < Output = T > ,
37+ {
38+ enter ( || GLOBAL_EXECUTOR . run ( future) )
5839}
5940
6041/// Enters the tokio context if the `tokio` feature is enabled.
0 commit comments