1
1
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
2
2
mod linux {
3
+ use crate :: cell:: Cell ;
3
4
use crate :: ops:: Deref ;
4
5
use crate :: sync:: atomic:: AtomicU32 ;
5
6
use crate :: sys:: cvt;
6
- use crate :: { io, ptr} ;
7
+ use crate :: { io, ptr, thread_local} ;
8
+
9
+ thread_local ! {
10
+ static TID : Cell <u32 > = Cell :: new( 0 ) ;
11
+ }
7
12
8
13
pub type State = u32 ;
9
14
@@ -27,7 +32,14 @@ mod linux {
27
32
}
28
33
29
34
pub fn locked ( ) -> State {
30
- ( unsafe { libc:: gettid ( ) } ) as _
35
+ let tid = TID . get ( ) ;
36
+ if tid == 0 {
37
+ let tid = ( unsafe { libc:: gettid ( ) } ) as u32 ;
38
+ TID . set ( tid) ;
39
+ tid
40
+ } else {
41
+ tid
42
+ }
31
43
}
32
44
33
45
pub fn is_contended ( futex_val : State ) -> bool {
@@ -75,11 +87,16 @@ pub use linux::*;
75
87
76
88
#[ cfg( target_os = "freebsd" ) ]
77
89
mod freebsd {
90
+ use crate :: cell:: Cell ;
78
91
use crate :: mem:: transmute;
79
92
use crate :: ops:: Deref ;
80
93
use crate :: sync:: atomic:: AtomicU32 ;
81
94
use crate :: sys:: cvt;
82
- use crate :: { io, ptr} ;
95
+ use crate :: { io, ptr, thread_local} ;
96
+
97
+ thread_local ! {
98
+ static TID : Cell <u32 > = Cell :: new( 0 ) ;
99
+ }
83
100
84
101
pub type State = u32 ;
85
102
@@ -125,9 +142,16 @@ mod freebsd {
125
142
}
126
143
127
144
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 _
145
+ let tid = TID . get ( ) ;
146
+ if tid == 0 {
147
+ let mut tid: libc:: c_long = 0 ;
148
+ let _ = unsafe { libc:: thr_self ( ptr:: from_mut ( & mut tid) ) } ;
149
+ let tid = tid as u32 ;
150
+ TID . set ( tid) ;
151
+ tid
152
+ } else {
153
+ tid
154
+ }
131
155
}
132
156
133
157
pub fn is_contended ( futex_val : State ) -> bool {
0 commit comments