Created
September 28, 2022 22:13
-
-
Save phausler/0eb601434ae0223fedb1c39bb9d34b96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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