fn wakeup_waiters(&mut self) {
        // Wake as many tasks as the permits allow
        let mut available = self.permits;

        loop {
            match self.waiters.peek_last() {
                None => return,
                Some(last_waiter) => {
                    // Check if enough permits are available for this waiter.
                    // If not then a wakeup attempt won't be successful.
                    if available < last_waiter.required_permits {
                        return;
                    }
                    available -= last_waiter.required_permits;

                    // Notify the waiter that it can try to acquire the semaphore again.
                    // The notification gets tracked inside the waiter.
                    // If the waiter aborts it's wait (drops the future), another task
                    // must be woken.
                    if last_waiter.state != PollState::Notified {
                        last_waiter.state = PollState::Notified;

                        let task = &last_waiter.task;
                        if let Some(ref handle) = task {
                            handle.wake_by_ref();
                        }
                    }

                    // In the case of a non-fair semaphore, the waiters are directly
                    // removed from the semaphores wait queue when woken.
                    // That avoids having to remove the wait element later.
                    if !self.is_fair {
                        self.waiters.remove_last();
                    } else {
                        // For a fair Semaphore we never wake more than 1 task.
                        // That one needs to acquire the Semaphore.
                        // TODO: We actually should be able to wake more, since
                        // it's guaranteed that both tasks could make progress.
                        // However the we currently can't peek iterate in reverse order.
                        return;
                    }
                }
            }
        }
    }
/// Wakes up the last waiter and removes it from the wait queue
Wakes up the last waiter and removes it from the wait queue
Wakes up the last waiter and removes it from the wait queue
fn wakeup_waiters(&mut self) {
        // Wake as many tasks as the permits allow
        let mut available = self.permits;
        loop {
            match self.waiters.peek_last() {
                None => return,
                Some(last_waiter) => {
                    // Check if enough permits are available for this waiter.
                    // If not then a wakeup attempt won't be successful.
                    if available < last_waiter.required_permits {
                        return;
                    }
                    available -= last_waiter.required_permits;
                    // Notify the waiter that it can try to acquire the semaphore again.
                    // The notification gets tracked inside the waiter.
                    // If the waiter aborts it's wait (drops the future), another task
                    // must be woken.
                    if last_waiter.state != PollState::Notified {
                        last_waiter.state = PollState::Notified;
                        let task = &last_waiter.task;
                        if let Some(ref handle) = task {
                            handle.wake_by_ref();
                        }
                    }
                    // In the case of a non-fair semaphore, the waiters are directly
                    // removed from the semaphores wait queue when woken.
                    // That avoids having to remove the wait element later.
                    if !self.is_fair {
                        self.waiters.remove_last();
                    } else {
                        // For a fair Semaphore we never wake more than 1 task.
                        // That one needs to acquire the Semaphore.
                        // TODO: We actually should be able to wake more, since
                        // it's guaranteed that both tasks could make progress.
                        // However the we currently can't peek iterate in reverse order.
                        return;
                    }
                }
            }
        }
    }
fn wakeup_waiters ( & mut self ) { // Wake as many tasks as the permits allow let mut available = self . permits ; loop { match self . waiters . peek_last ( ) { None => return , Some ( last_waiter ) => { // Check if enough permits are available for this waiter. // If not then a wakeup attempt won't be successful. if available < last_waiter . required_permits { return ; } available -= last_waiter . required_permits ; // Notify the waiter that it can try to acquire the semaphore again. // The notification gets tracked inside the waiter. // If the waiter aborts it's wait (drops the future), another task // must be woken. if last_waiter . state != PollState :: Notified { last_waiter . state = PollState :: Notified ; let task = & last_waiter . task ; if let Some ( ref handle ) = task { handle . wake_by_ref ( ) ; } } // In the case of a non-fair semaphore, the waiters are directly // removed from the semaphores wait queue when woken. // That avoids having to remove the wait element later. if ! self . is_fair { self . waiters . remove_last ( ) ; } else { // For a fair Semaphore we never wake more than 1 task. // That one needs to acquire the Semaphore. // TODO: We actually should be able to wake more, since // it's guaranteed that both tasks could make progress. // However the we currently can't peek iterate in reverse order. return ; } } } } }
Wakes up the last waiter and removes it from the wait queue
Wakes up the last waiter and removes it from the wait queue
&mut self
&mut self