std: lazily allocate the main thread handle#132654
Merged
bors merged 4 commits intorust-lang:masterfrom Jan 15, 2025
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
#123550 eliminated the allocation of the main thread handle, but at the cost of greatly increased complexity. This PR proposes another approach: Instead of creating the main thread handle itself, the runtime simply remembers the thread ID of the main thread. The main thread handle is then only allocated when it is used, using the same lazy-initialization mechanism as for non-runtime use of
thread::current, and thenamemethod uses the thread ID to identify the main thread handle and return the correct name ("main") for it.Thereby, we also allow accessing
thread::currentbefore main: as the runtime no longer tries to install its own handle, this will no longer trigger an abort. Rather, the name returned fromnamewill only be "main" after the runtime initialization code has run, but I think that is acceptable.This new approach also requires some changes to the signal handling code, as calling
thread::currentwould now allocate when called on the main thread, which is not acceptable. I fixed this by adding a new function (with_current_name) that performs all the naming logic without allocation or without initializing the thread ID (which could allocate on some platforms).Reverts #123550, CC @GnomedDev