]> gcc.gnu.org Git - gcc.git/blob - libjava/gnu/java/nio/natSelectorImplPosix.cc
ac16dac7b9a715f2ec7dbd47b6b64de184c7e4ab
[gcc.git] / libjava / gnu / java / nio / natSelectorImplPosix.cc
1 // natSelectorImplPosix.cc
2
3 /* Copyright (C) 2002, 2003 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 #include <config.h>
12 #include <platform.h>
13
14 #include <errno.h>
15 #include <string.h>
16
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>
21
22 static void
23 helper_put_filedescriptors (jintArray fdArray, fd_set& fds, int& max_fd)
24 {
25 jint* tmpFDArray = elements (fdArray);
26
27 for (int index = 0; index < JvGetArrayLength (fdArray); index++)
28 {
29 int fd = tmpFDArray [index];
30 if (fd > 0)
31 {
32 FD_SET (tmpFDArray [index], &fds);
33
34 if (tmpFDArray [index] > max_fd)
35 max_fd = tmpFDArray [index];
36 }
37 }
38 }
39
40 static void
41 helper_get_filedescriptors (jintArray& fdArray, fd_set fds)
42 {
43 jint* tmpFDArray = elements (fdArray);
44
45 for (int index = 0; index < JvGetArrayLength (fdArray); index++)
46 {
47 int fd = tmpFDArray [index];
48 if (fd < 0 || !FD_ISSET (fd, &fds))
49 tmpFDArray [index] = 0;
50 }
51 }
52
53 static void
54 helper_reset (jintArray& fdArray)
55 {
56 jint* tmpFDArray = elements (fdArray);
57
58 for (int index = 0; index < JvGetArrayLength (fdArray); index++)
59 tmpFDArray [index] = 0;
60 }
61
62 jint
63 gnu::java::nio::SelectorImpl::implSelect (jintArray read, jintArray write,
64 jintArray except, jlong timeout)
65 {
66 jint result;
67 int max_fd = 0;
68 fd_set read_fds;
69 fd_set write_fds;
70 fd_set except_fds;
71 struct timeval real_time_data;
72 struct timeval *time_data = NULL;
73
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.
79 if (timeout > 0)
80 {
81 real_time_data.tv_sec = timeout / 1000;
82 real_time_data.tv_usec = (timeout % 1000) * 1000;
83 time_data = &real_time_data;
84 }
85
86 // Reset all fd_set structures
87 FD_ZERO (&read_fds);
88 FD_ZERO (&write_fds);
89 FD_ZERO (&except_fds);
90
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);
95
96 // Actually do the select
97 try
98 {
99 result = _Jv_select (max_fd + 1, &read_fds, &write_fds,
100 &except_fds, time_data);
101 }
102 catch (::java::io::InterruptedIOException *e)
103 {
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 ();
109 helper_reset (read);
110 helper_reset (write);
111 helper_reset (except);
112 return 0;
113 }
114
115 if (result < 0)
116 {
117 char* strerr = strerror (errno);
118 throw new ::java::io::IOException (JvNewStringUTF (strerr));
119 }
120
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);
125
126 return result;
127 }
This page took 0.596835 seconds and 4 git commands to generate.