@@ -12,17 +12,19 @@ use future::BoxFuture;
1212
1313mod unpark_mutex;
1414use self :: unpark_mutex:: UnparkMutex ;
15-
15+ mod unpark_handle;
16+ use self :: unpark_handle:: UnparkObj ;
1617mod task_rc;
1718mod data;
1819#[ allow( deprecated) ]
1920#[ cfg( feature = "with-deprecated" ) ]
2021pub use self :: task_rc:: TaskRc ;
2122pub use self :: data:: LocalKey ;
23+ pub use self :: unpark_handle:: UnparkHandle ;
2224
2325struct BorrowedTask < ' a > {
2426 id : usize ,
25- unpark : & ' a Arc < Unpark > ,
27+ unpark : UnparkHandle < ' a > ,
2628 map : & ' a data:: LocalMap ,
2729 events : Events ,
2830}
@@ -81,7 +83,7 @@ fn with<F: FnOnce(&BorrowedTask) -> R, R>(f: F) -> R {
8183#[ derive( Clone ) ]
8284pub struct Task {
8385 id : usize ,
84- unpark : Arc < Unpark > ,
86+ unpark : UnparkObj ,
8587 events : Events ,
8688}
8789
@@ -121,7 +123,7 @@ pub fn park() -> Task {
121123 Task {
122124 id : task. id ,
123125 events : task. events . clone ( ) ,
124- unpark : task. unpark . clone ( ) ,
126+ unpark : UnparkObj :: from ( task. unpark ) ,
125127 }
126128 } )
127129}
@@ -333,8 +335,8 @@ impl<F: Future> Spawn<F> {
333335 /// scheduled to receive a notification when poll can be called again.
334336 /// Otherwise if `Ready` or `Err` is returned, the `Spawn` task can be
335337 /// safely destroyed.
336- pub fn poll_future ( & mut self , unpark : Arc < Unpark > ) -> Poll < F :: Item , F :: Error > {
337- self . enter ( & unpark, |f| f. poll ( ) )
338+ pub fn poll_future ( & mut self , unpark : UnparkHandle ) -> Poll < F :: Item , F :: Error > {
339+ self . enter ( unpark, |f| f. poll ( ) )
338340 }
339341
340342 /// Waits for the internal future to complete, blocking this thread's
@@ -344,9 +346,11 @@ impl<F: Future> Spawn<F> {
344346 /// to complete. When a future cannot make progress it will use
345347 /// `thread::park` to block the current thread.
346348 pub fn wait_future ( & mut self ) -> Result < F :: Item , F :: Error > {
347- let unpark = Arc :: new ( ThreadUnpark :: new ( thread:: current ( ) ) ) ;
349+ let unpark = Arc :: new ( ThreadUnpark :: new ( ) ) ;
350+ let mut unpark2 = unpark. clone ( ) ;
351+ let handle = UnparkHandle :: new ( & mut unpark2) ;
348352 loop {
349- match try!( self . poll_future ( unpark . clone ( ) ) ) {
353+ match try!( self . poll_future ( handle ) ) {
350354 Async :: NotReady => unpark. park ( ) ,
351355 Async :: Ready ( e) => return Ok ( e) ,
352356 }
@@ -391,17 +395,19 @@ impl<F: Future> Spawn<F> {
391395
392396impl < S : Stream > Spawn < S > {
393397 /// Like `poll_future`, except polls the underlying stream.
394- pub fn poll_stream ( & mut self , unpark : Arc < Unpark > )
398+ pub fn poll_stream ( & mut self , unpark : UnparkHandle )
395399 -> Poll < Option < S :: Item > , S :: Error > {
396- self . enter ( & unpark, |stream| stream. poll ( ) )
400+ self . enter ( unpark, |stream| stream. poll ( ) )
397401 }
398402
399403 /// Like `wait_future`, except only waits for the next element to arrive on
400404 /// the underlying stream.
401405 pub fn wait_stream ( & mut self ) -> Option < Result < S :: Item , S :: Error > > {
402- let unpark = Arc :: new ( ThreadUnpark :: new ( thread:: current ( ) ) ) ;
406+ let unpark = Arc :: new ( ThreadUnpark :: new ( ) ) ;
407+ let mut unpark2 = unpark. clone ( ) ;
408+ let handle = UnparkHandle :: new ( & mut unpark2) ;
403409 loop {
404- match self . poll_stream ( unpark . clone ( ) ) {
410+ match self . poll_stream ( handle ) {
405411 Ok ( Async :: NotReady ) => unpark. park ( ) ,
406412 Ok ( Async :: Ready ( Some ( e) ) ) => return Some ( Ok ( e) ) ,
407413 Ok ( Async :: Ready ( None ) ) => return None ,
@@ -417,7 +423,7 @@ impl<S: Sink> Spawn<S> {
417423 /// If the underlying operation returns `NotReady` then the `unpark` value
418424 /// passed in will receive a notification when the operation is ready to be
419425 /// attempted again.
420- pub fn start_send ( & mut self , value : S :: SinkItem , unpark : & Arc < Unpark > )
426+ pub fn start_send ( & mut self , value : S :: SinkItem , unpark : UnparkHandle )
421427 -> StartSend < S :: SinkItem , S :: SinkError > {
422428 self . enter ( unpark, |sink| sink. start_send ( value) )
423429 }
@@ -427,7 +433,7 @@ impl<S: Sink> Spawn<S> {
427433 /// If the underlying operation returns `NotReady` then the `unpark` value
428434 /// passed in will receive a notification when the operation is ready to be
429435 /// attempted again.
430- pub fn poll_flush ( & mut self , unpark : & Arc < Unpark > )
436+ pub fn poll_flush ( & mut self , unpark : UnparkHandle )
431437 -> Poll < ( ) , S :: SinkError > {
432438 self . enter ( unpark, |sink| sink. poll_complete ( ) )
433439 }
@@ -439,10 +445,11 @@ impl<S: Sink> Spawn<S> {
439445 /// be blocked until it's able to send the value.
440446 pub fn wait_send ( & mut self , mut value : S :: SinkItem )
441447 -> Result < ( ) , S :: SinkError > {
442- let unpark = Arc :: new ( ThreadUnpark :: new ( thread:: current ( ) ) ) ;
443- let unpark2 = unpark. clone ( ) as Arc < Unpark > ;
448+ let unpark = Arc :: new ( ThreadUnpark :: new ( ) ) ;
449+ let mut unpark2 = unpark. clone ( ) ;
450+ let handle = UnparkHandle :: new ( & mut unpark2) ;
444451 loop {
445- value = match try!( self . start_send ( value, & unpark2 ) ) {
452+ value = match try!( self . start_send ( value, handle ) ) {
446453 AsyncSink :: NotReady ( v) => v,
447454 AsyncSink :: Ready => return Ok ( ( ) ) ,
448455 } ;
@@ -459,10 +466,11 @@ impl<S: Sink> Spawn<S> {
459466 /// The thread will be blocked until `poll_complete` returns that it's
460467 /// ready.
461468 pub fn wait_flush ( & mut self ) -> Result < ( ) , S :: SinkError > {
462- let unpark = Arc :: new ( ThreadUnpark :: new ( thread:: current ( ) ) ) ;
463- let unpark2 = unpark. clone ( ) as Arc < Unpark > ;
469+ let unpark = Arc :: new ( ThreadUnpark :: new ( ) ) ;
470+ let mut unpark2 = unpark. clone ( ) ;
471+ let handle = UnparkHandle :: new ( & mut unpark2) ;
464472 loop {
465- if try!( self . poll_flush ( & unpark2 ) ) . is_ready ( ) {
473+ if try!( self . poll_flush ( handle ) ) . is_ready ( ) {
466474 return Ok ( ( ) )
467475 }
468476 unpark. park ( ) ;
@@ -471,7 +479,7 @@ impl<S: Sink> Spawn<S> {
471479}
472480
473481impl < T > Spawn < T > {
474- fn enter < F , R > ( & mut self , unpark : & Arc < Unpark > , f : F ) -> R
482+ fn enter < F , R > ( & mut self , unpark : UnparkHandle , f : F ) -> R
475483 where F : FnOnce ( & mut T ) -> R
476484 {
477485 let task = BorrowedTask {
@@ -501,7 +509,7 @@ impl<T: fmt::Debug> fmt::Debug for Spawn<T> {
501509/// `Spawn::poll_stream` functions. It's transitively used as part of the
502510/// `Task::unpark` method to internally deliver notifications of readiness of a
503511/// future to move forward.
504- pub trait Unpark : Send + Sync {
512+ pub trait Unpark : Send {
505513 /// Indicates that an associated future and/or task are ready to make
506514 /// progress.
507515 ///
@@ -510,6 +518,12 @@ pub trait Unpark: Send + Sync {
510518 fn unpark ( & self ) ;
511519}
512520
521+ impl < T : Unpark + Sync > Unpark for Arc < T > {
522+ fn unpark ( & self ) {
523+ ( * * self ) . unpark ( ) ;
524+ }
525+ }
526+
513527/// A trait representing requests to poll futures.
514528///
515529/// This trait is an argument to the `Spawn::execute` which is used to run a
@@ -526,9 +540,9 @@ struct ThreadUnpark {
526540}
527541
528542impl ThreadUnpark {
529- fn new ( thread : thread :: Thread ) -> ThreadUnpark {
543+ fn new ( ) -> ThreadUnpark {
530544 ThreadUnpark {
531- thread : thread,
545+ thread : thread:: current ( ) ,
532546 ready : AtomicBool :: new ( false ) ,
533547 }
534548 }
@@ -563,15 +577,14 @@ impl Run {
563577 /// Actually run the task (invoking `poll` on its future) on the current
564578 /// thread.
565579 pub fn run ( self ) {
566- let Run { mut spawn, inner } = self ;
567-
580+ let Run { mut spawn, mut inner } = self ;
568581 // SAFETY: the ownership of this `Run` object is evidence that
569582 // we are in the `POLLING`/`REPOLL` state for the mutex.
570583 unsafe {
571584 inner. mutex . start_poll ( ) ;
572585
573586 loop {
574- match spawn. poll_future ( inner . clone ( ) ) {
587+ match spawn. poll_future ( UnparkHandle :: new ( & mut inner ) ) {
575588 Ok ( Async :: NotReady ) => { }
576589 Ok ( Async :: Ready ( ( ) ) ) |
577590 Err ( ( ) ) => return inner. mutex . complete ( ) ,
0 commit comments