Skip to content

Conversation

jsimmons
Copy link
Contributor

@jsimmons jsimmons commented Jun 26, 2025

Accessing the thread id is often used in profiling and debugging, as well as some approaches for sound single-threaded access to shared data.

Currently the only way to access the thread id is by first obtaining a handle to the current thread. While this is not exactly slow, it does require an atomic inc-ref and dec-ref operation, as well as the injection of Thread's drop code into the caller.

This publicly exposes the existing fast-path for accessing the current thread id.

edit: ACP: rust-lang/libs-team#650

@rustbot
Copy link
Collaborator

rustbot commented Jun 26, 2025

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 26, 2025
@workingjubilee
Copy link
Member

r? libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jun 26, 2025
@rustbot rustbot assigned m-ou-se and unassigned jhpratt Jun 26, 2025
@bors
Copy link
Collaborator

bors commented Aug 7, 2025

☔ The latest upstream changes (presumably #115746) made this pull request unmergeable. Please resolve the merge conflicts.

@tgross35
Copy link
Contributor

tgross35 commented Sep 2, 2025

Note: I have no idea how these kinds of proposals usually go, but an acp felt overkill for a tiny code change so here we are :)

Could you submit one anyway? It's the easiest way to get something on the radar, and it should be trivial.

@tgross35
Copy link
Contributor

tgross35 commented Sep 5, 2025

r? tgross35 for after that is done
@rustbot author for the ACP

@rustbot rustbot assigned tgross35 and unassigned m-ou-se Sep 5, 2025
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 5, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@tgross35 tgross35 added the S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. label Sep 5, 2025
@jsimmons jsimmons force-pushed the current-thread-id-accessor branch from 175584f to 82eb244 Compare September 7, 2025 19:47
@rustbot

This comment has been minimized.

@joshtriplett
Copy link
Member

r=me once the ACP is approved.

@joshtriplett
Copy link
Member

@bors delegate+

@bors
Copy link
Collaborator

bors commented Sep 13, 2025

✌️ @jsimmons, you can now approve this pull request!

If @joshtriplett told you to "r=me" after making some further change, please make that change, then do @bors r=@joshtriplett

@Dylan-DPC Dylan-DPC removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 26, 2025
@jsimmons
Copy link
Contributor Author

ACP Accepted: rust-lang/libs-team#650

@jsimmons
Copy link
Contributor Author

@bors r=@joshtriplett

@bors
Copy link
Collaborator

bors commented Sep 30, 2025

📌 Commit 82eb244 has been approved by joshtriplett

It is now in the queue for this repository.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Sep 30, 2025
@joboet
Copy link
Member

joboet commented Sep 30, 2025

@bors r-
The tracking issue number is missing from the #[unstable] attributes.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 30, 2025
@jsimmons jsimmons force-pushed the current-thread-id-accessor branch from 82eb244 to 5400f9e Compare September 30, 2025 18:19
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

/// });
///
/// let other_thread_id = other_thread.join().unwrap();
/// assert!(thread::current_id() != other_thread_id);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// assert!(thread::current_id() != other_thread_id);
/// assert_ne!(thread::current_id(), other_thread_id);

@tgross35 tgross35 removed the S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. label Sep 30, 2025
@jsimmons jsimmons force-pushed the current-thread-id-accessor branch from 5400f9e to c081c87 Compare September 30, 2025 18:40
@rustbot
Copy link
Collaborator

rustbot commented Sep 30, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Accessing the thread id is often used in profiling and debugging, as
well as some approaches for sound single-threaded access to data.

Currently the only way to access the thread id is by first obtaining a
handle to the current thread. While this is not exactly slow, it does
require an atomic inc-ref and dec-ref operation, as well as the
injection of `Thread`'s drop code into the caller.

This publicly exposes the existing fast-path for accessing the current
thread id.
@jsimmons jsimmons force-pushed the current-thread-id-accessor branch from c081c87 to cbaec31 Compare September 30, 2025 18:51
@tgross35
Copy link
Contributor

@bors r=joshtriplett,tgross35

@bors
Copy link
Collaborator

bors commented Sep 30, 2025

📌 Commit cbaec31 has been approved by joshtriplett,tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 30, 2025
bors added a commit that referenced this pull request Sep 30, 2025
Rollup of 6 pull requests

Successful merges:

 - #143069 (Add fast-path for accessing the current thread id)
 - #146518 (Improve the documentation around `ZERO_AR_DATE`)
 - #146596 (Add a dummy codegen backend)
 - #146617 (Don’t suggest foreign `doc(hidden)` types in "the following other types implement trait" diagnostics)
 - #146635 (cg_llvm: Stop using `as_c_char_ptr` for coverage-related bindings)
 - #147184 (Fix the bevy implied bounds hack for the next solver)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Oct 1, 2025
Rollup of 6 pull requests

Successful merges:

 - #143069 (Add fast-path for accessing the current thread id)
 - #146518 (Improve the documentation around `ZERO_AR_DATE`)
 - #146596 (Add a dummy codegen backend)
 - #146617 (Don’t suggest foreign `doc(hidden)` types in "the following other types implement trait" diagnostics)
 - #146635 (cg_llvm: Stop using `as_c_char_ptr` for coverage-related bindings)
 - #147184 (Fix the bevy implied bounds hack for the next solver)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3e621b3 into rust-lang:master Oct 1, 2025
10 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Oct 1, 2025
rust-timer added a commit that referenced this pull request Oct 1, 2025
Rollup merge of #143069 - jsimmons:current-thread-id-accessor, r=joshtriplett,tgross35

Add fast-path for accessing the current thread id

Accessing the thread id is often used in profiling and debugging, as well as some approaches for sound single-threaded access to shared data.

Currently the only way to access the thread id is by first obtaining a handle to the current thread. While this is not exactly slow, it does require an atomic inc-ref and dec-ref operation, as well as the injection of `Thread`'s drop code into the caller.

This publicly exposes the existing fast-path for accessing the current thread id.

edit: ACP: rust-lang/libs-team#650
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.