8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use cmp;
12
11
use io;
13
12
use libc:: { self , c_int} ;
14
13
use mem;
15
- use ptr;
16
14
use sys:: { cvt, cvt_r} ;
17
15
use sys:: fd:: FileDesc ;
18
16
@@ -80,16 +78,14 @@ pub fn read2(p1: AnonPipe,
80
78
p1. set_nonblocking ( true ) ?;
81
79
p2. set_nonblocking ( true ) ?;
82
80
83
- let max = cmp:: max ( p1. raw ( ) , p2. raw ( ) ) ;
81
+ let mut fds: [ libc:: pollfd ; 2 ] = unsafe { mem:: zeroed ( ) } ;
82
+ fds[ 0 ] . fd = p1. raw ( ) ;
83
+ fds[ 0 ] . events = libc:: POLLIN ;
84
+ fds[ 1 ] . fd = p2. raw ( ) ;
85
+ fds[ 1 ] . events = libc:: POLLIN ;
84
86
loop {
85
- // wait for either pipe to become readable using `select`
86
- cvt_r ( || unsafe {
87
- let mut read: libc:: fd_set = mem:: zeroed ( ) ;
88
- libc:: FD_SET ( p1. raw ( ) , & mut read) ;
89
- libc:: FD_SET ( p2. raw ( ) , & mut read) ;
90
- libc:: select ( max + 1 , & mut read, ptr:: null_mut ( ) , ptr:: null_mut ( ) ,
91
- ptr:: null_mut ( ) )
92
- } ) ?;
87
+ // wait for either pipe to become readable using `poll`
88
+ cvt_r ( || unsafe { libc:: poll ( fds. as_mut_ptr ( ) , 2 , -1 ) } ) ?;
93
89
94
90
// Read as much as we can from each pipe, ignoring EWOULDBLOCK or
95
91
// EAGAIN. If we hit EOF, then this will happen because the underlying
@@ -109,11 +105,11 @@ pub fn read2(p1: AnonPipe,
109
105
}
110
106
}
111
107
} ;
112
- if read ( & p1, v1) ? {
108
+ if fds [ 0 ] . revents != 0 && read ( & p1, v1) ? {
113
109
p2. set_nonblocking ( false ) ?;
114
110
return p2. read_to_end ( v2) . map ( |_| ( ) ) ;
115
111
}
116
- if read ( & p2, v2) ? {
112
+ if fds [ 1 ] . revents != 0 && read ( & p2, v2) ? {
117
113
p1. set_nonblocking ( false ) ?;
118
114
return p1. read_to_end ( v1) . map ( |_| ( ) ) ;
119
115
}
0 commit comments