unsafe fn run(ptr: *const ()) {
        let raw = Self::from_ptr(ptr);

        // Create a context from the raw proc pointer and the vtable inside the its pdata.
        let waker = ManuallyDrop::new(Waker::from_raw(RawWaker::new(
            ptr,
            &(*raw.pdata).vtable.raw_waker,
        )));
        let cx = &mut Context::from_waker(&waker);

        let mut state = (*raw.pdata).state.load(Ordering::Acquire);

        // Update the proc's state before polling its future.
        loop {
            // If the proc has been closed, drop the proc reference and return.
            if state & CLOSED != 0 {
                // Notify the awaiter that the proc has been closed.
                if state & AWAITER != 0 {
                    (*raw.pdata).notify();
                }

                // Drop the future.
                Self::drop_future(ptr);

                // Drop the proc reference.
                Self::decrement(ptr);
                return;
            }

            // Mark the proc as unscheduled and running.
            match (*raw.pdata).state.compare_exchange_weak(
                state,
                (state & !SCHEDULED) | RUNNING,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => {
                    // Update the state because we're continuing with polling the future.
                    state = (state & !SCHEDULED) | RUNNING;
                    break;
                }
                Err(s) => state = s,
            }
        }

        // Poll the inner future, but surround it with a guard that closes the proc in case polling
        // panics.
        let guard = Guard(raw);

        if let Some(before_start_cb) = &(*raw.stack).before_start {
            (*before_start_cb.clone())((*raw.stack).state.clone());
        }

        let poll = <F as Future>::poll(Pin::new_unchecked(&mut *raw.future), cx);
        mem::forget(guard);

        match poll {
            Poll::Ready(out) => {
                // Replace the future with its output.
                Self::drop_future(ptr);
                raw.output.write(out);

                // A place where the output will be stored in case it needs to be dropped.
                let mut output = None;

                // The proc is now completed.
                loop {
                    // If the handle is dropped, we'll need to close it and drop the output.
                    let new = if state & HANDLE == 0 {
                        (state & !RUNNING & !SCHEDULED) | COMPLETED | CLOSED
                    } else {
                        (state & !RUNNING & !SCHEDULED) | COMPLETED
                    };

                    // Mark the proc as not running and completed.
                    match (*raw.pdata).state.compare_exchange_weak(
                        state,
                        new,
                        Ordering::AcqRel,
                        Ordering::Acquire,
                    ) {
                        Ok(_) => {
                            // If the handle is dropped or if the proc was closed while running,
                            // now it's time to drop the output.
                            if state & HANDLE == 0 || state & CLOSED != 0 {
                                // Read the output.
                                output = Some(raw.output.read());
                            }

                            // Notify the awaiter that the proc has been completed.
                            if state & AWAITER != 0 {
                                (*raw.pdata).notify();
                            }

                            if let Some(after_complete_cb) = &(*raw.stack).after_complete {
                                (*after_complete_cb.clone())((*raw.stack).state.clone());
                            }

                            // Drop the proc reference.
                            Self::decrement(ptr);
                            break;
                        }
                        Err(s) => state = s,
                    }
                }

                // Drop the output if it was taken out of the proc.
                drop(output);
            }
            Poll::Pending => {
                // The proc is still not completed.
                loop {
                    // If the proc was closed while running, we'll need to unschedule in case it
                    // was woken and then clean up its resources.
                    let new = if state & CLOSED != 0 {
                        state & !RUNNING & !SCHEDULED
                    } else {
                        state & !RUNNING
                    };

                    // Mark the proc as not running.
                    match (*raw.pdata).state.compare_exchange_weak(
                        state,
                        new,
                        Ordering::AcqRel,
                        Ordering::Acquire,
                    ) {
                        Ok(state) => {
                            // If the proc was closed while running, we need to drop its future.
                            // If the proc was woken while running, we need to schedule it.
                            // Otherwise, we just drop the proc reference.
                            if state & CLOSED != 0 {
                                // The thread that closed the proc didn't drop the future because
                                // it was running so now it's our responsibility to do so.
                                Self::drop_future(ptr);

                                // Drop the proc reference.
                                Self::decrement(ptr);
                            } else if state & SCHEDULED != 0 {
                                // The thread that has woken the proc didn't reschedule it because
                                // it was running so now it's our responsibility to do so.
                                Self::schedule(ptr);
                            } else {
                                // Drop the proc reference.
                                Self::decrement(ptr);
                            }
                            break;
                        }
                        Err(s) => state = s,
                    }
                }
            }
        }
    }
