2 Extern implementation of Nit module stream
6 #include "stream._ffi.h"
7 #define Array_of_Int_length stream___Array_of_Int_length
8 #define Array_of_Int__index stream___Array_of_Int__index
9 #line 18 "lib/standard/stream.nit"
16 nullable_Int
stream___Object_intern_poll___impl( Object recv
, Array_of_Int in_fds
, Array_of_Int out_fds
)
18 #line 320 "lib/standard/stream.nit"
21 int in_len
, out_len
, total_len
;
25 int first_polled_fd
= -1;
28 in_len
= Array_of_Int_length( in_fds
);
29 out_len
= Array_of_Int_length( out_fds
);
30 total_len
= in_len
+ out_len
;
31 c_fds
= malloc( sizeof(struct pollfd
) * total_len
);
34 for ( i
=0; i
<in_len
; i
++ ) {
36 fd
= Array_of_Int__index( in_fds
, i
);
39 c_fds
[i
].events
= POLLIN
;
43 for ( i
=0; i
<out_len
; i
++ ) {
45 fd
= Array_of_Int__index( out_fds
, i
);
48 c_fds
[i
].events
= POLLOUT
;
51 /* poll all fds, unlimited timeout */
52 result
= poll( c_fds
, total_len
, -1 );
56 for ( i
=0; i
<total_len
; i
++ )
57 if ( c_fds
[i
].revents
& c_fds
[i
].events
|| /* awaited event */
58 c_fds
[i
].revents
& POLLHUP
) /* closed */
60 first_polled_fd
= c_fds
[i
].fd
;
64 return Int_as_nullable( first_polled_fd
);
66 else if ( result
< 0 )
67 fprintf( stderr
, "Error in Stream:poll: %s\n", strerror( errno
) );