1 // natSelectorImplPosix.cc
3 /* Copyright (C) 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
17 #include <gnu/java/nio/SelectorImpl.h>
18 #include <java/io/InterruptedIOException.h>
19 #include <java/io/IOException.h>
20 #include <java/lang/Thread.h>
23 helper_put_filedescriptors (jintArray fdArray
, fd_set
& fds
, int& max_fd
)
25 jint
* tmpFDArray
= elements (fdArray
);
27 for (int index
= 0; index
< JvGetArrayLength (fdArray
); index
++)
29 int fd
= tmpFDArray
[index
];
32 FD_SET (tmpFDArray
[index
], &fds
);
34 if (tmpFDArray
[index
] > max_fd
)
35 max_fd
= tmpFDArray
[index
];
41 helper_get_filedescriptors (jintArray
& fdArray
, fd_set fds
)
43 jint
* tmpFDArray
= elements (fdArray
);
45 for (int index
= 0; index
< JvGetArrayLength (fdArray
); index
++)
47 int fd
= tmpFDArray
[index
];
48 if (fd
< 0 || !FD_ISSET (fd
, &fds
))
49 tmpFDArray
[index
] = 0;
54 helper_reset (jintArray
& fdArray
)
56 jint
* tmpFDArray
= elements (fdArray
);
58 for (int index
= 0; index
< JvGetArrayLength (fdArray
); index
++)
59 tmpFDArray
[index
] = 0;
63 gnu::java::nio::SelectorImpl::implSelect (jintArray read
, jintArray write
,
64 jintArray except
, jlong timeout
)
71 struct timeval real_time_data
;
72 struct timeval
*time_data
= NULL
;
74 // If a legal timeout value isn't given, use NULL.
75 // This means an infinite timeout. The specification
76 // also says that a zero timeout should be treated
77 // as infinite. Otherwise (if the timeout value is legal),
78 // fill our timeval struct and use it for the select.
81 real_time_data
.tv_sec
= timeout
/ 1000;
82 real_time_data
.tv_usec
= (timeout
% 1000) * 1000;
83 time_data
= &real_time_data
;
86 // Reset all fd_set structures
89 FD_ZERO (&except_fds
);
91 // Fill the fd_set data structures for the _Jv_select() call.
92 helper_put_filedescriptors (read
, read_fds
, max_fd
);
93 helper_put_filedescriptors (write
, write_fds
, max_fd
);
94 helper_put_filedescriptors (except
, except_fds
, max_fd
);
96 // Actually do the select
99 result
= _Jv_select (max_fd
+ 1, &read_fds
, &write_fds
,
100 &except_fds
, time_data
);
102 catch (::java::io::InterruptedIOException
*e
)
104 // The behavior of JRE 1.4.1 is that no exception is thrown
105 // when the thread is interrupted, but the thread's interrupt
106 // status is set. Clear all of our select sets and return 0,
107 // indicating that nothing was selected.
108 ::java::lang::Thread::currentThread ()->interrupt ();
110 helper_reset (write
);
111 helper_reset (except
);
117 char* strerr
= strerror (errno
);
118 throw new ::java::io::IOException (JvNewStringUTF (strerr
));
121 // Set the file descriptors according to the values returned from select().
122 helper_get_filedescriptors (read
, read_fds
);
123 helper_get_filedescriptors (write
, write_fds
);
124 helper_get_filedescriptors (except
, except_fds
);