/// Runs a proc.
///
/// If polling its future panics, the proc will be closed and the panic propagated into the
/// caller.
Runs a proc.
If polling its future panics, the proc will be closed and the panic propagated into the
caller.
Runs a proc . If polling its future panics the proc will be closed and the panic propagated into the caller .
unsafe fn run(ptr: *const ()) {
        let raw = Self::from_ptr(ptr);
        // Create a context from the raw proc pointer and the vtable inside the its pdata.
        let waker = ManuallyDrop::new(Waker::from_raw(RawWaker::new(
            ptr,
            &(*raw.pdata).vtable.raw_waker,
        )));
        let cx = &mut Context::from_waker(&waker);
        let mut state = (*raw.pdata).state.load(Ordering::Acquire);
        // Update the proc's state before polling its future.
        loop {
            // If the proc has been closed, drop the proc reference and return.
            if state & CLOSED != 0 {
                // Notify the awaiter that the proc has been closed.
                if state & AWAITER != 0 {
                    (*raw.pdata).notify();
                }
                // Drop the future.
                Self::drop_future(ptr);
                // Drop the proc reference.
                Self::decrement(ptr);
                return;
            }
            // Mark the proc as unscheduled and running.
            match (*raw.pdata).state.compare_exchange_weak(
                state,
                (state & !SCHEDULED) | RUNNING,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => {
                    // Update the state because we're continuing with polling the future.
                    state = (state & !SCHEDULED) | RUNNING;
                    break;
                }
                Err(s) => state = s,
            }
        }
        // Poll the inner future, but surround it with a guard that closes the proc in case polling
        // panics.
        let guard = Guard(raw);
        if let Some(before_start_cb) = &(*raw.stack).before_start {
            (*before_start_cb.clone())((*raw.stack).state.clone());
        }
        let poll = <F as Future>::poll(Pin::new_unchecked(&mut *raw.future), cx);
        mem::forget(guard);
        match poll {
            Poll::Ready(out) => {
                // Replace the future with its output.
                Self::drop_future(ptr);
                raw.output.write(out);
                // A place where the output will be stored in case it needs to be dropped.
                let mut output = None;
                // The proc is now completed.
                loop {
                    // If the handle is dropped, we'll need to close it and drop the output.
                    let new = if state & HANDLE == 0 {
                        (state & !RUNNING & !SCHEDULED) | COMPLETED | CLOSED
                    } else {
                        (state & !RUNNING & !SCHEDULED) | COMPLETED
                    };
                    // Mark the proc as not running and completed.
                    match (*raw.pdata).state.compare_exchange_weak(
                        state,
                        new,
                        Ordering::AcqRel,
                        Ordering::Acquire,
                    ) {
                        Ok(_) => {
                            // If the handle is dropped or if the proc was closed while running,
                            // now it's time to drop the output.
                            if state & HANDLE == 0 || state & CLOSED != 0 {
                                // Read the output.
                                output = Some(raw.output.read());
                            }
                            // Notify the awaiter that the proc has been completed.
                            if state & AWAITER != 0 {
                                (*raw.pdata).notify();
                            }
                            if let Some(after_complete_cb) = &(*raw.stack).after_complete {
                                (*after_complete_cb.clone())((*raw.stack).state.clone());
                            }
                            // Drop the proc reference.
                            Self::decrement(ptr);
                            break;
                        }
                        Err(s) => state = s,
                    }
                }
                // Drop the output if it was taken out of the proc.
                drop(output);
            }
            Poll::Pending => {
                // The proc is still not completed.
                loop {
                    // If the proc was closed while running, we'll need to unschedule in case it
                    // was woken and then clean up its resources.
                    let new = if state & CLOSED != 0 {
                        state & !RUNNING & !SCHEDULED
                    } else {
                        state & !RUNNING
                    };
                    // Mark the proc as not running.
                    match (*raw.pdata).state.compare_exchange_weak(
                        state,
                        new,
                        Ordering::AcqRel,
                        Ordering::Acquire,
                    ) {
                        Ok(state) => {
                            // If the proc was closed while running, we need to drop its future.
                            // If the proc was woken while running, we need to schedule it.
                            // Otherwise, we just drop the proc reference.
                            if state & CLOSED != 0 {
                                // The thread that closed the proc didn't drop the future because
                                // it was running so now it's our responsibility to do so.
                                Self::drop_future(ptr);
                                // Drop the proc reference.
                                Self::decrement(ptr);
                            } else if state & SCHEDULED != 0 {
                                // The thread that has woken the proc didn't reschedule it because
                                // it was running so now it's our responsibility to do so.
                                Self::schedule(ptr);
                            } else {
                                // Drop the proc reference.
                                Self::decrement(ptr);
                            }
                            break;
                        }
                        Err(s) => state = s,
                    }
                }
            }
        }
    }
