Repository: tokio-rs/tokio
File: tokio/src/runtime/scheduler/multi_thread/worker.rs (Rust)

Code before:
                // really count as an async poll anymore.
                core.metrics.end_poll();

                // Check for a task in the LIFO slot
                let task = match core.lifo_slot.take() {
                    Some(task) => task,
                    None => return Ok(core),
                };

                // In ping-ping style workloads where task A notifies task B,
                // which notifies task A again, continuously prioritizing the
                // LIFO slot can cause starvation as these two tasks will
                // repeatedly schedule the other. To mitigate this, we limit the
                // number of times the LIFO slot is prioritized.
                if lifo_polls >= MAX_LIFO_POLLS_PER_TICK {
                    core.run_queue.push_back_or_overflow(
                        task,
                        self.worker.inject(),
                        &mut core.metrics,
                    );
                    return Ok(core);
                }

                lifo_polls += 1;

                if coop::has_budget_remaining() {
                    // Run the LIFO task, then loop
                    core.metrics.start_poll();
                    *self.core.borrow_mut() = Some(core);
                    let task = self.worker.handle.shared.owned.assert_owner(task);
                    task.run();
                } else {
                    // Not enough budget left to run the LIFO task, push it to
                    // the back of the queue and return.
                    core.run_queue.push_back_or_overflow(
                        task,
                        self.worker.inject(),
                        &mut core.metrics,
                    );
                    return Ok(core);
                }
            }
        })
    }

    fn maintenance(&self, mut core: Box<Core>) -> Box<Core> {
        if core.tick % self.worker.handle.shared.config.event_interval == 0 {
            super::counters::inc_num_maintenance();

            // Call `park` with a 0 timeout. This enables the I/O driver, timer, ...
            // to run without actually putting the thread to sleep.

Review comment (suggestion):
nit, TIOLI: it seems like we could combine  some of these branches, like
```suggestion
                // In ping-ping style workloads where task A notifies task B,
                // which notifies task A again, continuously prioritizing the
                // LIFO slot can cause starvation as these two tasks will
                // repeatedly schedule the other. To mitigate this, we limit the
                // number of times the LIFO slot is prioritized.
                let lifo_polls_remaining = lifo_polls >= MAX_LIFO_POLLS_PER_TICK;

                if lifo_polls_remaining && coop::has_budget_remaining() {
                    lifo_polls += 1;
```

Code after:
            task.run();
            let mut lifo_polls = 0;

            // As long as there is budget remaining and a task exists in the
            // `lifo_slot`, then keep running.
            loop {
                // Check if we still have the core. If not, the core was stolen
                // by another worker.
                let mut core = match self.core.borrow_mut().take() {
                    Some(core) => core,
                    None => {
                        // In this case, we cannot call `reset_lifo_enabled()`
                        // because the core was stolen. The stealer will handle
                        // that at the top of `Context::run`
                        return Err(());
                    }
                };

                // If task poll times is enabled, measure the poll time. Note
                // that, if the `core` is stolen, this means `block_in_place`
                // was called, turning the poll into a "blocking op". In this
                // case, we don't want to measure the poll time as it doesn't
                // really count as an async poll anymore.
                core.metrics.end_poll();

                // Check for a task in the LIFO slot
                let task = match core.lifo_slot.take() {
                    Some(task) => task,
                    None => {
                        self.reset_lifo_enabled(&mut core);
                        return Ok(core);
                    }
                };

                if !coop::has_budget_remaining() {
                    // Not enough budget left to run the LIFO task, push it to
                    // the back of the queue and return.
                    core.run_queue.push_back_or_overflow(
                        task,
                        self.worker.inject(),
                        &mut core.metrics,
                    );
                    // If we hit this point, the LIFO slot should be enabled.
                    // There is no need to reset it.
                    debug_assert!(core.lifo_enabled);
                    return Ok(core);
                }

                // Track that we are about to run a task from the LIFO slot.
                lifo_polls += 1;
                super::counters::inc_lifo_schedules();