1 /* This file is part of NIT ( http://www.nitlanguage.org ).
3 * Copyright 2004-2008 Jean Privat <jean@pryen.org>
5 * This file is free software, which comes along with NIT. This software is
6 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 * is kept unaltered, and a notification of the changes is added.
10 * You are allowed to redistribute it and sell it, alone or is a part of
14 #include "stream_nit.h"
19 int stream_FDStream_FDStream_native_read_char_1(void *s
, int fd
) {
22 ssize_t r
= read(fd
, &buf
, 1);
32 C implementation of stream::Object::intern_poll
34 Imported methods signatures:
35 bigint Array_length( Array recv ) for array::AbstractArrayRead::(abstract_collection::Collection::length)
36 nullable_Object Array__index( Array recv, bigint index ) for array::Array::(abstract_collection::SequenceRead::[])
37 int nullable_Object_is_a_bigint( nullable_Object value ) to check if a nullable Object is a Int
38 bigint nullable_Object_as_bigint( nullable_Object value ) to cast from nullable Object to Int
39 int Int_is_null( nullable_Int value ) to check if a nullable Int is a Int
40 bigint bigint_as_not_null( nullable_Int value ) to cast from nullable Int to Int
42 nullable_Int
Object_intern_poll___impl( Object recv
, Array in_fds
, Array out_fds
) {
43 int in_len
, out_len
, total_len
;
47 nullable_Object tmp_nit_obj
;
48 int first_polled_fd
= -1;
51 in_len
= Array_length( in_fds
);
52 out_len
= Array_length( out_fds
);
53 total_len
= in_len
+ out_len
;
54 c_fds
= malloc( sizeof(struct pollfd
) * total_len
);
57 for ( i
=0; i
<in_len
; i
++ ) {
59 tmp_nit_obj
= Array__index( in_fds
, i
);
60 fd
= nullable_Object_as_Int( tmp_nit_obj
);
63 c_fds
[i
].events
= POLLIN
;
67 for ( i
=0; i
<out_len
; i
++ ) {
69 tmp_nit_obj
= Array__index( out_fds
, i
);
70 fd
= nullable_Object_as_Int( tmp_nit_obj
);
73 c_fds
[i
].events
= POLLOUT
;
76 /* poll all fds, unlimited timeout */
77 result
= poll( c_fds
, total_len
, -1 );
81 for ( i
=0; i
<total_len
; i
++ )
82 if ( c_fds
[i
].revents
& c_fds
[i
].events
|| /* awaited event */
83 c_fds
[i
].revents
& POLLHUP
) /* closed */
85 first_polled_fd
= c_fds
[i
].fd
;
89 return Int_as_nullable( first_polled_fd
);
91 else if ( result
< 0 )
92 fprintf( stderr
, "Error in Stream:poll: %s\n", strerror( errno
) );