Search code examples
rustrust-tokio

Does tokio::time::sleep method take task off the run queue?


I was looking to tokio source code to get the answer for the question and I was under the impression that the sleep method literally puts a timer with duration but I think i might have misunderstood the code because doing so would be highly inefficient. Would it be possible to get a clearer picture of this?


Solution

  • When you await the sleep method, two things happen:

    1. The new timer is inserted into a timer wheel that is part of the Tokio runtime.
    2. The task yields to the executor. This takes the task off the run queue.

    The Tokio runtime will, between polling tasks running on it, check both epoll for IO events and the timer wheel for timer events. Whenever such an event happens, the Tokio runtime emits a wake-up to the associated task, putting that task back on to the run queue.

    When no tasks are ready to run, Tokio will go to sleep by blocking on an epoll event. This sleep has a timeout equal to the smallest timer in the timer wheel allowing Tokio to wake that task up when the timer elapses.

    A timer wheel is a data structure that lets you access the smallest timer efficiently, similar to e.g. a binary heap, but it allows removal of timers if they are cancelled.