Skip to content

Custom main and global executors#2654

Open
al45tair wants to merge 16 commits intoswiftlang:mainfrom
al45tair:custom-executors
Open

Custom main and global executors#2654
al45tair wants to merge 16 commits intoswiftlang:mainfrom
al45tair:custom-executors

Conversation

@al45tair
Copy link
Copy Markdown
Contributor

Add write up of custom executor proposals. There are still some concerns surrounding integration with Dispatch that need working through.

Use `any TaskExecutor` instead of `any Executor` for `Task.defaultExecutor`.

Rename `ExecutorJobKind` to `ExecutorJob.Kind`.

Add `EventableExecutor`; replace `SerialRunLoopExecutor` with `MainExecutor`,
then make `MainActor` and `PlatformMainExecutor` use the new protocol.
The previous iteration would have required us to make calling the
allocation methods a fatal error, and would have meant executors had
to check explicitly for the `.task` job kind before using them.

Instead, if we add a new `LocalAllocator` type, we can have an
`allocator` property that is `nil` if the job doesn't support allocation,
and a valid `LocalAllocator` otherwise.
@rjmccall rjmccall added the LSG Contains topics under the domain of the Language Steering Group label Feb 24, 2025
Since the previous pitch, I've added `Clock`-based `enqueue` methods
and made a few other changes, notably to the way you actually set
custome executors for your program.
Remove `EventableExecutor`; we can come back to that later.

Update documentation for `currentExecutor` and add `preferredExecutor` and
`currentSchedulableExecutor`.

Move the clock-based enqueuing to a separate `SchedulableExecutor` protocol,
and provide an efficient way to get it.  This means we don't need the
`supportsScheduling` property.

Back `ClockTraits` with `UInt32`.
@al45tair al45tair marked this pull request as ready for review March 14, 2025 16:17
@al45tair al45tair changed the title Initial public draft of custom executors proposal. Custom main and global executors Mar 14, 2025
We should use typed throws, which will make this work better for Embedded
Swift.
/// 1. The custom executor associated with an `Actor` on which we are
/// currently running, or
/// 2. The preferred executor for the currently executing `Task`, or
/// 3. The task executor for the current thread
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be "for the current task" instead of "current thread"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's a practical difference there; the task executors are tracked using thread local data.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that true? Aren't they tracked using task local data?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread proposals/nnnn-custom-main-and-global-executors.md Outdated
al45tair added 2 commits June 24, 2025 14:59
Removed clock conversion functions; we're going to add `enqueue` and
`run` functions instead.

Updated comments for various items.

Mention that we're planning to expose the built-in executor implementations
by name.
al45tair added 8 commits July 2, 2025 15:08
Allow `DefaultExecutorFactory` to be defined in the `@main` struct
as well as at top level.

Remove some text from the proposal that is out of date.

Don't expose the Dispatch or CF executors, since that implies that we
will have a permanent dependency on Dispatch or Foundation.
We can have something similar in a separate package; let's not share
this implementation outside of the runtime for now (it has a number
of gotchas and we'd have to spend quite a bit of time in the
proposal explaining what it's for and what it does).
Added some text explaining the reason for adding some of the API
surface (some of this was already present).

Also added a section about the previous `Clock` interface with the
`traits` and `convert` functions.
* Added `ThreadDonationExecutor`.

* Re-worked the other executors protocols to use it.

* Added default implementations for `ExecutorFactory`'s properties.

* Removed extraneous `Optional`s in the task allocator.

* Updated various bits of wording.
Added some text about alternatives to `currentExecutor`.  Also
credit John McCall as well, as he's provided substantial input on
the proposal at this point.
We've decided to make these non-public.
The delayed enqueuing support will be moved to another proposal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LSG Contains topics under the domain of the Language Steering Group

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants