-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Concurrency: Factor atomic operations in Task.sleep()
into a Sendable
wrapper
#72062
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
Conversation
… wrapper. When building the Swift standard library, the compiler warns: ``` warning: capture of 'wordPtr' with non-sendable type 'UnsafeMutablePointer<Builtin.Word>' in a `@Sendable` closure ``` This diagnostic will become an error in the Swift 6 language mode, so it needs to be addressed. This PR factors the atomic operations used by the implementation of `Task.sleep()` into an `@unchecked Sendable` wrapper in order to convince the compiler that these operations are thread-safe. As an added benefit, the code is more readable when the atomic operatios are abstracted away. This refactor intentionally translates the existing implementation into a wrapper as faithfully as possible and does not attempt to improve on any other aspects of the implementation, such as the unsafe manual memory allocation and deallocation.
@swift-ci please test |
I wonder if we can use |
When working on this I did briefly try to explore whether it was possible to use a non-copyable type to entirely replace the need for a pointer to heap allocated storage. However, as far as I can tell there'd be no way to convince the compiler that the lifetime of the closure passed to If adding a dependency on
To match the expectations of the existing builtins used for atomic operations. |
Ok, after looking into more I think this should be doable by using |
@Azoy I created a draft prototype of adopting |
while true { | ||
let state = SleepState(loading: wordPtr) | ||
let state = token.load() |
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.
late to the party here -- I was looking into this recently and it turned out we didn't properly handle nonisolated(unsafe)
for local variables (heh). That was fixed since then so I wondered if we could just nonisolated(unsafe)
the wordPtr
now and avoid the wrapper entirely.
Although avoiding the alloc entirely would be even better...! So thanks for investigating that too.
This resolved rdar://117594245 |
When building the Swift standard library, the compiler warns:
This diagnostic will become an error in the Swift 6 language mode, so it needs to be addressed. This PR factors the atomic operations used by the implementation of
Task.sleep()
into an@unchecked Sendable
wrapper in order to convince the compiler that these operations are thread-safe. As an added benefit, the code is more readable when the atomic operations are abstracted away. This refactor intentionally translates the existing implementation into a wrapper as faithfully as possible and does not attempt to improve on any other aspects of the implementation, such as the unsafe manual memory allocation and deallocation.