File tree 1 file changed +11
-12
lines changed
1 file changed +11
-12
lines changed Original file line number Diff line number Diff line change 1
1
#![ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
2
2
3
+ use crate :: convert:: TryInto ;
4
+ use crate :: ptr:: null;
3
5
use crate :: sync:: atomic:: AtomicI32 ;
4
6
use crate :: time:: Duration ;
5
7
6
8
pub fn futex_wait ( futex : & AtomicI32 , expected : i32 , timeout : Option < Duration > ) {
7
- let timespec;
8
- let timespec_ptr = match timeout {
9
- Some ( timeout) => {
10
- timespec = libc:: timespec {
11
- tv_sec : timeout. as_secs ( ) as _ ,
12
- tv_nsec : timeout. subsec_nanos ( ) as _ ,
13
- } ;
14
- & timespec as * const libc:: timespec
15
- }
16
- None => crate :: ptr:: null ( ) ,
17
- } ;
9
+ let timespec = timeout. and_then ( |d| {
10
+ Some ( libc:: timespec {
11
+ // Sleep forever if the timeout is longer than fits in a timespec.
12
+ tv_sec : d. as_secs ( ) . try_into ( ) . ok ( ) ?,
13
+ // This conversion never truncates, as subsec_nanos is always <1e9.
14
+ tv_nsec : d. subsec_nanos ( ) as _ ,
15
+ } )
16
+ } ) ;
18
17
unsafe {
19
18
libc:: syscall (
20
19
libc:: SYS_futex ,
21
20
futex as * const AtomicI32 ,
22
21
libc:: FUTEX_WAIT | libc:: FUTEX_PRIVATE_FLAG ,
23
22
expected,
24
- timespec_ptr ,
23
+ timespec . as_ref ( ) . map_or ( null ( ) , |d| d as * const libc :: timespec ) ,
25
24
) ;
26
25
}
27
26
}
You can’t perform that action at this time.
0 commit comments