1
1
cfg_if:: cfg_if! {
2
2
if #[ cfg( target_os = "linux" ) ] {
3
+ /// pthread_t is a pointer on some platforms,
4
+ /// so we wrap it in this to impl Send + Sync.
5
+ #[ derive( Clone , Copy ) ]
6
+ #[ repr( transparent) ]
7
+ struct PThread ( libc:: pthread_t) ;
8
+ // Safety: pthread_t is safe to send between threads
9
+ unsafe impl Send for PThread { }
10
+ // Safety: pthread_t is safe to share between threads
11
+ unsafe impl Sync for PThread { }
3
12
/// Mitigation for <https://github.com/rust-lang/rust/issues/126600>
4
13
///
5
14
/// On glibc, `libc::exit` has been observed to not always be thread-safe.
@@ -23,17 +32,17 @@ cfg_if::cfg_if! {
23
32
pub ( crate ) fn unique_thread_exit( ) {
24
33
let this_thread_id = unsafe { libc:: pthread_self( ) } ;
25
34
use crate :: sync:: { Mutex , PoisonError } ;
26
- static EXITING_THREAD_ID : Mutex <Option <libc :: pthread_t >> = Mutex :: new( None ) ;
35
+ static EXITING_THREAD_ID : Mutex <Option <PThread >> = Mutex :: new( None ) ;
27
36
let mut exiting_thread_id =
28
37
EXITING_THREAD_ID . lock( ) . unwrap_or_else( PoisonError :: into_inner) ;
29
38
match * exiting_thread_id {
30
39
None => {
31
40
// This is the first thread to call `unique_thread_exit`,
32
41
// and this is the first time it is called.
33
42
// Set EXITING_THREAD_ID to this thread's ID and return.
34
- * exiting_thread_id = Some ( this_thread_id) ;
43
+ * exiting_thread_id = Some ( PThread ( this_thread_id) ) ;
35
44
} ,
36
- Some ( exiting_thread_id) if exiting_thread_id == this_thread_id => {
45
+ Some ( exiting_thread_id) if exiting_thread_id. 0 == this_thread_id => {
37
46
// This is the first thread to call `unique_thread_exit`,
38
47
// but this is the second time it is called.
39
48
// Abort the process.
0 commit comments