Files
swift-mirror/stdlib/public/Concurrency/AsyncStreamBuffer.swift
Philippe Hausler 4886bd56e4 [Concurrency] AsyncStream and AsyncThrowingStream
* Rework YieldingContinuation to service values in a buffered fashion

* Fix word size calculation for locks

* Handle terminal states and finished/failed storage

* Wrap yielding continuation into a more featureful type for better ergonomics

* Hope springs eternal, maybe windows works with this?

* Prevent value overflows at .max limits

* Add a cancellation handler

* Fix series tests missing continuation parameters

* Fix series tests for mutable itertaors

* Rename to a more general name for Series's inner continuation type

* Whitespace fixes and add more commentary about public functions on Series

* Restore YieldingContinuation for now with deprecations to favor Series

* Ensure onCancel is invoked in deinit phases, and eliminate a potential for double cancellation

* Make sure ThrowingSeries has the same nonmutating setter for onCancel as Series

* Add a swath of more unit tests that exersize cancellation behavior as well as throwing behaviors

* Remove work-around for async testing

* Fixup do/catch range to properly handle ThrowingSeries test

* Address naming consistency of resume result function

* Adopt the async main test setup

* More migration of tests to new async mechanisms

* Handle the double finish/throw case

* Ensure the dependency on Dispatch is built for the series tests (due to semaphore usage)

* Add import-libdispatch to run command for Series tests

* Use non-combine based timeout intervals (portable to linux) for dispatch semaphore

* Rename Series -> AsyncStream and resume functions to just yield, and correct a missing default Element.self value

* Fix missing naming change issue for yielding an error on AsyncThrowingStream

* Remove argument label of buffering from tests

* Extract buffer and throwing variants into their own file

* Slightly refactor for only needing to store the producer instead of producer and cancel

* Rename onCancel to onTermination

* Convert handler access into a function pair

* Add finished states to the termination handler event pipeline and a disambiguation enum to identify finish versus cancel

* Ensure all termination happens before event propigation (and outside of the locks) and warn against requirements for locking on terminate and enqueue

* Modified to use Deque to back the storage and move the storage to inner types; overall perf went from 200kE/sec to over 1ME/sec

* Update stdlib/public/Concurrency/AsyncStream.swift

Co-authored-by: Doug Gregor <dgregor@apple.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Doug Gregor <dgregor@apple.com>

* Update stdlib/public/Concurrency/AsyncStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Remove local cruft for overlay disabling

* Remove local cruft for Dispatch overlay work

* Remove potential ABI impact for adding Deque

Co-authored-by: Doug Gregor <dgregor@apple.com>
Co-authored-by: Joseph Heck <heckj@mac.com>
2021-05-11 21:41:33 -07:00

