1
1
use std:: cell:: RefCell ;
2
2
use std:: future:: Future ;
3
- use std:: task:: { Context , Poll } ;
4
3
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) ;
11
5
12
6
thread_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( ) ) ;
18
8
}
19
9
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 >
21
11
where
22
12
F : Future < Output = T > + Send + ' static ,
23
13
T : Send + ' static ,
@@ -26,35 +16,26 @@ where
26
16
}
27
17
28
18
#[ 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 >
30
20
where
31
21
F : Future < Output = T > + ' static ,
32
22
T : ' static ,
33
23
{
34
- EXECUTOR . with ( |executor| executor. borrow ( ) . local_executor . spawn ( future) )
24
+ EXECUTOR . with ( |executor| executor. borrow ( ) . spawn ( future) )
35
25
}
36
26
37
27
pub ( crate ) fn run < F , T > ( future : F ) -> T
38
28
where
39
29
F : Future < Output = T > ,
40
30
{
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) )
58
39
}
59
40
60
41
/// Enters the tokio context if the `tokio` feature is enabled.
0 commit comments