File tree Expand file tree Collapse file tree 1 file changed +27
-4
lines changed
library/std/src/sys/pal/unix Expand file tree Collapse file tree 1 file changed +27
-4
lines changed Original file line number Diff line number Diff line change
1
+ use crate :: cell:: Cell ;
2
+ use crate :: thread_local;
3
+
4
+ thread_local ! {
5
+ static TID : Cell <u32 > = Cell :: new( 0 ) ;
6
+ }
7
+
1
8
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
2
9
mod linux {
10
+ use super :: TID ;
3
11
use crate :: ops:: Deref ;
4
12
use crate :: sync:: atomic:: AtomicU32 ;
5
13
use crate :: sys:: cvt;
@@ -27,7 +35,14 @@ mod linux {
27
35
}
28
36
29
37
pub fn locked ( ) -> State {
30
- ( unsafe { libc:: gettid ( ) } ) as _
38
+ let tid = TID . get ( ) ;
39
+ if tid == 0 {
40
+ let tid = ( unsafe { libc:: gettid ( ) } ) as u32 ;
41
+ TID . set ( tid) ;
42
+ tid
43
+ } else {
44
+ tid
45
+ }
31
46
}
32
47
33
48
pub fn is_contended ( futex_val : State ) -> bool {
@@ -75,6 +90,7 @@ pub use linux::*;
75
90
76
91
#[ cfg( target_os = "freebsd" ) ]
77
92
mod freebsd {
93
+ use super :: TID ;
78
94
use crate :: mem:: transmute;
79
95
use crate :: ops:: Deref ;
80
96
use crate :: sync:: atomic:: AtomicU32 ;
@@ -125,9 +141,16 @@ mod freebsd {
125
141
}
126
142
127
143
pub fn locked ( ) -> State {
128
- let mut tid: libc:: c_long = 0 ;
129
- let _ = unsafe { libc:: thr_self ( ptr:: from_mut ( & mut tid) ) } ;
130
- tid as _
144
+ let tid = TID . get ( ) ;
145
+ if tid == 0 {
146
+ let mut tid: libc:: c_long = 0 ;
147
+ let _ = unsafe { libc:: thr_self ( ptr:: from_mut ( & mut tid) ) } ;
148
+ let tid = tid as u32 ;
149
+ TID . set ( tid) ;
150
+ tid
151
+ } else {
152
+ tid
153
+ }
131
154
}
132
155
133
156
pub fn is_contended ( futex_val : State ) -> bool {
You can’t perform that action at this time.
0 commit comments