406 lines
11 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020-2021 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Swift
@_silgen_name("_swift_async_stream_lock_size")
func _lockWordCount() -> Int
@_silgen_name("_swift_async_stream_lock_init")
func _lockInit(_ ptr: UnsafeRawPointer)
@_silgen_name("_swift_async_stream_lock_lock")
func _lock(_ ptr: UnsafeRawPointer)
@_silgen_name("_swift_async_stream_lock_unlock")
func _unlock(_ ptr: UnsafeRawPointer)
@available(SwiftStdlib 5.5, *)
extension AsyncStream {
internal final class _Storage: UnsafeSendable {
typealias TerminationHandler = @Sendable (Continuation.Termination) -> Void
struct State {
var continuation: UnsafeContinuation<Element?, Never>?
var pending = _Deque<Element>()
let limit: Int
var onTermination: TerminationHandler?
var terminal: Bool = false
init(limit: Int) {
self.limit = limit
}
}
// Stored as a singular structured assignment for initialization
var state: State
private init(_doNotCallMe: ()) {
fatalError("Storage must be initialized by create")
}
deinit {
state.onTermination?(.cancelled)
}
private func lock() {
let ptr =
UnsafeRawPointer(Builtin.projectTailElems(self, UnsafeRawPointer.self))
_lock(ptr)
}
private func unlock() {
let ptr =
UnsafeRawPointer(Builtin.projectTailElems(self, UnsafeRawPointer.self))
_unlock(ptr)
}
func getOnTermination() -> TerminationHandler? {
lock()
let handler = state.onTermination
unlock()
return handler
}
func setOnTermination(_ newValue: TerminationHandler?) {
lock()
withExtendedLifetime(state.onTermination) {
state.onTermination = newValue
unlock()
}
}
func cancel() {
lock()
// swap out the handler before we invoke it to prevent double cancel
let handler = state.onTermination
state.onTermination = nil
unlock()
handler?(.cancelled) // handler must be invoked before yielding nil for termination
finish()
}
func yield(_ value: __owned Element) {
lock()
let limit = state.limit
if let continuation = state.continuation {
let count = state.pending.count
if count > 0 {
if !state.terminal && count < limit {
state.pending.append(value)
}
state.continuation = nil
let toSend = state.pending.removeFirst()
unlock()
continuation.resume(returning: toSend)
} else if state.terminal {
state.continuation = nil
unlock()
continuation.resume(returning: nil)
} else {
state.continuation = nil
unlock()
continuation.resume(returning: value)
}
} else {
if !state.terminal && ((limit == .max) || (state.pending.count < limit)) {
state.pending.append(value)
}
unlock()
}
}
func finish() {
lock()
let handler = state.onTermination
state.onTermination = nil
state.terminal = true
if let continuation = state.continuation {
if state.pending.count > 0 {
state.continuation = nil
let toSend = state.pending.removeFirst()
unlock()
handler?(.finished)
continuation.resume(returning: toSend)
} else if state.terminal {
state.continuation = nil
unlock()
handler?(.finished)
continuation.resume(returning: nil)
} else {
unlock()
handler?(.finished)
}
} else {
if state.limit == 0 {
state.pending.removeFirst()
}
unlock()
handler?(.finished)
}
}
func next(_ continuation: UnsafeContinuation<Element?, Never>) {
lock()
if state.continuation == nil {
if state.pending.count > 0 {
let toSend = state.pending.removeFirst()
unlock()
continuation.resume(returning: toSend)
} else if state.terminal {
unlock()
continuation.resume(returning: nil)
} else {
state.continuation = continuation
unlock()
}
} else {
unlock()
fatalError("attempt to await next() on more than one task")
}
}
func next() async -> Element? {
await withTaskCancellationHandler { [cancel] in
cancel()
} operation: {
await withUnsafeContinuation {
next($0)
}
}
}
static func create(limit: Int) -> _Storage {
let minimumCapacity = _lockWordCount()
let storage = Builtin.allocWithTailElems_1(
_Storage.self,
minimumCapacity._builtinWordValue,
UnsafeRawPointer.self
)
let state =
UnsafeMutablePointer<State>(Builtin.addressof(&storage.state))
state.initialize(to: State(limit: limit))
let ptr = UnsafeRawPointer(
Builtin.projectTailElems(storage, UnsafeRawPointer.self))
_lockInit(ptr)
return storage
}
}
}
@available(SwiftStdlib 5.5, *)
extension AsyncThrowingStream {
internal final class _Storage: UnsafeSendable {
typealias TerminationHandler = @Sendable (Continuation.Termination) -> Void
enum Terminal {
case finished
case failed(Error)
}
struct State {
var continuation: UnsafeContinuation<Element?, Error>?
var pending = _Deque<Element>()
let limit: Int
var onTermination: TerminationHandler?
var terminal: Terminal?
init(limit: Int) {
self.limit = limit
}
}
// Stored as a singular structured assignment for initialization
var state: State
private init(_doNotCallMe: ()) {
fatalError("Storage must be initialized by create")
}
deinit {
state.onTermination?(.cancelled)
}
private func lock() {
let ptr =
UnsafeRawPointer(Builtin.projectTailElems(self, UnsafeRawPointer.self))
_lock(ptr)
}
private func unlock() {
let ptr =
UnsafeRawPointer(Builtin.projectTailElems(self, UnsafeRawPointer.self))
_unlock(ptr)
}
func getOnTermination() -> TerminationHandler? {
lock()
let handler = state.onTermination
unlock()
return handler
}
func setOnTermination(_ newValue: TerminationHandler?) {
lock()
withExtendedLifetime(state.onTermination) {
state.onTermination = newValue
unlock()
}
}
func cancel() {
lock()
// swap out the handler before we invoke it to prevent double cancel
let handler = state.onTermination
state.onTermination = nil
unlock()
handler?(.cancelled) // handler must be invoked before yielding nil for termination
finish()
}
func yield(_ value: __owned Element) {
lock()
let limit = state.limit
if let continuation = state.continuation {
let count = state.pending.count
if count > 0 {
if state.terminal == nil && count < limit {
state.pending.append(value)
}
state.continuation = nil
let toSend = state.pending.removeFirst()
unlock()
continuation.resume(returning: toSend)
} else if let terminal = state.terminal {
state.continuation = nil
state.terminal = .finished
unlock()
switch terminal {
case .finished:
continuation.resume(returning: nil)
case .failed(let error):
continuation.resume(throwing: error)
}
} else {
state.continuation = nil
unlock()
continuation.resume(returning: value)
}
} else {
if state.terminal == nil && ((limit == .max) || (state.pending.count < limit)) {
state.pending.append(value)
}
unlock()
}
}
func finish(throwing error: __owned Error? = nil) {
lock()
let handler = state.onTermination
state.onTermination = nil
if state.terminal == nil {
if let failure = error {
state.terminal = .failed(failure)
} else {
state.terminal = .finished
}
}
if let continuation = state.continuation {
if state.pending.count > 0 {
state.continuation = nil
let toSend = state.pending.removeFirst()
unlock()
handler?(.finished(error))
continuation.resume(returning: toSend)
} else if let terminal = state.terminal {
state.continuation = nil
unlock()
handler?(.finished(error))
switch terminal {
case .finished:
continuation.resume(returning: nil)
case .failed(let error):
continuation.resume(throwing: error)
}
} else {
unlock()
handler?(.finished(error))
}
} else {
if state.limit == 0 {
state.pending.removeFirst()
}
unlock()
handler?(.finished(error))
}
}
func next(_ continuation: UnsafeContinuation<Element?, Error>) {
lock()
if state.continuation == nil {
if state.pending.count > 0 {
let toSend = state.pending.removeFirst()
unlock()
continuation.resume(returning: toSend)
} else if let terminal = state.terminal {
state.terminal = .finished
unlock()
switch terminal {
case .finished:
continuation.resume(returning: nil)
case .failed(let error):
continuation.resume(throwing: error)
}
} else {
state.continuation = continuation
unlock()
}
} else {
unlock()
fatalError("attempt to await next() on more than one task")
}
}
func next() async throws -> Element? {
try await withTaskCancellationHandler { [cancel] in
cancel()
} operation: {
try await withUnsafeThrowingContinuation {
next($0)
}
}
}
static func create(limit: Int) -> _Storage {
let minimumCapacity = _lockWordCount()
let storage = Builtin.allocWithTailElems_1(
_Storage.self,
minimumCapacity._builtinWordValue,
UnsafeRawPointer.self
)
let state =
UnsafeMutablePointer<State>(Builtin.addressof(&storage.state))
state.initialize(to: State(limit: limit))
let ptr = UnsafeRawPointer(
Builtin.projectTailElems(storage, UnsafeRawPointer.self))
_lockInit(ptr)
return storage
}
}
}