Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task: wake local tasks to the local queue when woken by the same thre…
…ad (#5095) Motivation Currently, when a task spawned on a `LocalSet` is woken by an I/O driver or time driver running on the same thread as the `LocalSet`, the task is pushed to the `LocalSet`'s locked remote run queue rather than to its unsynchronized local run queue. This is unfortunate, as it negates some of the performance benefits of having an unsynchronized local run queue. Instead, tasks are only woken to the local queue when they are woken by other tasks also running on the local set. This occurs because the local queue is only used when the `CONTEXT` thread-local contains a Context that's the same as the task's `Schedule` instance (an `Arc<Shared>`)'s Context. When the `LocalSet` is not being polled, the thread-local is unset, and the local run queue cannot be accessed by the `Schedule` implementation for `Arc<Shared>`. Solution This branch fixes this by moving the local run queue into Shared along with the remote run queue. When an `Arc<Shared>`'s Schedule impl wakes a task and the `CONTEXT` thread-local is None (indicating we are not currently polling the LocalSet on this thread), we now check if the current thread's `ThreadId` matches that of the thread the `LocalSet` was created on, and push the woken task to the local queue if it was. Moving the local run queue into `Shared` is somewhat unfortunate, as it means we now have a single field on the `Shared` type, which must not be accessed from other threads and must add an unsafe impl `Sync` for `Shared`. However, it's the only viable way to wake to the local queue from the Schedule impl for `Arc<Shared>`, so I figured it was worth the additional unsafe code. I added a debug assertion to check that the local queue is only accessed from the thread that owns the `LocalSet`.
- Loading branch information
Showing
1 changed file
with
172 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters