std: lazily allocate the main thread handle - #132654
Conversation
commented
Nov 5, 2024
|
r? @ChrisDenton rustbot has assigned @ChrisDenton. Use |
This comment has been minimized.
This comment has been minimized.
EDIT: I was right! |
| #[cfg(not(target_thread_local))] | ||
| #[allow(unused)] |
There was a problem hiding this comment.
Because this is also used when 64-bit atomics are unavailable.
There was a problem hiding this comment.
Is there any more specific cfg that can be used? #[allow(unused)], while sometimes necessary, makes me nervous because it could silently become actual dead code in the future.
| // INVARIANT: must be valid UTF-8 | ||
| name: Option<CString>, |
There was a problem hiding this comment.
This change seems worse. Having an invariant that can be violated in safe code is not great.
There was a problem hiding this comment.
I also wondered if it made sense to have a new-type that held both invariants (UTF-8 & c-string \0 ending) ?
There was a problem hiding this comment.
Utf8CStr is a fairly common type in the ecosystem, it would make sense for us to start tinkering with providing it.
commented
Nov 5, 2024
|
Also could you separate the file move into a different commit? It's easier to diff that way. |
This comment has been minimized.
This comment has been minimized.
commented
Nov 7, 2024
commented
Nov 20, 2024
|
☔ The latest upstream changes (presumably #133219) made this pull request unmergeable. Please resolve the merge conflicts. |
commented
Nov 25, 2024
|
I've undone the @rustbot ready |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
commented
Jan 9, 2025
|
☔ The latest upstream changes (presumably #135279) made this pull request unmergeable. Please resolve the merge conflicts. |
commented
Jan 11, 2025
|
☔ The latest upstream changes (presumably #135357) made this pull request unmergeable. Please resolve the merge conflicts. |
This reverts commit 0747f28.
Thereby, we also allow accessing thread::current before main: as the runtime no longer tries to install its own handle, this will no longer trigger an abort. Rather, the name returned from name will 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::current` would 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).
Originally authored by GnomedDev
commented
Jan 14, 2025
|
@bors r=ChrisDenton |
#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