1 |
774 |
jeremybenn |
/* gnu_java_nio_EpollSelectorImpl.c --
|
2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is a part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or (at
|
9 |
|
|
your option) any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
19 |
|
|
USA
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
#ifdef HAVE_CONFIG_H
|
40 |
|
|
#include <config.h>
|
41 |
|
|
#endif /* HAVE_CONFIG_H */
|
42 |
|
|
|
43 |
|
|
#ifdef HAVE_SYS_EPOLL_H
|
44 |
|
|
#include <sys/epoll.h>
|
45 |
|
|
#endif /* HAVE_SYS_EPOLL_H */
|
46 |
|
|
|
47 |
|
|
#include <config-int.h>
|
48 |
|
|
|
49 |
|
|
#include <gnu_java_nio_EpollSelectorImpl.h>
|
50 |
|
|
#include <jcl.h>
|
51 |
|
|
#include <errno.h>
|
52 |
|
|
#include <string.h>
|
53 |
|
|
|
54 |
|
|
#define IO_EXCEPTION "java/io/IOException"
|
55 |
|
|
|
56 |
|
|
/* #define TRACE_EPOLL 1 */
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/*
|
60 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
61 |
|
|
* Method: epoll_supported
|
62 |
|
|
* Signature: ()Z
|
63 |
|
|
*/
|
64 |
|
|
JNIEXPORT jboolean JNICALL
|
65 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_epoll_1supported (JNIEnv *e __attribute__((unused)),
|
66 |
|
|
jclass c __attribute__((unused)))
|
67 |
|
|
{
|
68 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
69 |
|
|
return JNI_TRUE;
|
70 |
|
|
#else
|
71 |
|
|
return JNI_FALSE;
|
72 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/*
|
76 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
77 |
|
|
* Method: sizeof_struct
|
78 |
|
|
* Signature: ()I
|
79 |
|
|
*/
|
80 |
|
|
JNIEXPORT jint JNICALL
|
81 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_sizeof_1struct (JNIEnv *env,
|
82 |
|
|
jclass c __attribute__((unused)))
|
83 |
|
|
{
|
84 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
85 |
|
|
(void) env;
|
86 |
|
|
#ifdef TRACE_EPOLL
|
87 |
|
|
fprintf (stderr, "%s: sizeof is %d\n", __FUNCTION__, sizeof (struct epoll_event));
|
88 |
|
|
#endif /* TRACE_EPOLL */
|
89 |
|
|
return sizeof (struct epoll_event);
|
90 |
|
|
#else
|
91 |
|
|
JCL_ThrowException (env, "java/lang/InternalError", "epoll support not available");
|
92 |
|
|
return -1;
|
93 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
/*
|
97 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
98 |
|
|
* Method: epoll_create
|
99 |
|
|
* Signature: (I)I
|
100 |
|
|
*/
|
101 |
|
|
JNIEXPORT jint JNICALL
|
102 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_epoll_1create (JNIEnv *env,
|
103 |
|
|
jclass c __attribute__((unused)),
|
104 |
|
|
jint size)
|
105 |
|
|
{
|
106 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
107 |
|
|
int fd = epoll_create (size);
|
108 |
|
|
|
109 |
|
|
#ifdef TRACE_EPOLL
|
110 |
|
|
fprintf (stderr, "%s: epoll_create returns %d\n", __FUNCTION__, fd);
|
111 |
|
|
#endif /* TRACE_EPOLL */
|
112 |
|
|
|
113 |
|
|
if (fd == -1)
|
114 |
|
|
{
|
115 |
|
|
if (ENOSYS == errno)
|
116 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
117 |
|
|
strerror (errno));
|
118 |
|
|
else
|
119 |
|
|
JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
|
120 |
|
|
}
|
121 |
|
|
return fd;
|
122 |
|
|
#else
|
123 |
|
|
(void) size;
|
124 |
|
|
JCL_ThrowException (env, "java/lang/InternalError", "epoll support not available");
|
125 |
|
|
return -1;
|
126 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/*
|
130 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
131 |
|
|
* Method: epoll_add
|
132 |
|
|
* Signature: (III)V
|
133 |
|
|
*/
|
134 |
|
|
JNIEXPORT void JNICALL
|
135 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_epoll_1add (JNIEnv *env,
|
136 |
|
|
jclass c __attribute__((unused)),
|
137 |
|
|
jint efd, jint fd, jint ops)
|
138 |
|
|
{
|
139 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
140 |
|
|
struct epoll_event event;
|
141 |
|
|
|
142 |
|
|
memset (&event, 0, sizeof (struct epoll_event));
|
143 |
|
|
|
144 |
|
|
if ((ops & gnu_java_nio_EpollSelectorImpl_OP_ACCEPT) != 0
|
145 |
|
|
|| (ops & gnu_java_nio_EpollSelectorImpl_OP_READ) != 0)
|
146 |
|
|
event.events = EPOLLIN;
|
147 |
|
|
|
148 |
|
|
if ((ops & gnu_java_nio_EpollSelectorImpl_OP_CONNECT) != 0
|
149 |
|
|
|| (ops & gnu_java_nio_EpollSelectorImpl_OP_WRITE) != 0)
|
150 |
|
|
event.events |= EPOLLOUT;
|
151 |
|
|
|
152 |
|
|
event.data.fd = fd;
|
153 |
|
|
|
154 |
|
|
#ifdef TRACE_EPOLL
|
155 |
|
|
fprintf (stderr, "%s: adding struct epoll_event { events: %o; data.fd: %d } to %d\n",
|
156 |
|
|
__FUNCTION__, event.events, event.data.fd, efd);
|
157 |
|
|
#endif /* TRACE_EPOLL */
|
158 |
|
|
|
159 |
|
|
if (epoll_ctl (efd, EPOLL_CTL_ADD, fd, &event) == -1)
|
160 |
|
|
{
|
161 |
|
|
if (ENOSYS == errno)
|
162 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
163 |
|
|
strerror (errno));
|
164 |
|
|
else
|
165 |
|
|
JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
|
166 |
|
|
}
|
167 |
|
|
#else
|
168 |
|
|
(void) efd;
|
169 |
|
|
(void) fd;
|
170 |
|
|
(void) ops;
|
171 |
|
|
JCL_ThrowException (env, "java/lang/InternalError", "epoll support not available");
|
172 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
/*
|
177 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
178 |
|
|
* Method: epoll_modify
|
179 |
|
|
* Signature: (III)V
|
180 |
|
|
*/
|
181 |
|
|
JNIEXPORT void JNICALL
|
182 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_epoll_1modify (JNIEnv *env,
|
183 |
|
|
jclass c __attribute__((unused)),
|
184 |
|
|
jint efd, jint fd, jint ops)
|
185 |
|
|
{
|
186 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
187 |
|
|
struct epoll_event event;
|
188 |
|
|
|
189 |
|
|
memset (&event, 0, sizeof (struct epoll_event));
|
190 |
|
|
|
191 |
|
|
if ((ops & gnu_java_nio_EpollSelectorImpl_OP_ACCEPT) != 0
|
192 |
|
|
|| (ops & gnu_java_nio_EpollSelectorImpl_OP_READ) != 0)
|
193 |
|
|
event.events = EPOLLIN;
|
194 |
|
|
|
195 |
|
|
if ((ops & gnu_java_nio_EpollSelectorImpl_OP_CONNECT) != 0
|
196 |
|
|
|| (ops & gnu_java_nio_EpollSelectorImpl_OP_WRITE) != 0)
|
197 |
|
|
event.events |= EPOLLOUT;
|
198 |
|
|
|
199 |
|
|
event.data.fd = fd;
|
200 |
|
|
|
201 |
|
|
#ifdef TRACE_EPOLL
|
202 |
|
|
fprintf (stderr, "%s: modding struct epoll_event { events: %o; data.fd: %d } on %d\n",
|
203 |
|
|
__FUNCTION__, event.events, event.data.fd, efd);
|
204 |
|
|
#endif /* TRACE_EPOLL */
|
205 |
|
|
|
206 |
|
|
if (epoll_ctl (efd, EPOLL_CTL_MOD, fd, &event) == -1)
|
207 |
|
|
{
|
208 |
|
|
if (ENOSYS == errno)
|
209 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
210 |
|
|
strerror (errno));
|
211 |
|
|
else
|
212 |
|
|
JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
|
213 |
|
|
}
|
214 |
|
|
#else
|
215 |
|
|
(void) efd;
|
216 |
|
|
(void) fd;
|
217 |
|
|
(void) ops;
|
218 |
|
|
JCL_ThrowException (env, "java/lang/InternalError", "epoll support not available");
|
219 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
/*
|
224 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
225 |
|
|
* Method: epoll_delete
|
226 |
|
|
* Signature: (II)V
|
227 |
|
|
*/
|
228 |
|
|
JNIEXPORT void JNICALL
|
229 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_epoll_1delete (JNIEnv *env,
|
230 |
|
|
jclass c __attribute__((unused)),
|
231 |
|
|
jint efd, jint fd)
|
232 |
|
|
{
|
233 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
234 |
|
|
struct epoll_event event;
|
235 |
|
|
|
236 |
|
|
memset (&event, 0, sizeof (struct epoll_event));
|
237 |
|
|
event.data.fd = fd;
|
238 |
|
|
|
239 |
|
|
#ifdef TRACE_EPOLL
|
240 |
|
|
fprintf (stderr, "%s: delete events on fd %d for %d\n", __FUNCTION__, fd, efd);
|
241 |
|
|
#endif /* TRACE_EPOLL */
|
242 |
|
|
|
243 |
|
|
/* Older kernel versions require a non-null `event' parameter,
|
244 |
|
|
* even though it is ignored by this call.
|
245 |
|
|
*/
|
246 |
|
|
if (epoll_ctl (efd, EPOLL_CTL_DEL, fd, &event) == -1)
|
247 |
|
|
{
|
248 |
|
|
if (ENOSYS == errno)
|
249 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
250 |
|
|
strerror (errno));
|
251 |
|
|
/* XXX the docs here seem a little strange. If `fd' is closed,
|
252 |
|
|
epoll_ctl returns EBADF; but the docs say that this happens
|
253 |
|
|
only when efd is invalid. Go figure.
|
254 |
|
|
*/
|
255 |
|
|
else if (ENOENT == errno || EBADF == errno)
|
256 |
|
|
return; /* fd is closed; it's already removed. */
|
257 |
|
|
else
|
258 |
|
|
JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
|
259 |
|
|
}
|
260 |
|
|
#else
|
261 |
|
|
(void) efd;
|
262 |
|
|
(void) fd;
|
263 |
|
|
JCL_ThrowException (env, "java/lang/InternalError", "epoll support not available");
|
264 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
/*
|
268 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
269 |
|
|
* Method: epoll_wait
|
270 |
|
|
* Signature: (ILjava/nio/ByteBuffer;II)I
|
271 |
|
|
*/
|
272 |
|
|
JNIEXPORT jint JNICALL
|
273 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_epoll_1wait (JNIEnv *env,
|
274 |
|
|
jclass c __attribute__((unused)),
|
275 |
|
|
jint efd, jobject nstate,
|
276 |
|
|
jint num_events, jint timeout)
|
277 |
|
|
{
|
278 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
279 |
|
|
void *p = (*env)->GetDirectBufferAddress (env, nstate);
|
280 |
|
|
struct epoll_event *events = (struct epoll_event *) p;
|
281 |
|
|
int ret;
|
282 |
|
|
|
283 |
|
|
if (p == NULL)
|
284 |
|
|
{
|
285 |
|
|
if (!(*env)->ExceptionCheck (env))
|
286 |
|
|
JCL_ThrowException (env, IO_EXCEPTION, "getting native state failed");
|
287 |
|
|
return -1;
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
#ifdef TRACE_EPOLL
|
291 |
|
|
fprintf (stderr, "%s: events: %p; num_events: %d; timeout: %d; efd: %d\n",
|
292 |
|
|
__FUNCTION__, p, num_events, timeout, efd);
|
293 |
|
|
#endif /* TRACE_EPOLL */
|
294 |
|
|
|
295 |
|
|
ret = epoll_wait (efd, events, num_events, timeout);
|
296 |
|
|
|
297 |
|
|
if (ret == -1)
|
298 |
|
|
{
|
299 |
|
|
if (ENOSYS == errno)
|
300 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
301 |
|
|
strerror (errno));
|
302 |
|
|
else if (EINTR == errno)
|
303 |
|
|
ret = 0;
|
304 |
|
|
else
|
305 |
|
|
JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
#ifdef TRACE_EPOLL
|
309 |
|
|
fprintf (stderr, " epoll_wait returns %d\n", ret);
|
310 |
|
|
{
|
311 |
|
|
int i;
|
312 |
|
|
for (i = 0; i < ret; i++)
|
313 |
|
|
{
|
314 |
|
|
fprintf (stderr, " [%4i]: events: %o; data.fd: %d\n", i, events[i].events,
|
315 |
|
|
events[i].data.fd);
|
316 |
|
|
}
|
317 |
|
|
}
|
318 |
|
|
fflush (stderr);
|
319 |
|
|
#endif /* TRACE_EPOLL */
|
320 |
|
|
|
321 |
|
|
return ret;
|
322 |
|
|
#else
|
323 |
|
|
(void) efd;
|
324 |
|
|
(void) nstate;
|
325 |
|
|
(void) num_events;
|
326 |
|
|
(void) timeout;
|
327 |
|
|
JCL_ThrowException (env, "java/lang/InternalError", "epoll support not available");
|
328 |
|
|
return -1;
|
329 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
/*
|
334 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
335 |
|
|
* Method: selected_fd
|
336 |
|
|
* Signature: (Ljava/nio/ByteBuffer;)I
|
337 |
|
|
*/
|
338 |
|
|
JNIEXPORT jint JNICALL
|
339 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_selected_1fd (JNIEnv *env,
|
340 |
|
|
jclass c __attribute__((unused)),
|
341 |
|
|
jobject value)
|
342 |
|
|
{
|
343 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
344 |
|
|
void *p = (*env)->GetDirectBufferAddress (env, value);
|
345 |
|
|
struct epoll_event *event = (struct epoll_event *) p;
|
346 |
|
|
|
347 |
|
|
#ifdef TRACE_EPOLL
|
348 |
|
|
fprintf (stderr, "%s: event: %p\n", __FUNCTION__, p);
|
349 |
|
|
#endif /* TRACE_EPOLL */
|
350 |
|
|
|
351 |
|
|
if (p == NULL)
|
352 |
|
|
{
|
353 |
|
|
if (!(*env)->ExceptionCheck (env))
|
354 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
355 |
|
|
"getting native state failed");
|
356 |
|
|
return -1;
|
357 |
|
|
}
|
358 |
|
|
|
359 |
|
|
#ifdef TRACE_EPOLL
|
360 |
|
|
fprintf (stderr, " data.fd: %d\n", event->data.fd);
|
361 |
|
|
fflush (stderr);
|
362 |
|
|
#endif /* TRACE_EPOLL */
|
363 |
|
|
|
364 |
|
|
return event->data.fd;
|
365 |
|
|
#else
|
366 |
|
|
(void) value;
|
367 |
|
|
JCL_ThrowException (env, "java/lang/InternalError", "epoll support not available");
|
368 |
|
|
return -1;
|
369 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
370 |
|
|
}
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
/*
|
374 |
|
|
* Class: gnu_java_nio_EpollSelectorImpl
|
375 |
|
|
* Method: selected_ops
|
376 |
|
|
* Signature: (Ljava/nio/ByteBuffer;)I
|
377 |
|
|
*/
|
378 |
|
|
JNIEXPORT jint JNICALL
|
379 |
|
|
Java_gnu_java_nio_EpollSelectorImpl_selected_1ops (JNIEnv *env,
|
380 |
|
|
jclass c __attribute__((unused)),
|
381 |
|
|
jobject value)
|
382 |
|
|
{
|
383 |
|
|
#ifdef HAVE_EPOLL_CREATE
|
384 |
|
|
void *p = (*env)->GetDirectBufferAddress (env, value);
|
385 |
|
|
struct epoll_event *event = (struct epoll_event *) p;
|
386 |
|
|
int ret = 0;
|
387 |
|
|
|
388 |
|
|
#ifdef TRACE_EPOLL
|
389 |
|
|
fprintf (stderr, "%s: event: %p\n", __FUNCTION__, p);
|
390 |
|
|
#endif /* TRACE_EPOLL */
|
391 |
|
|
|
392 |
|
|
if (p == NULL)
|
393 |
|
|
{
|
394 |
|
|
if (!(*env)->ExceptionCheck (env))
|
395 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
396 |
|
|
"getting native state failed");
|
397 |
|
|
return -1;
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
if ((event->events & EPOLLIN) != 0)
|
401 |
|
|
ret |= gnu_java_nio_EpollSelectorImpl_OP_ACCEPT | gnu_java_nio_EpollSelectorImpl_OP_READ;
|
402 |
|
|
if ((event->events & EPOLLOUT) != 0)
|
403 |
|
|
ret |= gnu_java_nio_EpollSelectorImpl_OP_CONNECT | gnu_java_nio_EpollSelectorImpl_OP_WRITE;
|
404 |
|
|
|
405 |
|
|
#ifdef TRACE_EPOLL
|
406 |
|
|
fprintf (stderr, " events: %o\n", event->events);
|
407 |
|
|
fflush (stderr);
|
408 |
|
|
#endif /* TRACE_EPOLL */
|
409 |
|
|
|
410 |
|
|
return ret;
|
411 |
|
|
#else
|
412 |
|
|
(void) value;
|
413 |
|
|
JCL_ThrowException (env, "java/lang/InternalError", "epoll support not available");
|
414 |
|
|
return -1;
|
415 |
|
|
#endif /* HAVE_EPOLL_CREATE */
|
416 |
|
|
}
|