Skip to content

Commit

Permalink
Lock async stream inits (#11)
Browse files Browse the repository at this point in the history
* Lock async stream inits

Fixes #10.

* wip
  • Loading branch information
stephencelis committed Aug 5, 2024
1 parent 479750b commit a46265b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Sources/ConcurrencyExtras/AsyncStream.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Foundation

extension AsyncStream {
/// Produces an `AsyncStream` from an `AsyncSequence` by consuming the sequence till it
/// terminates, ignoring any failure.
Expand Down Expand Up @@ -51,10 +53,13 @@ extension AsyncStream {
///
/// - Parameter sequence: An async sequence.
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element {
let lock = NSLock()
var iterator: S.AsyncIterator?
self.init {
if iterator == nil {
iterator = sequence.makeAsyncIterator()
lock.withLock {
if iterator == nil {
iterator = sequence.makeAsyncIterator()
}
}
return try? await iterator?.next()
}
Expand Down
9 changes: 7 additions & 2 deletions Sources/ConcurrencyExtras/AsyncThrowingStream.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import Foundation

extension AsyncThrowingStream where Failure == Error {
/// Produces an `AsyncThrowingStream` from an `AsyncSequence` by consuming the sequence till it
/// terminates, rethrowing any failure.
///
/// - Parameter sequence: An async sequence.
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element {
let lock = NSLock()
var iterator: S.AsyncIterator?
self.init {
if iterator == nil {
iterator = sequence.makeAsyncIterator()
lock.withLock {
if iterator == nil {
iterator = sequence.makeAsyncIterator()
}
}
return try await iterator?.next()
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/ConcurrencyExtras/Internal/Locking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

#if !(os(iOS) || os(macOS) || os(tvOS) || os(watchOS))
extension NSLock {
func withLock<R>(_ body: () throws -> R) rethrows -> R {
self.lock()
defer { self.unlock() }
return try body()
}
}
#endif

0 comments on commit a46265b

Please sign in to comment.