2
2
3
3
use crate :: cmp;
4
4
use crate :: io:: { self , Initializer , IoSlice , IoSliceMut , Read } ;
5
- use crate :: lazy:: SyncOnceCell ;
6
5
use crate :: mem;
6
+ use crate :: sync:: atomic:: { AtomicUsize , Ordering } ;
7
7
use crate :: sys:: cvt;
8
8
use crate :: sys_common:: AsInner ;
9
9
@@ -28,10 +28,11 @@ const READ_LIMIT: usize = c_int::MAX as usize - 1;
28
28
const READ_LIMIT : usize = libc:: ssize_t:: MAX as usize ;
29
29
30
30
#[ cfg( any( target_os = "linux" , target_os = "macos" ) ) ]
31
- fn max_iov ( ) -> c_int {
32
- static LIM : SyncOnceCell < c_int > = SyncOnceCell :: new ( ) ;
31
+ fn max_iov ( ) -> usize {
32
+ static LIM : AtomicUsize = AtomicUsize :: new ( 0 ) ;
33
33
34
- * LIM . get_or_init ( || {
34
+ let mut lim = LIM . load ( Ordering :: Relaxed ) ;
35
+ if lim == 0 {
35
36
let ret = unsafe {
36
37
libc:: sysconf (
37
38
#[ cfg( target_os = "linux" ) ]
@@ -43,13 +44,16 @@ fn max_iov() -> c_int {
43
44
44
45
// 1024 is the default value on modern Linux systems
45
46
// and hopefully more useful than `c_int::MAX`.
46
- if ret > 0 { ret as c_int } else { 1024 }
47
- } )
47
+ lim = if ret > 0 { ret as usize } else { 1024 } ;
48
+ LIM . store ( lim, Ordering :: Relaxed ) ;
49
+ }
50
+
51
+ lim
48
52
}
49
53
50
54
#[ cfg( not( any( target_os = "linux" , target_os = "macos" ) ) ) ]
51
- fn max_iov ( ) -> c_int {
52
- c_int:: MAX
55
+ fn max_iov ( ) -> usize {
56
+ c_int:: MAX as usize
53
57
}
54
58
55
59
impl FileDesc {
@@ -80,7 +84,7 @@ impl FileDesc {
80
84
libc:: readv (
81
85
self . fd ,
82
86
bufs. as_ptr ( ) as * const libc:: iovec ,
83
- cmp:: min ( bufs. len ( ) , max_iov ( ) as usize ) as c_int ,
87
+ cmp:: min ( bufs. len ( ) , max_iov ( ) ) as c_int ,
84
88
)
85
89
} ) ?;
86
90
Ok ( ret as usize )
@@ -137,7 +141,7 @@ impl FileDesc {
137
141
libc:: writev (
138
142
self . fd ,
139
143
bufs. as_ptr ( ) as * const libc:: iovec ,
140
- cmp:: min ( bufs. len ( ) , max_iov ( ) as usize ) as c_int ,
144
+ cmp:: min ( bufs. len ( ) , max_iov ( ) ) as c_int ,
141
145
)
142
146
} ) ?;
143
147
Ok ( ret as usize )
0 commit comments