-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(maitake): add utilities for work-stealing #322
Merged
Merged
Conversation
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
hawkw
force-pushed
the
eliza/worksteal
branch
2 times, most recently
from
September 28, 2022 18:10
3471e1e
to
90d9851
Compare
if they are stealable
this fixes an issue where the queued task count wouldn't be incremented when a task wakes itself.
hawkw
force-pushed
the
eliza/worksteal
branch
from
September 29, 2022 19:31
9902585
to
86b95b5
Compare
By using an exhaustive destructuring in manual `fmt::Debug` impls, we get a compiler error if an additional field is added to a type with a manual `Debug` impl which is not used in the `Debug` impl. This is nice, since it helps guard against accidentally adding a field without formatting it. I've applied this refactoring to *most* manual `Debug` impls, with the exception of a few that do something more weird than just manually printing all the `Debug`-able field values. This caught at least one case where I had forgotten to add a field to a `Debug` impl :) Thanks to Twitter User @cratelyn for this idea <3
hawkw
changed the title
wip: feat(maitake): add utilities for work-stealing
feat(maitake): add utilities for work-stealing
Oct 8, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This branch adds support for implementing work-stealing systems across
multiple schedulers to the
maitake::scheduler
module. In particular,it adds the following new APIs:
scheduler::Injector
type, representing a shared injector queuethat can be used to spawn tasks to be executed by any of a set of
worker
Scheduler
s,scheduler::Stealer
type, which wraps acordyceps::mpsc_queue::Consumer
and steals tasks from anInjector
queue or
Scheduler
/StaticScheduler
's run queue,Scheduler::try_steal
andStaticScheduler::try_steal
methods, whichreturn a
Result<Stealer, TryStealError>
, for attempting to stealtasks from a given scheduler's queue
I've also updated the
mycelium::rt
module to use these new APIs toimplement a global spawn with work-stealing across multiple cores.
Implementation Notes
In order to implement work-stealing, it was necessary to modify the
Task
type to allow thescheduler
field stored by the task to bechanged after the task is created. This may occur when a task is stolen
from one scheduler by another, or when a task is spawned on a shared
Injector
queue and not yet bound to a scheduler.This required changing the
scheduler
field to anUnsafeCell
.Currently, access to the
UnsafeCell
is controlled by the task's statebits, and the current state system is sufficient for ensuring a task is
not bound to a new scheduler while it's queued.
Tasks can only be re-bound to the same type of scheduler, since the
size of the stored scheduler field must be the same when rebinding.
Currently, this is checked by a debug assertion in the
Task
code, andthe work-stealing APIs use
PhantomData
to prevent tasks from beingstolen from a scheduler of one type and spawned by a scheduler of a
different type.
Future Work
Some things that this branch doesn't do, but I'd like to get to in the
future:
maitake
for setting a globalInjector
queue to use with an ambiently-available
task::spawn
function?Schedule
reference so thatthe
UnsafeCell
can be replaced with anAtomicPtr
. This wouldprobably require one of the following:
Schedule
trait to haveclone_scheduler(*const ())
and
drop_scheduler(*const ())
or similar, to hook into ref countingfor the
Arc
scheduler,Schedule
instances with another custom vtable. Thatcould potentially permit stealing tasks from a scheduler of one
type onto a scheduler of a different type, if the vtable could also
be changed (requiring a vtable pointer...)
Injector
queue work with thetask::Builder
API.Currently, tasks configured with a builder must be spawned on a real
Scheduler
, and cannot be spawned on anInjector
. It would be niceto fix that.
this yet because I'm lazy and want to get this PR merged sooner...