1 /*
2 * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include "jni.h"
27 #include "jni_util.h"
28 #include "jvm.h"
29 #include "jlong.h"
30 #include "sun_nio_ch_PollArrayWrapper.h"
31 #include <poll.h>
32 #include <unistd.h>
33 #include <sys/time.h>
34
35 #define RESTARTABLE(_cmd, _result) do { \
36 do { \
37 _result = _cmd; \
38 } while((_result == -1) && (errno == EINTR)); \
39 } while(0)
40
41 static int
ipoll(struct pollfd fds[],unsigned int nfds,int timeout)42 ipoll(struct pollfd fds[], unsigned int nfds, int timeout)
43 {
44 jlong start, now;
45 int remaining = timeout;
46 struct timeval t;
47 int diff;
48
49 gettimeofday(&t, NULL);
50 start = t.tv_sec * 1000 + t.tv_usec / 1000;
51
52 for (;;) {
53 int res = poll(fds, nfds, remaining);
54 if (res < 0 && errno == EINTR) {
55 if (remaining >= 0) {
56 gettimeofday(&t, NULL);
57 now = t.tv_sec * 1000 + t.tv_usec / 1000;
58 diff = now - start;
59 remaining -= diff;
60 if (diff < 0 || remaining <= 0) {
61 return 0;
62 }
63 start = now;
64 }
65 } else {
66 return res;
67 }
68 }
69 }
70
71 JNIEXPORT jint JNICALL
Java_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv * env,jobject this,jlong address,jint numfds,jlong timeout)72 Java_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv *env, jobject this,
73 jlong address, jint numfds,
74 jlong timeout)
75 {
76 struct pollfd *a;
77 int err = 0;
78
79 a = (struct pollfd *) jlong_to_ptr(address);
80
81 if (timeout <= 0) { /* Indefinite or no wait */
82 RESTARTABLE (poll(a, numfds, timeout), err);
83 } else { /* Bounded wait; bounded restarts */
84 err = ipoll(a, numfds, timeout);
85 }
86
87 if (err < 0) {
88 JNU_ThrowIOExceptionWithLastError(env, "Poll failed");
89 }
90 return (jint)err;
91 }
92
93 JNIEXPORT void JNICALL
Java_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv * env,jobject this,jint fd)94 Java_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
95 {
96 int fakebuf[1];
97 fakebuf[0] = 1;
98 if (write(fd, fakebuf, 1) < 0) {
99 JNU_ThrowIOExceptionWithLastError(env,
100 "Write to interrupt fd failed");
101 }
102 }
103