11//! A thread pool for running blocking functions asynchronously.
22
3- use std:: fmt;
4- use std:: pin:: Pin ;
53use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
64use std:: thread;
75use std:: time:: Duration ;
@@ -10,16 +8,16 @@ use crossbeam_channel::{bounded, Receiver, Sender};
108use lazy_static:: lazy_static;
119
1210use crate :: future:: Future ;
13- use crate :: task:: { Context , Poll } ;
11+ use crate :: task:: task :: { JoinHandle , Tag } ;
1412use crate :: utils:: abort_on_panic;
1513
1614const MAX_THREADS : u64 = 10_000 ;
1715
1816static DYNAMIC_THREAD_COUNT : AtomicU64 = AtomicU64 :: new ( 0 ) ;
1917
2018struct 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 > > ,
2321}
2422
2523lazy_static ! {
@@ -85,7 +83,7 @@ fn maybe_create_another_blocking_thread() {
8583// Enqueues work, attempting to send to the threadpool in a
8684// nonblocking way and spinning up another worker thread if
8785// 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 > ) {
8987 if let Err ( err) = POOL . sender . try_send ( t) {
9088 // We were not able to send to the channel without
9189 // blocking. Try to spin up another thread and then
@@ -98,35 +96,15 @@ fn schedule(t: async_task::Task<()>) {
9896/// Spawns a blocking task.
9997///
10098/// 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 >
102100where
103101 F : Future < Output = R > + Send + ' static ,
104102 R : Send + ' static ,
105103{
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) ;
107106 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)
130108}
131109
132110/// Generates a random number in `0..n`.
0 commit comments