2626//! function is similar to the standard library's `thread::park` method where it
2727//! returns a handle to wake up a task at a later date (via an `unpark` method).
2828
29- mod unpark_mutex;
30- mod task_rc;
31- pub use self :: task_rc:: TaskRc ;
32-
3329use { BoxFuture , Poll } ;
3430
3531use std:: prelude:: v1:: * ;
3632
37- use std:: thread;
38- use std:: cell:: { Cell , RefCell } ;
33+ use std:: cell:: Cell ;
3934use std:: sync:: Arc ;
4035use std:: sync:: atomic:: { Ordering , AtomicUsize , ATOMIC_USIZE_INIT } ;
36+ use std:: thread;
4137
4238use self :: unpark_mutex:: UnparkMutex ;
4339
44- use typemap:: { SendMap , TypeMap } ;
40+ mod unpark_mutex;
41+ mod task_rc;
42+ mod data;
43+ pub use self :: task_rc:: TaskRc ;
44+ pub use self :: data:: LocalKey ;
4545
46- thread_local ! ( static CURRENT_TASK : Cell <( * const Task , * const TaskData ) > = {
46+ thread_local ! ( static CURRENT_TASK : Cell <( * const Task , * const data :: LocalMap ) > = {
4747 Cell :: new( ( 0 as * const _, 0 as * const _) )
4848} ) ;
4949
@@ -57,10 +57,10 @@ fn fresh_task_id() -> usize {
5757 return id
5858}
5959
60- fn set < F , R > ( task : & Task , data : & TaskData , f : F ) -> R
60+ fn set < F , R > ( task : & Task , data : & data :: LocalMap , f : F ) -> R
6161 where F : FnOnce ( ) -> R
6262{
63- struct Reset ( ( * const Task , * const TaskData ) ) ;
63+ struct Reset ( ( * const Task , * const data :: LocalMap ) ) ;
6464 impl Drop for Reset {
6565 fn drop ( & mut self ) {
6666 CURRENT_TASK . with ( |c| c. set ( self . 0 ) ) ;
@@ -74,7 +74,7 @@ fn set<F, R>(task: &Task, data: &TaskData, f: F) -> R
7474 } )
7575}
7676
77- fn with < F : FnOnce ( & Task , & TaskData ) -> R , R > ( f : F ) -> R {
77+ fn with < F : FnOnce ( & Task , & data :: LocalMap ) -> R , R > ( f : F ) -> R {
7878 let ( task, data) = CURRENT_TASK . with ( |c| c. get ( ) ) ;
7979 assert ! ( !task. is_null( ) , "no Task is currently running" ) ;
8080 assert ! ( !data. is_null( ) ) ;
@@ -148,49 +148,6 @@ pub fn with_unpark_event<F, R>(event: UnparkEvent, f: F) -> R
148148 } )
149149}
150150
151- /// Access the current task's local data.
152- ///
153- /// Each task has its own set of local data, which is required to be `Send` but
154- /// not `Sync`. Futures within a task can access this data at any point.
155- ///
156- /// # Panics
157- ///
158- /// This function will panic if a task is not currently being executed. That
159- /// is, this method can be dangerous to call outside of an implementation of
160- /// `poll`.
161- ///
162- /// It will also panic if called in the context of another `with_local_data` or
163- /// `with_local_data_mut`.
164- pub fn with_local_data < F , R > ( f : F ) -> R
165- where F : FnOnce ( & SendMap ) -> R
166- {
167- with ( |_, data| {
168- f ( & * data. borrow ( ) )
169- } )
170- }
171-
172- /// Access the current task's local data, mutably.
173- ///
174- /// Each task has its own set of local data, which is required to be `Send` but
175- /// not `Sync`. Futures within a task can access this data at any point.
176- ///
177- /// # Panics
178- ///
179- /// This function will panic if a task is not currently being executed. That
180- /// is, this method can be dangerous to call outside of an implementation of
181- /// `poll`.
182- ///
183- /// It will also panic if called in the context of another `with_local_data` or
184- /// `with_local_data_mut`.
185- pub fn with_local_data_mut < F , R > ( f : F ) -> R
186- where F : FnOnce ( & mut SendMap ) -> R
187- {
188- with ( |_, data| {
189- f ( & mut * data. borrow_mut ( ) )
190- } )
191- }
192-
193-
194151/// A handle to a "task", which represents a single lightweight "thread" of
195152/// execution driving a future to completion.
196153///
@@ -222,11 +179,9 @@ enum TaskKind {
222179 Local ( thread:: Thread ) ,
223180}
224181
225- type TaskData = RefCell < SendMap > ;
226-
227182struct MutexInner {
228183 future : BoxFuture < ( ) , ( ) > ,
229- task_data : TaskData ,
184+ task_data : data :: LocalMap ,
230185}
231186
232187/// A task intended to be run directly on a local thread.
@@ -239,7 +194,7 @@ struct MutexInner {
239194/// `std::thread::Thread::unpark` for the thread that entered the task.
240195pub struct ThreadTask {
241196 id : usize ,
242- task_data : TaskData ,
197+ task_data : data :: LocalMap ,
243198}
244199
245200/// A handle for running an executor-bound task.
@@ -254,7 +209,7 @@ impl ThreadTask {
254209 pub fn new ( ) -> ThreadTask {
255210 ThreadTask {
256211 id : fresh_task_id ( ) ,
257- task_data : RefCell :: new ( TypeMap :: custom ( ) ) ,
212+ task_data : data :: local_map ( ) ,
258213 }
259214 }
260215
@@ -364,13 +319,13 @@ impl Task {
364319 ///
365320 /// Does not actually begin task execution; use the `unpark` method to do
366321 /// so.
367- pub fn new ( ) -> Task {
322+ pub fn new ( exec : Arc < Executor > , future : BoxFuture < ( ) , ( ) > ) -> Task {
368323 Task {
369324 id : fresh_task_id ( ) ,
370325 kind : TaskKind :: Executor {
371326 mutex : Arc :: new ( UnparkMutex :: new ( MutexInner {
372327 future : future,
373- task_data : RefCell :: new ( TypeMap :: custom ( ) ) ,
328+ task_data : data :: local_map ( ) ,
374329 } ) ) ,
375330 exec : exec
376331 } ,
@@ -408,15 +363,6 @@ impl Task {
408363 TaskKind :: Local ( ref thread) => thread. unpark ( )
409364 }
410365 }
411-
412- /// Determines whether the underlying future has ever polled with a final
413- /// result (or panicked), and thus terminated.
414- pub fn is_done ( & self ) -> bool {
415- match self . kind {
416- TaskKind :: Executor { ref mutex, .. } => mutex. is_complete ( ) ,
417- _ => false ,
418- }
419- }
420366}
421367
422368impl Events {
0 commit comments