File tree Expand file tree Collapse file tree 1 file changed +25
-3
lines changed Expand file tree Collapse file tree 1 file changed +25
-3
lines changed Original file line number Diff line number Diff line change 1
1
use libc:: c_int;
2
+
2
3
use std:: fs:: File ;
3
4
use std:: io:: { self , Read , Write } ;
4
5
use std:: mem;
@@ -21,13 +22,24 @@ pub struct Acquired {
21
22
}
22
23
23
24
impl Client {
24
- pub fn new ( limit : usize ) -> io:: Result < Client > {
25
+ pub fn new ( mut limit : usize ) -> io:: Result < Client > {
25
26
let client = unsafe { Client :: mk ( ) ? } ;
27
+
26
28
// I don't think the character written here matters, but I could be
27
29
// wrong!
28
- for _ in 0 ..limit {
29
- ( & client. write ) . write_all ( & [ b'|' ] ) ?;
30
+ const BUFFER : [ u8 ; 128 ] = [ b'|' ; 128 ] ;
31
+
32
+ set_nonblocking ( client. write . as_raw_fd ( ) , true ) ?;
33
+
34
+ while limit > 0 {
35
+ let n = limit. min ( BUFFER . len ( ) ) ;
36
+
37
+ ( & client. write ) . write_all ( & BUFFER [ ..n] ) ?;
38
+ limit -= n;
30
39
}
40
+
41
+ set_nonblocking ( client. write . as_raw_fd ( ) , false ) ?;
42
+
31
43
Ok ( client)
32
44
}
33
45
@@ -322,6 +334,16 @@ fn set_cloexec(fd: c_int, set: bool) -> io::Result<()> {
322
334
}
323
335
}
324
336
337
+ fn set_nonblocking ( fd : c_int , set : bool ) -> io:: Result < ( ) > {
338
+ let status_flag = if set { libc:: O_NONBLOCK } else { 0 } ;
339
+
340
+ unsafe {
341
+ cvt ( libc:: fcntl ( fd, libc:: F_SETFL , status_flag) ) ?;
342
+ }
343
+
344
+ Ok ( ( ) )
345
+ }
346
+
325
347
fn cvt ( t : c_int ) -> io:: Result < c_int > {
326
348
if t == -1 {
327
349
Err ( io:: Error :: last_os_error ( ) )
You can’t perform that action at this time.
0 commit comments