1
1
//! A thread pool for running blocking functions asynchronously.
2
2
3
- use std:: fmt;
4
- use std:: pin:: Pin ;
5
3
use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
6
4
use std:: thread;
7
5
use std:: time:: Duration ;
@@ -10,16 +8,16 @@ use crossbeam_channel::{bounded, Receiver, Sender};
10
8
use lazy_static:: lazy_static;
11
9
12
10
use crate :: future:: Future ;
13
- use crate :: task:: { Context , Poll } ;
11
+ use crate :: task:: task :: { JoinHandle , Tag } ;
14
12
use crate :: utils:: abort_on_panic;
15
13
16
14
const MAX_THREADS : u64 = 10_000 ;
17
15
18
16
static DYNAMIC_THREAD_COUNT : AtomicU64 = AtomicU64 :: new ( 0 ) ;
19
17
20
18
struct Pool {
21
- sender : Sender < async_task:: Task < ( ) > > ,
22
- receiver : Receiver < async_task:: Task < ( ) > > ,
19
+ sender : Sender < async_task:: Task < Tag > > ,
20
+ receiver : Receiver < async_task:: Task < Tag > > ,
23
21
}
24
22
25
23
lazy_static ! {
@@ -85,7 +83,7 @@ fn maybe_create_another_blocking_thread() {
85
83
// Enqueues work, attempting to send to the threadpool in a
86
84
// nonblocking way and spinning up another worker thread if
87
85
// there is not a thread ready to accept the work.
88
- fn schedule ( t : async_task:: Task < ( ) > ) {
86
+ fn schedule ( t : async_task:: Task < Tag > ) {
89
87
if let Err ( err) = POOL . sender . try_send ( t) {
90
88
// We were not able to send to the channel without
91
89
// blocking. Try to spin up another thread and then
@@ -98,35 +96,15 @@ fn schedule(t: async_task::Task<()>) {
98
96
/// Spawns a blocking task.
99
97
///
100
98
/// The task will be spawned onto a thread pool specifically dedicated to blocking tasks.
101
- pub fn spawn < F , R > ( future : F ) -> JoinHandle < R >
99
+ pub ( crate ) fn spawn < F , R > ( future : F ) -> JoinHandle < R >
102
100
where
103
101
F : Future < Output = R > + Send + ' static ,
104
102
R : Send + ' static ,
105
103
{
106
- let ( task, handle) = async_task:: spawn ( future, schedule, ( ) ) ;
104
+ let tag = Tag :: new ( None ) ;
105
+ let ( task, handle) = async_task:: spawn ( future, schedule, tag) ;
107
106
task. schedule ( ) ;
108
- JoinHandle ( handle)
109
- }
110
-
111
- /// A handle to a blocking task.
112
- pub struct JoinHandle < R > ( async_task:: JoinHandle < R , ( ) > ) ;
113
-
114
- impl < R > Unpin for JoinHandle < R > { }
115
-
116
- impl < R > Future for JoinHandle < R > {
117
- type Output = R ;
118
-
119
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
120
- Pin :: new ( & mut self . 0 ) . poll ( cx) . map ( |out| out. unwrap ( ) )
121
- }
122
- }
123
-
124
- impl < R > fmt:: Debug for JoinHandle < R > {
125
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
126
- f. debug_struct ( "JoinHandle" )
127
- . field ( "handle" , & self . 0 )
128
- . finish ( )
129
- }
107
+ JoinHandle :: new ( handle)
130
108
}
131
109
132
110
/// Generates a random number in `0..n`.
0 commit comments