Skip to content

Instantly share code, notes, and snippets.

@phausler
Created September 28, 2022 22:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phausler/0eb601434ae0223fedb1c39bb9d34b96 to your computer and use it in GitHub Desktop.
Save phausler/0eb601434ae0223fedb1c39bb9d34b96 to your computer and use it in GitHub Desktop.
import os
public struct Gate<T: Sendable>: Sendable {
struct Pass: Sendable {
var value: T
var continuaton: UnsafeContinuation<Void, Never>
}
struct Pending: Sendable {
var continuation: UnsafeContinuation<T, Never>
}
struct Resumption {
let pass: Pass
let pending: Pending
func resume() {
pending.continuation.resume(returning: pass.value)
pass.continuaton.resume()
}
}
struct State: Sendable {
var passing = [Pass]()
var pending = [Pending]()
mutating func drain() -> Resumption? {
if !passing.isEmpty && !pending.isEmpty {
return Resumption(pass: passing.removeFirst(), pending: pending.removeFirst())
}
return nil
}
}
let state = OSAllocatedUnfairLock(initialState: State())
public func pass(_ value: T) async {
return await withUnsafeContinuation { continuation in
state.withLock { state in
state.passing.append(Pass(value: value, continuaton: continuation))
return state.drain()
}?.resume()
}
}
public var value: T {
get async {
await withUnsafeContinuation { continuation in
state.withLock { state in
state.pending.append(Pending(continuation: continuation))
return state.drain()
}?.resume()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment