[Pitch 3] Custom Main and Global Executors

This is a third pitch for the ability to write custom default executors (both the main executor, as used for the main actor, and the global executor, used by default for Swift Task s). You can find the first pitch and the second pitch here.

The draft Custom Main and Global Executors PR has been updated with the following changes following feedback from the second pitch:

  • Clocks no longer support Instant or Duration conversion.
  • Instead, Clock has grown run(_ job:at instant:tolerance:clock:) and enqueue(_ job:on executor:at instant:tolerance:) methods.
    • These allow an executor that finds itself asked to schedule a job using a Clock it doesn't recognise to hand the problem off to that Clock, the idea being that the Clock will know how to do the right thing.
  • Expose the built-in executor implementations.
  • Update the text surrounding the task allocator and executor private data to indicate that these facilities are only for use until the point at which a Job is executed. Executors much release any resources prior to that point, and in the case of the task allocator, must do so in strict reverse order of allocation.
8 Likes

Thanks for the update @al45tair!

This looks really good, I think especially the way this redesigns the enqueues is very promising and I think this can work well enough -- even with clocks one would not recognize, we can then forward to the clock's enqueue etc.

The exposed executor types also sounds good to me. And we're finally giving a real swift type to the previously-known-as "the default global concurrent executor (which happens to be dispatch based)" with the DispatchGlobalTaskExecutor type :slight_smile:

AFAICS this addresses all previous discussion points quite nicely. Thanks for the docs updates as well on the allocator, they read nice now. We could maybe make it even more scary but I don't necessarily want to promise how we'll crash if you do something wrong there...

Let's see if anyone else has some points here but otherwise I think we could get this up for an official review soon enough :slight_smile: