Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit596be7e

Browse files
committed
Use pthread_t instead of numeric thread id
1 parent2e90f6f commit596be7e

File tree

1 file changed

+12
-45
lines changed

1 file changed

+12
-45
lines changed

‎std/src/sys/pal/common/exit_guard.rs‎

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,60 +26,27 @@ cfg_if::cfg_if! {
2626
/// * If it is called again on a different thread, it will wait in a loop
2727
/// (waiting for the process to exit).
2828
pub(crate)fn unique_thread_exit(){
29-
let this_thread_id =unsafe{ libc::gettid()};
30-
debug_assert_ne!(this_thread_id,0,"thread ID cannot be zero");
31-
#[cfg(target_has_atomic ="32")]
32-
{
33-
usecrate::sync::atomic::{AtomicI32,Ordering};
34-
staticEXITING_THREAD_ID:AtomicI32 =AtomicI32::new(0);
35-
matchEXITING_THREAD_ID.compare_exchange(
36-
0,
37-
this_thread_id,
38-
Ordering::Relaxed,
39-
Ordering::Relaxed,
40-
){
41-
Ok(_zero) =>{
42-
// This is the first thread to call `unique_thread_exit`,
43-
// and this is the first time it is called.
44-
// Set EXITING_THREAD_ID to this thread's ID (done by the
45-
// compare_exchange) and return.
46-
}
47-
Err(id)if id == this_thread_id =>{
48-
// This is the first thread to call `unique_thread_exit`,
49-
// but this is the second time it is called.
50-
// Abort the process.
51-
core::panicking::panic_nounwind("std::process::exit called re-entrantly")
52-
}
53-
Err(_) =>{
54-
// This is not the first thread to call `unique_thread_exit`.
55-
// Pause until the process exits.
56-
loop{
57-
// Safety: libc::pause is safe to call.
58-
unsafe{ libc::pause();}
59-
}
60-
}
61-
}
62-
}
63-
#[cfg(not(target_has_atomic ="32"))]
64-
{
65-
usecrate::sync::{Mutex,PoisonError};
66-
staticEXITING_THREAD_ID:Mutex<i32> =Mutex::new(0);
67-
letmut exiting_thread_id =
68-
EXITING_THREAD_ID.lock().unwrap_or_else(PoisonError::into_inner);
69-
if*exiting_thread_id ==0{
29+
let this_thread_id =unsafe{ libc::pthread_self()};
30+
usecrate::sync::{Mutex,PoisonError};
31+
staticEXITING_THREAD_ID:Mutex<Option<libc::pthread_t>> =Mutex::new(None);
32+
letmut exiting_thread_id =
33+
EXITING_THREAD_ID.lock().unwrap_or_else(PoisonError::into_inner);
34+
match*exiting_thread_id{
35+
None =>{
7036
// This is the first thread to call `unique_thread_exit`,
7137
// and this is the first time it is called.
7238
// Set EXITING_THREAD_ID to this thread's ID and return.
73-
*exiting_thread_id = this_thread_id;
74-
} elseif*exiting_thread_id == this_thread_id{
39+
*exiting_thread_id =Some(this_thread_id);
40+
},
41+
Some(exiting_thread_id)if exiting_thread_id == this_thread_id =>{
7542
// This is the first thread to call `unique_thread_exit`,
7643
// but this is the second time it is called.
7744
// Abort the process.
7845
core::panicking::panic_nounwind("std::process::exit called re-entrantly")
79-
} else{
46+
}
47+
Some(_) =>{
8048
// This is not the first thread to call `unique_thread_exit`.
8149
// Pause until the process exits.
82-
// Park until the process exits.
8350
drop(exiting_thread_id);
8451
loop{
8552
// Safety: libc::pause is safe to call.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp