Skip to content

[WIP] Broadcast algorithm #214

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
246 changes: 246 additions & 0 deletions Sources/AsyncAlgorithms/AsyncBroadcastSequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
//===----------------------------------------------------------------------===//
//
// 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 DequeModule

extension AsyncSequence where Self: Sendable, Element: Sendable {
public func broadcast() -> AsyncBroadcastSequence<Self> {
AsyncBroadcastSequence(self)
}
}

public struct AsyncBroadcastSequence<Base: AsyncSequence>: Sendable where Base: Sendable, Base.Element: Sendable {
struct State : Sendable {
enum Terminal {
case failure(Error)
case finished
}

struct Side {
var buffer = Deque<Element>()
var terminal: Terminal?
var continuation: UnsafeContinuation<Result<Element?, Error>, Never>?

mutating func drain() {
if !buffer.isEmpty, let continuation {
let element = buffer.removeFirst()
continuation.resume(returning: .success(element))
self.continuation = nil
} else if let terminal, let continuation {
switch terminal {
case .failure(let error):
self.terminal = .finished
continuation.resume(returning: .failure(error))
case .finished:
continuation.resume(returning: .success(nil))
}
self.continuation = nil
}
}

mutating func cancel() {
buffer.removeAll()
terminal = .finished
drain()
}

mutating func next(_ continuation: UnsafeContinuation<Result<Element?, Error>, Never>) {
assert(self.continuation == nil) // presume that the sides are NOT sendable iterators...
self.continuation = continuation
drain()
}

mutating func emit(_ result: Result<Element?, Error>) {
switch result {
case .success(let element):
if let element {
buffer.append(element)
} else {
terminal = .finished
}
case .failure(let error):
terminal = .failure(error)
}
drain()
}
}

var id = 0
var sides = [Int: Side]()

init() { }

mutating func establish() -> Int {
defer { id += 1 }
sides[id] = Side()
return id
}

static func establish(_ state: ManagedCriticalState<State>) -> Int {
state.withCriticalRegion { $0.establish() }
}

mutating func cancel(_ id: Int) {
if var side = sides.removeValue(forKey: id) {
side.cancel()
}
}

static func cancel(_ state: ManagedCriticalState<State>, id: Int) {
state.withCriticalRegion { $0.cancel(id) }
}

mutating func next(_ id: Int, continuation: UnsafeContinuation<Result<Element?, Error>, Never>) {
sides[id]?.next(continuation)
}

static func next(_ state: ManagedCriticalState<State>, id: Int) async -> Result<Element?, Error> {
await withUnsafeContinuation { continuation in
state.withCriticalRegion { $0.next(id, continuation: continuation) }
}
}

mutating func emit(_ result: Result<Element?, Error>) {
for id in sides.keys {
sides[id]?.emit(result)
}
}

static func emit(_ state: ManagedCriticalState<State>, result: Result<Element?, Error>) {
state.withCriticalRegion { $0.emit(result) }
}
}

struct Iteration {
enum Status {
case initial(Base)
case iterating(Task<Void, Never>)
case terminal
}

var status: Status

init(_ base: Base) {
status = .initial(base)
}

static func task(_ state: ManagedCriticalState<State>, base: Base) -> Task<Void, Never> {
Task {
do {
for try await element in base {
State.emit(state, result: .success(element))
}
State.emit(state, result: .success(nil))
} catch {
State.emit(state, result: .failure(error))
}
}
}
Comment on lines +135 to +146
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I just noticed after the discussion in the forums around deferred. This is actually creating multiple upstream iterators if I understand this code correctly. IMO we really need to avoid this otherwise this algorithm is not capable of transforming a unicast AsyncSequence into a broadcasted one. @phausler please correct if I am misunderstanding the code here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is creating 1 singular task to iterate so only 1 upstream iterator is made.

Copy link

@jdberry jdberry Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm clearly being dense, because it also looks to me like this would create a new Task, at least each time broadcast is called. And for each Task there would be a separate iterator of the base sequence.

Ah, so maybe the intent is that broadcast is only called once, and then the broadcast sequence is copied to duplicate it? That's a bit awkward.


mutating func start(_ state: ManagedCriticalState<State>) -> Bool {
switch status {
case .terminal:
return false
case .initial(let base):
status = .iterating(Iteration.task(state, base: base))
default:
break
}
return true
}

mutating func cancel() {
switch status {
case .iterating(let task):
task.cancel()
default:
break
}
status = .terminal
}

static func start(_ iteration: ManagedCriticalState<Iteration>, state: ManagedCriticalState<State>) -> Bool {
iteration.withCriticalRegion { $0.start(state) }
}

static func cancel(_ iteration: ManagedCriticalState<Iteration>) {
iteration.withCriticalRegion { $0.cancel() }
}
}

let state: ManagedCriticalState<State>
let iteration: ManagedCriticalState<Iteration>

init(_ base: Base) {
state = ManagedCriticalState(State())
iteration = ManagedCriticalState(Iteration(base))
}
}


