diff options
| author | Zachary S <zasample18+github@gmail.com> | 2024-07-03 13:02:47 -0500 |
|---|---|---|
| committer | Zachary S <zasample18+github@gmail.com> | 2024-07-03 13:28:24 -0500 |
| commit | 5e83fafd88c2b2f8510f7b9db8c2748be7eb239c (patch) | |
| tree | a938659b9afb9646139e6c58a93d319e396b7e04 /library | |
| parent | c36fdeb9a3da04839e0892e1351286881c848e39 (diff) | |
| download | rust-5e83fafd88c2b2f8510f7b9db8c2748be7eb239c.tar.gz rust-5e83fafd88c2b2f8510f7b9db8c2748be7eb239c.zip | |
Use libc::pause instead of std::thread::park in wait-for-exit loop
Diffstat (limited to 'library')
| -rw-r--r-- | library/std/src/sys/pal/common/exit_guard.rs | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/library/std/src/sys/pal/common/exit_guard.rs b/library/std/src/sys/pal/common/exit_guard.rs index 37c8f97e45d..06f62941db7 100644 --- a/library/std/src/sys/pal/common/exit_guard.rs +++ b/library/std/src/sys/pal/common/exit_guard.rs @@ -10,9 +10,9 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "linux")] { /// Mitigation for <https://github.com/rust-lang/rust/issues/126600> /// - /// On `unix` (where `libc::exit` may not be thread-safe), ensure that only one Rust thread - /// calls `libc::exit` (or returns from `main`) by calling this function before calling - /// `libc::exit` (or returning from `main`). + /// On UNIX-like platforms (where `libc::exit` may not be thread-safe), ensure that only one + /// Rust thread calls `libc::exit` (or returns from `main`) by calling this function before + /// calling `libc::exit` (or returning from `main`). /// /// Technically not enough to ensure soundness, since other code directly calling /// libc::exit will still race with this. @@ -23,7 +23,7 @@ cfg_if::cfg_if! { /// This function will return only the first time it is called in a process. /// /// * If it is called again on the same thread as the first call, it will abort. - /// * If it is called again on a different thread, it will `thread::park()` in a loop + /// * If it is called again on a different thread, it will wait in a loop /// (waiting for the process to exit). pub(crate) fn unique_thread_exit() { let this_thread_id = unsafe { libc::gettid() }; @@ -52,9 +52,10 @@ cfg_if::cfg_if! { } Err(_) => { // This is not the first thread to call `unique_thread_exit`. - // Park until the process exits. + // Pause until the process exits. loop { - crate::thread::park(); + // Safety: libc::pause is safe to call. + unsafe { libc::pause(); } } } } @@ -77,10 +78,12 @@ cfg_if::cfg_if! { core::panicking::panic_nounwind("std::process::exit called re-entrantly") } else { // This is not the first thread to call `unique_thread_exit`. + // Pause until the process exits. // Park until the process exits. drop(exiting_thread_id); loop { - crate::thread::park(); + // Safety: libc::pause is safe to call. + unsafe { libc::pause(); } } } } |
