Files
swift-async-algorithms/Sources/AsyncSequenceValidation/TaskDriver.swift
T
George Barnett 042e1c4d9d Move platform requirements to availability annotations (#348)
* Move platform requirements to availability annotations

Adding or raising the deployment platforms in the package manifest is a
SemVer major breaking change as consumers must also add or raise their
deployment platforms. This is a known limitation of SwiftPM.

Unforunately this means that it's very difficult for non-leaf packages
to adopt packages which declare their platforms in the manifest. Doing
so puts the brakes on adoption and ecosystem growth. For 'core' packages
like this one availability constraints should be expressed on
declarations rather than in the manifest.

This patch adds equivalent availability annotations to declarations
across the package and removes platforms from the package manifest.

* Address naming feedback
2025-04-22 08:07:37 +01:00

107 lines
3.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import _CAsyncSequenceValidationSupport
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#else
#error("Unsupported platform")
#endif
#if canImport(Darwin)
@available(AsyncAlgorithms 1.0, *)
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return nil
}
#elseif (canImport(Glibc) && !os(Android)) || canImport(Musl)
@available(AsyncAlgorithms 1.0, *)
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
return nil
}
#elseif os(Android)
@available(AsyncAlgorithms 1.0, *)
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return UnsafeMutableRawPointer(bitPattern: 0xdeadbee)!
}
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#endif
@available(AsyncAlgorithms 1.0, *)
final class TaskDriver {
let work: (TaskDriver) -> Void
let queue: WorkQueue
#if canImport(Darwin)
var thread: pthread_t?
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
var thread = pthread_t()
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#endif
init(queue: WorkQueue, _ work: @escaping (TaskDriver) -> Void) {
self.queue = queue
self.work = work
}
func start() {
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
pthread_create(
&thread,
nil,
start_thread,
Unmanaged.passRetained(self).toOpaque()
)
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#endif
}
func run() {
#if canImport(Darwin)
pthread_setname_np("Validation Diagram Clock Driver")
#endif
work(self)
}
func join() {
#if canImport(Darwin)
pthread_join(thread!, nil)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
pthread_join(thread, nil)
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#endif
}
func enqueue(_ job: JobRef) {
let job = Job(job)
queue.enqueue(AsyncSequenceValidationDiagram.Context.currentJob) {
let previous = AsyncSequenceValidationDiagram.Context.currentJob
AsyncSequenceValidationDiagram.Context.currentJob = job
job.execute()
AsyncSequenceValidationDiagram.Context.currentJob = previous
}
}
}