Skip to content

Add Task.isCurrent #67504

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

KaQuMiQ
Copy link

@KaQuMiQ KaQuMiQ commented Jul 25, 2023

Add extension to Task allowing to check if it is currently running task.

@KaQuMiQ KaQuMiQ requested review from ktoso and kavon as code owners July 25, 2023 09:35
@ktoso ktoso added the swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal label Jul 25, 2023
@ktoso
Copy link
Contributor

ktoso commented Jul 25, 2023

This would need a swift evolution proposal: https://github.com/apple/swift-evolution

It's not wrong per se, but makes me wonder about the usefulness of this check.
Can you explain more why you'd need such method?

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs Swift Evolution proposal

@ktoso ktoso added the concurrency Feature: umbrella label for concurrency language features label Jul 25, 2023
@ktoso ktoso self-assigned this Jul 25, 2023
@KaQuMiQ
Copy link
Author

KaQuMiQ commented Jul 25, 2023

Sure! The main use case of this is to allow clearing / cancelling tasks which are spawned to produce a side effect on an object which keeps track of multiple tasks. Tasks cannot have a reference to itself (nor any identifier unless you explicitly create one on the side) in order to identify themselves i.e within a callback to the object managing tasks. So i.e. if you have an object which spawns a task for mutation of itself while tracking spawned tasks and one of them finishes this allows to identify which one it was without any additional work. This is extremely useful in case where you chain multiple tasks. In that case you can track reference only to the latest one and use that reference for spawning next (exchange current with new one and wait for current inside new one). In that case if you want to clear the task on callback you know if latest scheduled is the one which is executing callback. I am already using this implementation: https://gist.github.com/KaQuMiQ/a3a73bfd7ff4b41c7d231a1ce1555bde for this purpose. It helps simplifying multiple tasks management by eliminating additional unnecessary identifiers.

@ktoso
Copy link
Contributor

ktoso commented Jul 25, 2023

Hmmm... this sounds somewhat suspicious if I'm to be honest. A Task is Hashable/Equatable so you could store and "remove this one" if you do pass around unstructured Task, and the notion of chaining tasks also seems suspicious. With async/await the "chaining" as you'd normally do with Futures is done using async/await, and not chaining one task to another.

I would suggest we take this idea to the forums: https://forums.swift.org where you can take a stab at a Swift Evolution style writeup why this is necessary and we could evaluate the specific use-cases. Right now I'm a bit concerned that code patterns that we don't want to encourage made you reach for this.

Thanks in advance!

@KaQuMiQ
Copy link
Author

KaQuMiQ commented Jul 25, 2023

A Task is Hashable/Equatable so you could store and "remove this one" if you do pass around unstructured Task

Yes, but when you spawn a Task it does not have an access to reference to itself. Having a proxy access would require additional dedicated synchronization or could lead to grabbing invalid reference to different task when gathering exclusive access to the reference again after task was spawned.

let spawned: Task = .detached {
  // can't get reference to spawned, doing it from outside might return different task after synchronizing access again
}

the notion of chaining tasks also seems suspicious. With async/await the "chaining" as you'd normally do with Futures is done using async/await, and not chaining one task to another.

Yes in asychronous context that is natural. However I am refering to the situation where you know that there is a spawned task you have to wait for but you are producing updates in synchronous context and can't wait for the result while having a need for producing a new update.

I would suggest we take this idea to the forums: https://forums.swift.org/

I will prepare something then. Thanks for response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
concurrency Feature: umbrella label for concurrency language features swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants