FSF GCC merge 02/23/03
[official-gcc.git] / libjava / gnu / java / nio / natSelectorImpl.cc
blobd7185828d592c3a6c842825cb7805364e446c9b1
1 // natSelectorImpl.cc
3 /* Copyright (C) 2002 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
9 details. */
11 #include <config.h>
12 #include <platform.h>
14 #include <errno.h>
15 #include <string.h>
17 #if HAVE_BSTRING_H
18 // Needed for bzero, implicitly used by FD_ZERO on IRIX 5.2
19 #include <bstring.h>
20 #endif
22 //#include <gcj/cni.h>
23 #include <gnu/java/nio/SelectorImpl.h>
24 #include <java/io/IOException.h>
26 void
27 helper_put_filedescriptors (jintArray java_fd_array, fd_set& fds, int& max_fd)
29 int counter;
30 jint* java_fds;
32 java_fds = elements (java_fd_array);
34 for (counter = 0; counter < JvGetArrayLength (java_fd_array); counter++)
36 FD_SET (java_fds [counter], &fds);
38 if (java_fds [counter] > max_fd)
40 max_fd = java_fds [counter];
45 void
46 helper_get_filedescriptors (jintArray& java_fd_array, fd_set fds)
48 int counter;
49 int counter_fds;
50 jint* java_fds;
51 jintArray new_array_fds;
52 jint* new_data_fds;
54 counter_fds = 0;
55 java_fds = elements (java_fd_array);
57 for (counter = 0; counter < JvGetArrayLength (java_fd_array); counter++)
59 if (FD_ISSET (java_fds[counter], &fds))
61 counter_fds++;
65 new_array_fds = JvNewIntArray (counter_fds);
66 new_data_fds = elements (new_array_fds);
68 for (counter = 0; counter < JvGetArrayLength (java_fd_array); counter++)
70 if (FD_ISSET (java_fds[counter], &fds))
72 new_data_fds[counter] = java_fds[counter];
76 java_fd_array = new_array_fds;
79 jint
80 gnu::java::nio::SelectorImpl::java_do_select (jintArray read, jintArray write,
81 jintArray except, jlong timeout)
83 jint result;
84 int max_fd = 0;
85 fd_set read_fds;
86 fd_set write_fds;
87 fd_set except_fds;
88 struct timeval real_time_data;
89 struct timeval *time_data = NULL;
91 real_time_data.tv_sec = 0;
92 real_time_data.tv_usec = timeout;
94 // If not legal timeout value is given, use NULL.
95 // This means an infinite timeout.
96 if (timeout >= 0)
98 time_data = &real_time_data;
101 // Reset all fd_set structures
102 FD_ZERO (&read_fds);
103 FD_ZERO (&write_fds);
104 FD_ZERO (&except_fds);
106 // Fill the fd_set data structures for the _Jv_select() call.
107 helper_put_filedescriptors (read, read_fds, max_fd);
108 helper_put_filedescriptors (write, write_fds, max_fd);
109 helper_put_filedescriptors (except, except_fds, max_fd);
111 // Actually do the select
112 result = _Jv_select (max_fd + 1, &read_fds, &write_fds, &except_fds, time_data);
114 if (result < 0)
116 char* strerr = strerror (errno);
117 throw new ::java::io::IOException (JvNewStringUTF (strerr));
120 // Set the file descriptors according to the values returned from select().
121 helper_get_filedescriptors (read, read_fds);
122 helper_get_filedescriptors (write, write_fds);
123 helper_get_filedescriptors (except, except_fds);
125 return result;