Skip to content

[Freestanding] Disable Task.sleep and AsyncStream. #60919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions stdlib/public/Concurrency/AsyncStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import Swift

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
/// An asynchronous sequence generated from a closure that calls a continuation
/// to produce new elements.
///
Expand Down Expand Up @@ -429,3 +430,113 @@ extension AsyncStream.Continuation {

@available(SwiftStdlib 5.1, *)
extension AsyncStream: @unchecked Sendable where Element: Sendable { }
#else
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public struct AsyncStream<Element> {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public struct Continuation: Sendable {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public enum Termination {
case finished
case cancelled
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public enum YieldResult {
case enqueued(remaining: Int)
case dropped(Element)
case terminated
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public enum BufferingPolicy {
case unbounded
case bufferingOldest(Int)
case bufferingNewest(Int)
}
@discardableResult
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func yield(_ value: __owned Element) -> YieldResult {
fatalError("Unavailable in task-to-thread concurrency model")
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func finish() {
fatalError("Unavailable in task-to-thread concurrency model")
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public var onTermination: (@Sendable (Termination) -> Void)? {
get {
fatalError("Unavailable in task-to-thread concurrency model")
}
nonmutating set {
fatalError("Unavailable in task-to-thread concurrency model")
}
}
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public init(
_ elementType: Element.Type = Element.self,
bufferingPolicy limit: Continuation.BufferingPolicy = .unbounded,
_ build: (Continuation) -> Void
) {
fatalError("Unavailable in task-to-thread concurrency model")
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public init(
unfolding produce: @escaping () async -> Element?,
onCancel: (@Sendable () -> Void)? = nil
) {
fatalError("Unavailable in task-to-thread concurrency model")
}
}

@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
extension AsyncStream {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public struct Iterator {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public mutating func next() async -> Element? {
fatalError("Unavailable in task-to-thread concurrency model")
}
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func makeAsyncIterator() -> Iterator {
fatalError("Unavailable in task-to-thread concurrency model")
}
}

@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
extension AsyncStream.Continuation {
@discardableResult
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func yield(
with result: Result<Element, Never>
) -> YieldResult {
fatalError("Unavailable in task-to-thread concurrency model")
}
@discardableResult
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func yield() -> YieldResult where Element == Void {
fatalError("Unavailable in task-to-thread concurrency model")
}
}

@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
extension AsyncStream: @unchecked Sendable where Element: Sendable { }
#endif
2 changes: 2 additions & 0 deletions stdlib/public/Concurrency/AsyncStreamBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import Swift

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
#if ASYNC_STREAM_STANDALONE
@_exported import _Concurrency
import Darwin
Expand Down Expand Up @@ -592,3 +593,4 @@ final class _AsyncStreamCriticalStorage<Contents>: @unchecked Sendable {
return storage
}
}
#endif
110 changes: 110 additions & 0 deletions stdlib/public/Concurrency/AsyncThrowingStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import Swift

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
/// An asynchronous sequence generated from an error-throwing closure that
/// calls a continuation to produce new elements.
///
Expand Down Expand Up @@ -474,3 +475,112 @@ extension AsyncThrowingStream.Continuation {

@available(SwiftStdlib 5.1, *)
extension AsyncThrowingStream: @unchecked Sendable where Element: Sendable { }
#else
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public struct AsyncThrowingStream<Element, Failure: Error> {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public struct Continuation: Sendable {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public enum Termination {
case finished(Failure?)
case cancelled
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public enum YieldResult {
case enqueued(remaining: Int)
case dropped(Element)
case terminated
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public enum BufferingPolicy {
case unbounded
case bufferingOldest(Int)
case bufferingNewest(Int)
}
@discardableResult
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func yield(_ value: __owned Element) -> YieldResult {
fatalError("Unavailable in task-to-thread concurrency model")
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func finish(throwing error: __owned Failure? = nil) {
fatalError("Unavailable in task-to-thread concurrency model")
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public var onTermination: (@Sendable (Termination) -> Void)? {
get {
fatalError("Unavailable in task-to-thread concurrency model")
}
nonmutating set {
fatalError("Unavailable in task-to-thread concurrency model")
}
}
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public init(
_ elementType: Element.Type = Element.self,
bufferingPolicy limit: Continuation.BufferingPolicy = .unbounded,
_ build: (Continuation) -> Void
) where Failure == Error {
fatalError("Unavailable in task-to-thread concurrency model")
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public init(
unfolding produce: @escaping () async throws -> Element?
) where Failure == Error {
fatalError("Unavailable in task-to-thread concurrency model")
}
}

@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
extension AsyncThrowingStream {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public struct Iterator {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public mutating func next() async throws -> Element? {
fatalError("Unavailable in task-to-thread concurrency model")
}
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func makeAsyncIterator() -> Iterator {
fatalError("Unavailable in task-to-thread concurrency model")
}
}

@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
extension AsyncThrowingStream.Continuation {
@discardableResult
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func yield(
with result: Result<Element, Failure>
) -> YieldResult where Failure == Error {
fatalError("Unavailable in task-to-thread concurrency model")
}
@discardableResult
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func yield() -> YieldResult where Element == Void {
fatalError("Unavailable in task-to-thread concurrency model")
}
}

@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
extension AsyncThrowingStream: @unchecked Sendable where Element: Sendable { }
#endif
2 changes: 2 additions & 0 deletions stdlib/public/Concurrency/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public protocol Clock<Duration>: Sendable {
var now: Instant { get }
var minimumResolution: Instant.Duration { get }

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws
#endif
}


Expand Down
10 changes: 10 additions & 0 deletions stdlib/public/Concurrency/ContinuousClock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ extension ContinuousClock: Clock {
.seconds(seconds) + .nanoseconds(nanoseconds))
}

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
/// Suspend task execution until a given deadline within a tolerance.
/// If no tolerance is specified then the system may adjust the deadline
/// to coalesce CPU wake-ups to more efficiently process the wake-ups in
Expand All @@ -93,6 +94,15 @@ extension ContinuousClock: Clock {
tolerance: tolerance,
clock: .continuous)
}
#else
@available(SwiftStdlib 5.7, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public func sleep(
until deadline: Instant, tolerance: Swift.Duration? = nil
) async throws {
fatalError("Unavailable in task-to-thread concurrency model")
}
#endif
}

@available(SwiftStdlib 5.7, *)
Expand Down
Loading