8
8
target_os = "fuchsia" ,
9
9
) ) ]
10
10
11
+ use crate :: ptr;
11
12
use crate :: sync:: atomic:: AtomicU32 ;
12
13
use crate :: time:: Duration ;
13
14
@@ -24,7 +25,6 @@ pub type SmallPrimitive = u32;
24
25
#[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "freebsd" ) ) ]
25
26
pub fn futex_wait ( futex : & AtomicU32 , expected : u32 , timeout : Option < Duration > ) -> bool {
26
27
use super :: time:: Timespec ;
27
- use crate :: ptr:: null;
28
28
use crate :: sync:: atomic:: Ordering :: Relaxed ;
29
29
30
30
// Calculate the timeout as an absolute timespec.
@@ -52,10 +52,10 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
52
52
_flags: libc:: UMTX_ABSTIME ,
53
53
_clockid: libc:: CLOCK_MONOTONIC as u32 ,
54
54
} ) ;
55
- let umtx_timeout_ptr = umtx_timeout. as_ref( ) . map_or( null( ) , |t| t as * const _ ) ;
55
+ let umtx_timeout_ptr = umtx_timeout. as_ref( ) . map_or( ptr :: null( ) , ptr :: from_ref ) ;
56
56
let umtx_timeout_size = umtx_timeout. as_ref( ) . map_or( 0 , |t| crate :: mem:: size_of_val( t) ) ;
57
57
libc:: _umtx_op(
58
- futex as * const AtomicU32 as * mut _,
58
+ ptr :: from_ref ( futex) as * mut _,
59
59
libc:: UMTX_OP_WAIT_UINT_PRIVATE ,
60
60
expected as libc:: c_ulong,
61
61
crate :: ptr:: without_provenance_mut( umtx_timeout_size) ,
@@ -66,11 +66,11 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
66
66
// absolute time rather than a relative time.
67
67
libc:: syscall(
68
68
libc:: SYS_futex ,
69
- futex as * const AtomicU32 ,
69
+ ptr :: from_ref ( futex) ,
70
70
libc:: FUTEX_WAIT_BITSET | libc:: FUTEX_PRIVATE_FLAG ,
71
71
expected,
72
- timespec. as_ref( ) . map_or( null( ) , |t| t as * const libc :: timespec ) ,
73
- null:: <u32 >( ) , // This argument is unused for FUTEX_WAIT_BITSET.
72
+ timespec. as_ref( ) . map_or( ptr :: null( ) , ptr :: from_ref ) ,
73
+ ptr :: null:: <u32 >( ) , // This argument is unused for FUTEX_WAIT_BITSET.
74
74
!0u32 , // A full bitmask, to make it behave like a regular FUTEX_WAIT.
75
75
)
76
76
} else {
@@ -95,55 +95,52 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
95
95
/// On some platforms, this always returns false.
96
96
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
97
97
pub fn futex_wake ( futex : & AtomicU32 ) -> bool {
98
- let ptr = futex as * const AtomicU32 ;
98
+ let p = ptr :: from_ref ( futex) ;
99
99
let op = libc:: FUTEX_WAKE | libc:: FUTEX_PRIVATE_FLAG ;
100
- unsafe { libc:: syscall ( libc:: SYS_futex , ptr , op, 1 ) > 0 }
100
+ unsafe { libc:: syscall ( libc:: SYS_futex , p , op, 1 ) > 0 }
101
101
}
102
102
103
103
/// Wakes up all threads that are waiting on `futex_wait` on this futex.
104
104
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
105
105
pub fn futex_wake_all ( futex : & AtomicU32 ) {
106
- let ptr = futex as * const AtomicU32 ;
106
+ let p = ptr :: from_ref ( futex) ;
107
107
let op = libc:: FUTEX_WAKE | libc:: FUTEX_PRIVATE_FLAG ;
108
108
unsafe {
109
- libc:: syscall ( libc:: SYS_futex , ptr , op, i32:: MAX ) ;
109
+ libc:: syscall ( libc:: SYS_futex , p , op, i32:: MAX ) ;
110
110
}
111
111
}
112
112
113
113
// FreeBSD doesn't tell us how many threads are woken up, so this always returns false.
114
114
#[ cfg( target_os = "freebsd" ) ]
115
115
pub fn futex_wake ( futex : & AtomicU32 ) -> bool {
116
- use crate :: ptr:: null_mut;
117
116
unsafe {
118
117
libc:: _umtx_op (
119
- futex as * const AtomicU32 as * mut _ ,
118
+ ptr :: from_ref ( futex) as * mut _ ,
120
119
libc:: UMTX_OP_WAKE_PRIVATE ,
121
120
1 ,
122
- null_mut ( ) ,
123
- null_mut ( ) ,
121
+ ptr :: null_mut ( ) ,
122
+ ptr :: null_mut ( ) ,
124
123
)
125
124
} ;
126
125
false
127
126
}
128
127
129
128
#[ cfg( target_os = "freebsd" ) ]
130
129
pub fn futex_wake_all ( futex : & AtomicU32 ) {
131
- use crate :: ptr:: null_mut;
132
130
unsafe {
133
131
libc:: _umtx_op (
134
- futex as * const AtomicU32 as * mut _ ,
132
+ ptr :: from_ref ( futex) as * mut _ ,
135
133
libc:: UMTX_OP_WAKE_PRIVATE ,
136
134
i32:: MAX as libc:: c_ulong ,
137
- null_mut ( ) ,
138
- null_mut ( ) ,
135
+ ptr :: null_mut ( ) ,
136
+ ptr :: null_mut ( ) ,
139
137
)
140
138
} ;
141
139
}
142
140
143
141
#[ cfg( target_os = "openbsd" ) ]
144
142
pub fn futex_wait ( futex : & AtomicU32 , expected : u32 , timeout : Option < Duration > ) -> bool {
145
143
use super :: time:: Timespec ;
146
- use crate :: ptr:: { null, null_mut} ;
147
144
148
145
// Overflows are rounded up to an infinite timeout (None).
149
146
let timespec = timeout
@@ -152,11 +149,11 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
152
149
153
150
let r = unsafe {
154
151
libc:: futex (
155
- futex as * const AtomicU32 as * mut u32 ,
152
+ ptr :: from_ref ( futex) as * mut u32 ,
156
153
libc:: FUTEX_WAIT ,
157
154
expected as i32 ,
158
- timespec. as_ref ( ) . map_or ( null ( ) , |t| t as * const libc :: timespec ) ,
159
- null_mut ( ) ,
155
+ timespec. as_ref ( ) . map_or ( ptr :: null ( ) , ptr :: from ) ,
156
+ ptr :: null_mut ( ) ,
160
157
)
161
158
} ;
162
159
@@ -165,23 +162,26 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
165
162
166
163
#[ cfg( target_os = "openbsd" ) ]
167
164
pub fn futex_wake ( futex : & AtomicU32 ) -> bool {
168
- use crate :: ptr:: { null, null_mut} ;
169
165
unsafe {
170
- libc:: futex ( futex as * const AtomicU32 as * mut u32 , libc:: FUTEX_WAKE , 1 , null ( ) , null_mut ( ) )
171
- > 0
166
+ libc:: futex (
167
+ ptr:: from_ref ( futex) as * mut u32 ,
168
+ libc:: FUTEX_WAKE ,
169
+ 1 ,
170
+ ptr:: null ( ) ,
171
+ ptr:: null_mut ( ) ,
172
+ ) > 0
172
173
}
173
174
}
174
175
175
176
#[ cfg( target_os = "openbsd" ) ]
176
177
pub fn futex_wake_all ( futex : & AtomicU32 ) {
177
- use crate :: ptr:: { null, null_mut} ;
178
178
unsafe {
179
179
libc:: futex (
180
- futex as * const AtomicU32 as * mut u32 ,
180
+ ptr :: from_ref ( futex) as * mut u32 ,
181
181
libc:: FUTEX_WAKE ,
182
182
i32:: MAX ,
183
- null ( ) ,
184
- null_mut ( ) ,
183
+ ptr :: null ( ) ,
184
+ ptr :: null_mut ( ) ,
185
185
) ;
186
186
}
187
187
}
@@ -195,7 +195,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
195
195
timeout. and_then ( |d| Some ( i32:: try_from ( d. as_millis ( ) ) . ok ( ) ?. max ( 1 ) ) ) . unwrap_or ( 0 ) ;
196
196
197
197
let r = unsafe {
198
- libc:: umtx_sleep ( futex as * const AtomicU32 as * const i32 , expected as i32 , timeout_ms)
198
+ libc:: umtx_sleep ( ptr :: from_ref ( futex) as * const i32 , expected as i32 , timeout_ms)
199
199
} ;
200
200
201
201
r == 0 || super :: os:: errno ( ) != libc:: ETIMEDOUT
@@ -204,13 +204,13 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
204
204
// DragonflyBSD doesn't tell us how many threads are woken up, so this always returns false.
205
205
#[ cfg( target_os = "dragonfly" ) ]
206
206
pub fn futex_wake ( futex : & AtomicU32 ) -> bool {
207
- unsafe { libc:: umtx_wakeup ( futex as * const AtomicU32 as * const i32 , 1 ) } ;
207
+ unsafe { libc:: umtx_wakeup ( ptr :: from_ref ( futex) as * const i32 , 1 ) } ;
208
208
false
209
209
}
210
210
211
211
#[ cfg( target_os = "dragonfly" ) ]
212
212
pub fn futex_wake_all ( futex : & AtomicU32 ) {
213
- unsafe { libc:: umtx_wakeup ( futex as * const AtomicU32 as * const i32 , i32:: MAX ) } ;
213
+ unsafe { libc:: umtx_wakeup ( ptr :: from_ref ( futex) as * const i32 , i32:: MAX ) } ;
214
214
}
215
215
216
216
#[ cfg( target_os = "emscripten" ) ]
0 commit comments