Warning
This package is currently still under active development.
This package provides platform-native executors for Swift Concurrency.
π Swift package for Swift Concurrency executors π¦ Compatible with Swift Package Manager π± Supports Linux, Windows, iOS, macOS, watchOS, tvOS, and visionOS π§ Built with Swift 6.2+, Xcode 26+
π Jump to:
- π Overview
- βοΈ Use Cases
- π Glossary
- π Getting Started
- π Documentation
- π§° Release Info
- π‘ Contributing
- π οΈ Support
- π Security
- π License
This package provides high-performance platform-native executors for Swift Concurrency that do not rely on Dispatch or Foundation to provide the job scheduling system.
The executors provided by this package are intended to be used as the default executors on their respective platform. Furthermore, individual instances of these executors can be created to provide custom actor or task executors.
- Swift version: 6.2+
To use Swift Platform Executors in your Swift project, add it as a dependency in
your Package.swift
file:
.package(
url: "https://github.com/swiftlang/swift-platform-executors",
from: "0.0.1"
),
Once the relevant Swift Evolution proposal lands, it will be possible to replace the default main and global executors with executors from this package.
To use the provided executors as the default executors in your application, add the following type alias to the file including your appliation's main entry point.
import PlatformExecutors
typealias DefaultExecutorFactory = PlatformExecutorFactory
Be aware that if you take advantage of this option, the Dispatch main
queue will not be processed, so anything that relies explicitly on
Dispatch.main
will not work.
Win32EventLoopExecutor
is a
SerialExecutor
and may be used as a custom actor executor,
ala
SE-0392:
import Win32NativeExecutors
actor Win32Actor {
nonisolated let executor = Win32EventLoopExecutor()
nonisolated var unownedExecutor: UnownedSerialExecutor {
self.executor.asUnownedSerialExecutor()
}
func greet() {
print("Hello from a Win32 event loop!")
try? await Task.sleep(for: .seconds(3))
}
}
func test() {
let myActor = Win32Actor()
let t = Task.detached {
await myActor.greet()
myActor.executor.stop()
}
myActor.executor.run()
}
This will also work for global actors, e.g.
import Win32NativeExecutors
@globalActor
actor MessageLoopActor {
let executor = Win32EventLoopExecutor()
nonisolated var unownedExecutor: UnownedSerialExecutor {
self.executor.asUnownedSerialExecutor()
}
}
@MessageLoopActor
func hello() {
print("Hello from a Win32 event loop!")
try? await Task.sleep(for: .seconds(3))
}
func test() {
let t = Task.detached {
await hello()
myActor.executor.stop()
}
myActor.executor.run()
}
Note that you will need to call the run()
method on the
Win32EventLoopExecutor
from some thread to actually service the
message loop. This will return on receipt of WM_QUIT
, or if
something calls the stop()
method (the latter is thread-safe and can
be done asynchronously; the message loop will stop when it is next
safe to do so).
The Win32ThreadPoolExecutor
is a
TaskExecutor
built on the Win32 Thread Pool
API
and can be used with the
withTaskExecutorPreference(_:operation:)
,
Task(executorPreference:)
or group.addTask(executorPreference:)
families of APIs:
import Win32NativeExecutors
let threadPool = Win32ThreadPoolExecutor()
func test() {
Task {
await withTaskExecutorPreference(threadPool) {
print("I am running in the default Win32 thread pool")
}
}
}
If you have a custom Win32 thread pool that you wish to use instead,
you can use the Win32ThreadPoolExecutor(pool: PTP_POOL?)
API to
construct an executor that will target that thread pool specifically.
Passing nil
to that API will use the default pool.
- API Documentation - Complete API reference and guides
This repository is released as a package.
- Release Cadence: Whenever new significant changes have landed on
main
.
We welcome contributions to Swift Platform Executors!
To get started, please read the Contributing Guide.
If you have any questions or need help, feel free to reach out by opening an issue or contacting the maintainers.
- GitHub Issues - Bug reports and feature requests
- CODEOWNERS
This repo is part of the Platform Steering Group.
If you discover a security vulnerability, please follow our security policy for responsible disclosure.
This project is licensed under the terms of the LICENSE