unsafe fn run ( ptr : * const ( ) ) { let raw = Self :: from_ptr ( ptr ) ; // Create a context from the raw proc pointer and the vtable inside the its pdata. let waker = ManuallyDrop :: new ( Waker :: from_raw ( RawWaker :: new ( ptr , & ( * raw . pdata ) . vtable . raw_waker , ) ) ) ; let cx = & mut Context :: from_waker ( & waker ) ; let mut state = ( * raw . pdata ) . state . load ( Ordering :: Acquire ) ; // Update the proc's state before polling its future. loop { // If the proc has been closed, drop the proc reference and return. if state & CLOSED != 0 { // Notify the awaiter that the proc has been closed. if state & AWAITER != 0 { ( * raw . pdata ) . notify ( ) ; } // Drop the future. Self :: drop_future ( ptr ) ; // Drop the proc reference. Self :: decrement ( ptr ) ; return ; } // Mark the proc as unscheduled and running. match ( * raw . pdata ) . state . compare_exchange_weak ( state , ( state & ! SCHEDULED ) | RUNNING , Ordering :: AcqRel , Ordering :: Acquire , ) { Ok ( _ ) => { // Update the state because we're continuing with polling the future. state = ( state & ! SCHEDULED ) | RUNNING ; break ; } Err ( s ) => state = s , } } // Poll the inner future, but surround it with a guard that closes the proc in case polling // panics. let guard = Guard ( raw ) ; if let Some ( before_start_cb ) = & ( * raw . stack ) . before_start { ( * before_start_cb . clone ( ) ) ( ( * raw . stack ) . state . clone ( ) ) ; } let poll = < F as Future > :: poll ( Pin :: new_unchecked ( & mut * raw . future ) , cx ) ; mem :: forget ( guard ) ; match poll { Poll :: Ready ( out ) => { // Replace the future with its output. Self :: drop_future ( ptr ) ; raw . output . write ( out ) ; // A place where the output will be stored in case it needs to be dropped. let mut output = None ; // The proc is now completed. loop { // If the handle is dropped, we'll need to close it and drop the output. let new = if state & HANDLE == 0 { ( state & ! RUNNING & ! SCHEDULED ) | COMPLETED | CLOSED } else { ( state & ! RUNNING & ! SCHEDULED ) | COMPLETED } ; // Mark the proc as not running and completed. match ( * raw . pdata ) . state . compare_exchange_weak ( state , new , Ordering :: AcqRel , Ordering :: Acquire , ) { Ok ( _ ) => { // If the handle is dropped or if the proc was closed while running, // now it's time to drop the output. if state & HANDLE == 0 || state & CLOSED != 0 { // Read the output. output = Some ( raw . output . read ( ) ) ; } // Notify the awaiter that the proc has been completed. if state & AWAITER != 0 { ( * raw . pdata ) . notify ( ) ; } if let Some ( after_complete_cb ) = & ( * raw . stack ) . after_complete { ( * after_complete_cb . clone ( ) ) ( ( * raw . stack ) . state . clone ( ) ) ; } // Drop the proc reference. Self :: decrement ( ptr ) ; break ; } Err ( s ) => state = s , } } // Drop the output if it was taken out of the proc. drop ( output ) ; } Poll :: Pending => { // The proc is still not completed. loop { // If the proc was closed while running, we'll need to unschedule in case it // was woken and then clean up its resources. let new = if state & CLOSED != 0 { state & ! RUNNING & ! SCHEDULED } else { state & ! RUNNING } ; // Mark the proc as not running. match ( * raw . pdata ) . state . compare_exchange_weak ( state , new , Ordering :: AcqRel , Ordering :: Acquire , ) { Ok ( state ) => { // If the proc was closed while running, we need to drop its future. // If the proc was woken while running, we need to schedule it. // Otherwise, we just drop the proc reference. if state & CLOSED != 0 { // The thread that closed the proc didn't drop the future because // it was running so now it's our responsibility to do so. Self :: drop_future ( ptr ) ; // Drop the proc reference. Self :: decrement ( ptr ) ; } else if state & SCHEDULED != 0 { // The thread that has woken the proc didn't reschedule it because // it was running so now it's our responsibility to do so. Self :: schedule ( ptr ) ; } else { // Drop the proc reference. Self :: decrement ( ptr ) ; } break ; } Err ( s ) => state = s , } } } } }
Runs a proc.
Runs a proc .
ptr
*const ()
ptr
*const ()