88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- use cmp;
1211use io;
1312use libc:: { self , c_int} ;
1413use mem;
15- use ptr;
1614use sys:: { cvt, cvt_r} ;
1715use sys:: fd:: FileDesc ;
1816
@@ -80,16 +78,14 @@ pub fn read2(p1: AnonPipe,
8078 p1. set_nonblocking ( true ) ?;
8179 p2. set_nonblocking ( true ) ?;
8280
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 ;
8486 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 ) } ) ?;
9389
9490 // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
9591 // EAGAIN. If we hit EOF, then this will happen because the underlying
@@ -109,11 +105,11 @@ pub fn read2(p1: AnonPipe,
109105 }
110106 }
111107 } ;
112- if read ( & p1, v1) ? {
108+ if fds [ 0 ] . revents != 0 && read ( & p1, v1) ? {
113109 p2. set_nonblocking ( false ) ?;
114110 return p2. read_to_end ( v2) . map ( |_| ( ) ) ;
115111 }
116- if read ( & p2, v2) ? {
112+ if fds [ 1 ] . revents != 0 && read ( & p2, v2) ? {
117113 p1. set_nonblocking ( false ) ?;
118114 return p1. read_to_end ( v1) . map ( |_| ( ) ) ;
119115 }
0 commit comments