extension AsyncBroadcastSequence: AsyncSequence {
public typealias Element = Base.Element

public struct Iterator: AsyncIteratorProtocol {
final class Context {
let state: ManagedCriticalState<State>
var iteration: ManagedCriticalState<Iteration>
let id: Int

init(_ state: ManagedCriticalState<State>, _ iteration: ManagedCriticalState<Iteration>) {
self.state = state
self.iteration = iteration
self.id = State.establish(state)
}

deinit {
State.cancel(state, id: id)
if iteration.isKnownUniquelyReferenced() {
Iteration.cancel(iteration)
}
Comment on lines +206 to +208

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something here, but if the last two iterators enter deinit at the same time (and both perform the isKnownUniquelyReferenced check before releasing iteration), could that trigger a race condition such that neither ends up calling Iteration.cancel?

}

func next() async rethrows -> Element? {
guard Iteration.start(iteration, state: state) else {
return nil
}
defer {
if Task.isCancelled && iteration.isKnownUniquelyReferenced() {
Iteration.cancel(iteration)
}
}
return try await withTaskCancellationHandler {
let result = await State.next(state, id: id)
return try result._rethrowGet()
} onCancel: { [state, id] in
State.cancel(state, id: id)
}
}
}

let context: Context

init(_ state: ManagedCriticalState<State>, _ iteration: ManagedCriticalState<Iteration>) {
context = Context(state, iteration)
}

public mutating func next() async rethrows -> Element? {
try await context.next()
}
}

public func makeAsyncIterator() -> Iterator {
Iterator(state, iteration)
}
}

@available(*, unavailable)
extension AsyncBroadcastSequence.Iterator: Sendable { }
6 changes: 5 additions & 1 deletion Sources/AsyncAlgorithms/Locking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct ManagedCriticalState<State> {
}
}

private let buffer: ManagedBuffer<State, Lock.Primitive>
private var buffer: ManagedBuffer<State, Lock.Primitive>

init(_ initial: State) {
buffer = LockedBuffer.create(minimumCapacity: 1) { buffer in
Expand All @@ -133,6 +133,10 @@ struct ManagedCriticalState<State> {
return try critical(&header.pointee)
}
}

mutating func isKnownUniquelyReferenced() -> Bool {
Swift.isKnownUniquelyReferenced(&buffer)
}
}

extension ManagedCriticalState: @unchecked Sendable where State: Sendable { }
56 changes: 56 additions & 0 deletions Tests/AsyncAlgorithmsTests/TestBroadcast.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

@preconcurrency import XCTest
import AsyncAlgorithms

final class TestBroadcast: XCTestCase {
func test_basic_broadcasting() async {
let base = [1, 2, 3, 4].async
let a = base.broadcast()
let b = a
let results = await withTaskGroup(of: [Int].self) { group in
group.addTask {
await Array(a)
}
group.addTask {
await Array(b)
}
return await Array(group)
}
XCTAssertEqual(results[0], results[1])
}

func test_basic_broadcasting_from_channel() async {
let base = AsyncChannel<Int>()
let a = base.broadcast()
let b = a
let results = await withTaskGroup(of: [Int].self) { group in
group.addTask {
var sent = [Int]()
for i in 0..<10 {
sent.append(i)
await base.send(i)
}
base.finish()
return sent
}
group.addTask {
await Array(a)
}
group.addTask {
await Array(b)
}
return await Array(group)
}
XCTAssertEqual(results[0], results[1])
}
}