Skip to content

swiftlang/swift-platform-executors

Swift Platform Executors

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

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.

βš™οΈ Use Cases

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.

🏁 Getting Started

Prerequisites

  • Swift version: 6.2+

Installation / Integration

Adding as a Dependency

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"
),

Usage

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.

Windows

Win32EventLoopExecutor

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).

Win32ThreadPoolExecutor

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.

πŸ“˜ Documentation

🧰 Release Info

This repository is released as a package.

  • Release Cadence: Whenever new significant changes have landed on main.

πŸ’‘ Contributing

We welcome contributions to Swift Platform Executors!

To get started, please read the Contributing Guide.

πŸ› οΈ Support

If you have any questions or need help, feel free to reach out by opening an issue or contacting the maintainers.

This repo is part of the Platform Steering Group.

πŸ” Security

If you discover a security vulnerability, please follow our security policy for responsible disclosure.

License

This project is licensed under the terms of the LICENSE

About

This package provides platform-native executors for Swift Concurrency.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published