2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / nio / natSelectorImplPosix.cc
blobac16dac7b9a715f2ec7dbd47b6b64de184c7e4ab
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
9 details. */
11 #include <config.h>
12 #include <platform.h>
14 #include <errno.h>
15 #include <string.h>
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>
22 static void
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];
30 if (fd > 0)
32 FD_SET (tmpFDArray [index], &fds);
34 if (tmpFDArray [index] > max_fd)
35 max_fd = tmpFDArray [index];
40 static void
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;
53 static void
54 helper_reset (jintArray& fdArray)
56 jint* tmpFDArray = elements (fdArray);
58 for (int index = 0; index < JvGetArrayLength (fdArray); index++)
59 tmpFDArray [index] = 0;
62 jint
63 gnu::java::nio::SelectorImpl::implSelect (jintArray read, jintArray write,
64 jintArray except, jlong timeout)
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;
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)
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
87 FD_ZERO (&read_fds);
88 FD_ZERO (&write_fds);
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
97 try
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 ();
109 helper_reset (read);
110 helper_reset (write);
111 helper_reset (except);
112 return 0;
115 if (result < 0)
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);
126 return result;