-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Enable Sendability for AsyncStream and AsyncThrowingStream #41713
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
Merged
phausler
merged 2 commits into
swiftlang:main
from
phausler:phausler/asyncstream_sendability
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ import Swift | |
import Darwin | ||
|
||
func _lockWordCount() -> Int { | ||
let sz = | ||
let sz = | ||
MemoryLayout<os_unfair_lock>.size / MemoryLayout<UnsafeRawPointer>.size | ||
return max(sz, 1) | ||
} | ||
|
@@ -57,7 +57,7 @@ extension AsyncStream { | |
typealias TerminationHandler = @Sendable (Continuation.Termination) -> Void | ||
|
||
struct State { | ||
var continuation: UnsafeContinuation<Element?, Never>? | ||
var continuations = [UnsafeContinuation<Element?, Never>]() | ||
var pending = _Deque<Element>() | ||
let limit: Continuation.BufferingPolicy | ||
var onTermination: TerminationHandler? | ||
|
@@ -105,7 +105,7 @@ extension AsyncStream { | |
} | ||
} | ||
|
||
func cancel() { | ||
@Sendable func cancel() { | ||
lock() | ||
// swap out the handler before we invoke it to prevent double cancel | ||
let handler = state.onTermination | ||
|
@@ -123,7 +123,9 @@ extension AsyncStream { | |
lock() | ||
let limit = state.limit | ||
let count = state.pending.count | ||
if let continuation = state.continuation { | ||
|
||
if !state.continuations.isEmpty { | ||
let continuation = state.continuations.removeFirst() | ||
if count > 0 { | ||
if !state.terminal { | ||
switch limit { | ||
|
@@ -151,17 +153,14 @@ extension AsyncStream { | |
} else { | ||
result = .terminated | ||
} | ||
state.continuation = nil | ||
let toSend = state.pending.removeFirst() | ||
unlock() | ||
continuation.resume(returning: toSend) | ||
} else if state.terminal { | ||
state.continuation = nil | ||
result = .terminated | ||
unlock() | ||
continuation.resume(returning: nil) | ||
} else { | ||
state.continuation = nil | ||
switch limit { | ||
case .unbounded: | ||
result = .enqueued(remaining: .max) | ||
|
@@ -212,15 +211,15 @@ extension AsyncStream { | |
state.onTermination = nil | ||
state.terminal = true | ||
|
||
if let continuation = state.continuation { | ||
if let continuation = state.continuations.first { | ||
if state.pending.count > 0 { | ||
state.continuation = nil | ||
state.continuations.removeFirst() | ||
let toSend = state.pending.removeFirst() | ||
unlock() | ||
handler?(.finished) | ||
continuation.resume(returning: toSend) | ||
} else if state.terminal { | ||
state.continuation = nil | ||
state.continuations.removeFirst() | ||
unlock() | ||
handler?(.finished) | ||
continuation.resume(returning: nil) | ||
|
@@ -236,22 +235,20 @@ extension AsyncStream { | |
|
||
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() | ||
} | ||
state.continuations.append(continuation) | ||
if state.pending.count > 0 { | ||
let cont = state.continuations.removeFirst() | ||
let toSend = state.pending.removeFirst() | ||
unlock() | ||
cont.resume(returning: toSend) | ||
} else if state.terminal { | ||
let cont = state.continuations.removeFirst() | ||
unlock() | ||
cont.resume(returning: nil) | ||
} else { | ||
unlock() | ||
fatalError("attempt to await next() on more than one task") | ||
} | ||
|
||
} | ||
|
||
func next() async -> Element? { | ||
|
@@ -341,7 +338,7 @@ extension AsyncThrowingStream { | |
} | ||
} | ||
|
||
func cancel() { | ||
@Sendable func cancel() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is that one necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so since it is used in the termination which is sendable |
||
lock() | ||
// swap out the handler before we invoke it to prevent double cancel | ||
let handler = state.onTermination | ||
|
@@ -595,3 +592,4 @@ final class _AsyncStreamCriticalStorage<Contents>: @unchecked Sendable { | |
return storage | ||
} | ||
} | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phausler apologies for resurrecting this, but if you have a chance, could you help me understand why this check was removed? i assume it was not directly related to the conditional Sendable conformance since the same change wasn't made to the throwing variant, but maybe i'm overlooking something.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phausler I'd like the
fatalError
back please.