Skip to content

[Concurrency] Introduce Task.id#89237

Draft
ktoso wants to merge 2 commits into
swiftlang:mainfrom
ktoso:wip-task-id
Draft

[Concurrency] Introduce Task.id#89237
ktoso wants to merge 2 commits into
swiftlang:mainfrom
ktoso:wip-task-id

Conversation

@ktoso
Copy link
Copy Markdown
Contributor

@ktoso ktoso commented May 19, 2026

This ID is stable and can be used to quickly identify tasks.

Pitch thread: https://forums.swift.org/t/pitch-cheap-task-identity-for-high-performance-instrumentation/86666/23

This ID is stable and can be used to quickly identify tasks.
It is implemented as a growing counter though we don't promise it'll
stay this way.

Align with proposal

update the symbols
@ktoso ktoso added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label May 19, 2026
///
/// Available: Swift 6.5
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
uint64_t swift_task_getCurrentTaskId(void);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is more efficient than getting the task builtin into swift and then call swift_task_getJobTaskId on it; because getting it into swift caused an ARC operation on the task handle.

// ever surfacing the task reference to Swift, so there's no ARC dance.
// The runtime returns 0 when there is no current task; task IDs are
// guaranteed to be non-zero.
let id = _getCurrentTaskId()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Backdeployment note: We could backdeploy this more if we do the "get task -> get id from job" and use the more efficient version in 6.5+

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think that would be a desirable idea.

@ktoso ktoso changed the title Introduce Task.id for quickly identifying tasks [Concurrency] Introduce Task.id May 19, 2026
@ktoso
Copy link
Copy Markdown
Contributor Author

ktoso commented May 19, 2026

@swift-ci please smoke test

@ktoso ktoso added the concurrency Feature: umbrella label for concurrency language features label May 19, 2026
@ktoso
Copy link
Copy Markdown
Contributor Author

ktoso commented May 19, 2026

@swift-ci please smoke test

/// - SeeAlso: ``UnsafeCurrentTask/id``
@available(StdlibDeploymentTarget 6.5, *)
@frozen
public struct TaskID: Sendable, Hashable {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry for asking, but why do we want to have this as a separate type? Isn't this just a single UInt64? Not that I'm against using newtypes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the info! I agree using a newtype is more idiomatic.

/// scoped to the current process and are not suitable for cross-process
/// correlation.
///
/// Reading the ID of the currently-executing task is fast.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should expand this part. I will come up with a suggestion if you haven't already.

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 pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants