about summary refs log tree commit diff
path: root/library/std/src/sys/pal/unix/stack_overflow.rs
diff options
context:
space:
mode:
authorjoboet <jonasboettiger@icloud.com>2024-11-25 13:01:35 +0100
committerjoboet <jonasboettiger@icloud.com>2025-01-14 13:37:28 +0100
commit14f7f4b7bfcfbfe9a6f778e762cac83420e08007 (patch)
tree90265fd148d36907b20072e05ced8406d520206f /library/std/src/sys/pal/unix/stack_overflow.rs
parent0e5ee891b2b07321175e398153bfa86c667f3494 (diff)
downloadrust-14f7f4b7bfcfbfe9a6f778e762cac83420e08007.tar.gz
rust-14f7f4b7bfcfbfe9a6f778e762cac83420e08007.zip
std: lazily allocate the main thread handle
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).
Diffstat (limited to 'library/std/src/sys/pal/unix/stack_overflow.rs')
-rw-r--r--library/std/src/sys/pal/unix/stack_overflow.rs9
1 files changed, 5 insertions, 4 deletions
diff --git a/library/std/src/sys/pal/unix/stack_overflow.rs b/library/std/src/sys/pal/unix/stack_overflow.rs
index 69b31da427f..db5c6bd3a1c 100644
--- a/library/std/src/sys/pal/unix/stack_overflow.rs
+++ b/library/std/src/sys/pal/unix/stack_overflow.rs
@@ -100,10 +100,11 @@ mod imp {
         // If the faulting address is within the guard page, then we print a
         // message saying so and abort.
         if start <= addr && addr < end {
-            rtprintpanic!(
-                "\nthread '{}' has overflowed its stack\n",
-                thread::current().name().unwrap_or("<unknown>")
-            );
+            thread::with_current_name(|name| {
+                let name = name.unwrap_or("<unknown>");
+                rtprintpanic!("\nthread '{name}' has overflowed its stack\n");
+            });
+
             rtabort!("stack overflow");
         } else {
             // Unregister ourselves by reverting back to the default behavior.