Skip to content

Commit 71b86da

Browse files
committed
Uses ptr::from_ref in futex.rs
1 parent 02fd202 commit 71b86da

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

library/std/src/sys/pal/unix/futex.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
target_os = "fuchsia",
99
))]
1010

11+
use crate::ptr;
1112
use crate::sync::atomic::AtomicU32;
1213
use crate::time::Duration;
1314

@@ -24,7 +25,6 @@ pub type SmallPrimitive = u32;
2425
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
2526
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
2627
use super::time::Timespec;
27-
use crate::ptr::null;
2828
use crate::sync::atomic::Ordering::Relaxed;
2929

3030
// Calculate the timeout as an absolute timespec.
@@ -52,10 +52,10 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
5252
_flags: libc::UMTX_ABSTIME,
5353
_clockid: libc::CLOCK_MONOTONIC as u32,
5454
});
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);
5656
let umtx_timeout_size = umtx_timeout.as_ref().map_or(0, |t| crate::mem::size_of_val(t));
5757
libc::_umtx_op(
58-
futex as *const AtomicU32 as *mut _,
58+
ptr::from_ref(futex) as *mut _,
5959
libc::UMTX_OP_WAIT_UINT_PRIVATE,
6060
expected as libc::c_ulong,
6161
crate::ptr::without_provenance_mut(umtx_timeout_size),
@@ -66,11 +66,11 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
6666
// absolute time rather than a relative time.
6767
libc::syscall(
6868
libc::SYS_futex,
69-
futex as *const AtomicU32,
69+
ptr::from_ref(futex),
7070
libc::FUTEX_WAIT_BITSET | libc::FUTEX_PRIVATE_FLAG,
7171
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.
7474
!0u32, // A full bitmask, to make it behave like a regular FUTEX_WAIT.
7575
)
7676
} else {
@@ -95,55 +95,52 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
9595
/// On some platforms, this always returns false.
9696
#[cfg(any(target_os = "linux", target_os = "android"))]
9797
pub fn futex_wake(futex: &AtomicU32) -> bool {
98-
let ptr = futex as *const AtomicU32;
98+
let p = ptr::from_ref(futex);
9999
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 }
101101
}
102102

103103
/// Wakes up all threads that are waiting on `futex_wait` on this futex.
104104
#[cfg(any(target_os = "linux", target_os = "android"))]
105105
pub fn futex_wake_all(futex: &AtomicU32) {
106-
let ptr = futex as *const AtomicU32;
106+
let p = ptr::from_ref(futex);
107107
let op = libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG;
108108
unsafe {
109-
libc::syscall(libc::SYS_futex, ptr, op, i32::MAX);
109+
libc::syscall(libc::SYS_futex, p, op, i32::MAX);
110110
}
111111
}
112112

113113
// FreeBSD doesn't tell us how many threads are woken up, so this always returns false.
114114
#[cfg(target_os = "freebsd")]
115115
pub fn futex_wake(futex: &AtomicU32) -> bool {
116-
use crate::ptr::null_mut;
117116
unsafe {
118117
libc::_umtx_op(
119-
futex as *const AtomicU32 as *mut _,
118+
ptr::from_ref(futex) as *mut _,
120119
libc::UMTX_OP_WAKE_PRIVATE,
121120
1,
122-
null_mut(),
123-
null_mut(),
121+
ptr::null_mut(),
122+
ptr::null_mut(),
124123
)
125124
};
126125
false
127126
}
128127

129128
#[cfg(target_os = "freebsd")]
130129
pub fn futex_wake_all(futex: &AtomicU32) {
131-
use crate::ptr::null_mut;
132130
unsafe {
133131
libc::_umtx_op(
134-
futex as *const AtomicU32 as *mut _,
132+
ptr::from_ref(futex) as *mut _,
135133
libc::UMTX_OP_WAKE_PRIVATE,
136134
i32::MAX as libc::c_ulong,
137-
null_mut(),
138-
null_mut(),
135+
ptr::null_mut(),
136+
ptr::null_mut(),
139137
)
140138
};
141139
}
142140

143141
#[cfg(target_os = "openbsd")]
144142
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
145143
use super::time::Timespec;
146-
use crate::ptr::{null, null_mut};
147144

148145
// Overflows are rounded up to an infinite timeout (None).
149146
let timespec = timeout
@@ -152,11 +149,11 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
152149

153150
let r = unsafe {
154151
libc::futex(
155-
futex as *const AtomicU32 as *mut u32,
152+
ptr::from_ref(futex) as *mut u32,
156153
libc::FUTEX_WAIT,
157154
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(),
160157
)
161158
};
162159

@@ -165,23 +162,26 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
165162

166163
#[cfg(target_os = "openbsd")]
167164
pub fn futex_wake(futex: &AtomicU32) -> bool {
168-
use crate::ptr::{null, null_mut};
169165
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
172173
}
173174
}
174175

175176
#[cfg(target_os = "openbsd")]
176177
pub fn futex_wake_all(futex: &AtomicU32) {
177-
use crate::ptr::{null, null_mut};
178178
unsafe {
179179
libc::futex(
180-
futex as *const AtomicU32 as *mut u32,
180+
ptr::from_ref(futex) as *mut u32,
181181
libc::FUTEX_WAKE,
182182
i32::MAX,
183-
null(),
184-
null_mut(),
183+
ptr::null(),
184+
ptr::null_mut(),
185185
);
186186
}
187187
}
@@ -195,7 +195,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
195195
timeout.and_then(|d| Some(i32::try_from(d.as_millis()).ok()?.max(1))).unwrap_or(0);
196196

197197
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)
199199
};
200200

201201
r == 0 || super::os::errno() != libc::ETIMEDOUT
@@ -204,13 +204,13 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
204204
// DragonflyBSD doesn't tell us how many threads are woken up, so this always returns false.
205205
#[cfg(target_os = "dragonfly")]
206206
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) };
208208
false
209209
}
210210

211211
#[cfg(target_os = "dragonfly")]
212212
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) };
214214
}
215215

216216
#[cfg(target_os = "emscripten")]

0 commit comments

Comments
 (0)