author | bpb |
Mon, 12 Feb 2018 08:19:33 -0800 | |
changeset 48848 | 81f3a5eaecb0 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
48848
81f3a5eaecb0
8197498: (se) kevent polling should ignore signal interrupt (EINTR)
bpb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. |
12047 | 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 |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
12047 | 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 |
/* |
|
27 |
* KQueueArrayWrapper.c |
|
28 |
* Implementation of Selector using FreeBSD / Mac OS X kqueues |
|
29 |
* Derived from Sun's DevPollArrayWrapper |
|
30 |
*/ |
|
31 |
||
32 |
||
33 |
#include "jni.h" |
|
34 |
#include "jni_util.h" |
|
35 |
#include "jvm.h" |
|
36 |
#include "jlong.h" |
|
48848
81f3a5eaecb0
8197498: (se) kevent polling should ignore signal interrupt (EINTR)
bpb
parents:
47216
diff
changeset
|
37 |
#include "nio_util.h" |
12047 | 38 |
|
39 |
#include <sys/types.h> |
|
40 |
#include <sys/event.h> |
|
41 |
#include <sys/time.h> |
|
42 |
||
43 |
JNIEXPORT void JNICALL |
|
44 |
Java_sun_nio_ch_KQueueArrayWrapper_initStructSizes(JNIEnv *env, jclass clazz) |
|
45 |
{ |
|
46 |
#define CHECK_EXCEPTION() { \ |
|
47 |
if ((*env)->ExceptionCheck(env)) { \ |
|
48 |
goto exceptionOccurred; \ |
|
49 |
} \ |
|
50 |
} |
|
51 |
||
52 |
#define CHECK_ERROR_AND_EXCEPTION(_field) { \ |
|
53 |
if (_field == NULL) { \ |
|
54 |
goto badField; \ |
|
55 |
} \ |
|
56 |
CHECK_EXCEPTION(); \ |
|
57 |
} |
|
58 |
||
59 |
||
60 |
jfieldID field; |
|
61 |
||
62 |
field = (*env)->GetStaticFieldID(env, clazz, "EVFILT_READ", "S"); |
|
63 |
CHECK_ERROR_AND_EXCEPTION(field); |
|
64 |
(*env)->SetStaticShortField(env, clazz, field, EVFILT_READ); |
|
65 |
CHECK_EXCEPTION(); |
|
66 |
||
67 |
field = (*env)->GetStaticFieldID(env, clazz, "EVFILT_WRITE", "S"); |
|
68 |
CHECK_ERROR_AND_EXCEPTION(field); |
|
69 |
(*env)->SetStaticShortField(env, clazz, field, EVFILT_WRITE); |
|
70 |
CHECK_EXCEPTION(); |
|
71 |
||
72 |
field = (*env)->GetStaticFieldID(env, clazz, "SIZEOF_KEVENT", "S"); |
|
73 |
CHECK_ERROR_AND_EXCEPTION(field); |
|
74 |
(*env)->SetStaticShortField(env, clazz, field, (short) sizeof(struct kevent)); |
|
75 |
CHECK_EXCEPTION(); |
|
76 |
||
77 |
field = (*env)->GetStaticFieldID(env, clazz, "FD_OFFSET", "S"); |
|
78 |
CHECK_ERROR_AND_EXCEPTION(field); |
|
79 |
(*env)->SetStaticShortField(env, clazz, field, (short) offsetof(struct kevent, ident)); |
|
80 |
CHECK_EXCEPTION(); |
|
81 |
||
82 |
field = (*env)->GetStaticFieldID(env, clazz, "FILTER_OFFSET", "S"); |
|
83 |
CHECK_ERROR_AND_EXCEPTION(field); |
|
84 |
(*env)->SetStaticShortField(env, clazz, field, (short) offsetof(struct kevent, filter)); |
|
85 |
CHECK_EXCEPTION(); |
|
86 |
return; |
|
87 |
||
88 |
badField: |
|
89 |
return; |
|
90 |
||
91 |
exceptionOccurred: |
|
92 |
return; |
|
93 |
||
94 |
#undef CHECK_EXCEPTION |
|
95 |
#undef CHECK_ERROR_AND_EXCEPTION |
|
96 |
} |
|
97 |
||
98 |
JNIEXPORT jint JNICALL |
|
99 |
Java_sun_nio_ch_KQueueArrayWrapper_init(JNIEnv *env, jobject this) |
|
100 |
{ |
|
101 |
int kq = kqueue(); |
|
102 |
if (kq < 0) { |
|
103 |
JNU_ThrowIOExceptionWithLastError(env, "KQueueArrayWrapper: kqueue() failed"); |
|
104 |
} |
|
105 |
return kq; |
|
106 |
} |
|
107 |
||
108 |
||
109 |
JNIEXPORT void JNICALL |
|
110 |
Java_sun_nio_ch_KQueueArrayWrapper_register0(JNIEnv *env, jobject this, |
|
111 |
jint kq, jint fd, jint r, jint w) |
|
112 |
{ |
|
113 |
struct kevent changes[2]; |
|
114 |
struct kevent errors[2]; |
|
115 |
struct timespec dontBlock = {0, 0}; |
|
116 |
||
117 |
// if (r) then { register for read } else { unregister for read } |
|
118 |
// if (w) then { register for write } else { unregister for write } |
|
119 |
// Ignore errors - they're probably complaints about deleting non- |
|
120 |
// added filters - but provide an error array anyway because |
|
121 |
// kqueue behaves erratically if some of its registrations fail. |
|
122 |
EV_SET(&changes[0], fd, EVFILT_READ, r ? EV_ADD : EV_DELETE, 0, 0, 0); |
|
123 |
EV_SET(&changes[1], fd, EVFILT_WRITE, w ? EV_ADD : EV_DELETE, 0, 0, 0); |
|
124 |
kevent(kq, changes, 2, errors, 2, &dontBlock); |
|
125 |
} |
|
126 |
||
127 |
JNIEXPORT jint JNICALL |
|
128 |
Java_sun_nio_ch_KQueueArrayWrapper_kevent0(JNIEnv *env, jobject this, jint kq, |
|
129 |
jlong kevAddr, jint kevCount, |
|
130 |
jlong timeout) |
|
131 |
{ |
|
132 |
struct kevent *kevs = (struct kevent *)jlong_to_ptr(kevAddr); |
|
133 |
struct timespec ts; |
|
134 |
struct timespec *tsp; |
|
135 |
int result; |
|
136 |
||
137 |
// Java timeout is in milliseconds. Convert to struct timespec. |
|
138 |
// Java timeout == -1 : wait forever : timespec timeout of NULL |
|
139 |
// Java timeout == 0 : return immediately : timespec timeout of zero |
|
140 |
if (timeout >= 0) { |
|
40739
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
141 |
// For some indeterminate reason kevent(2) has been found to fail with |
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
142 |
// an EINVAL error for timeout values greater than or equal to |
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
143 |
// 100000001000L. To avoid this problem, clamp the timeout arbitrarily |
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
144 |
// to the maximum value of a 32-bit signed integer which is |
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
145 |
// approximately 25 days in milliseconds. |
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
146 |
const jlong timeoutMax = 0x7fffffff; // java.lang.Integer.MAX_VALUE |
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
147 |
if (timeout > timeoutMax) { |
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
148 |
timeout = timeoutMax; |
bdff9be6c98f
8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
25859
diff
changeset
|
149 |
} |
12047 | 150 |
ts.tv_sec = timeout / 1000; |
151 |
ts.tv_nsec = (timeout % 1000) * 1000000; //nanosec = 1 million millisec |
|
152 |
tsp = &ts; |
|
153 |
} else { |
|
154 |
tsp = NULL; |
|
155 |
} |
|
156 |
||
48848
81f3a5eaecb0
8197498: (se) kevent polling should ignore signal interrupt (EINTR)
bpb
parents:
47216
diff
changeset
|
157 |
RESTARTABLE(kevent(kq, NULL, 0, kevs, kevCount, tsp), result); |
12047 | 158 |
if (result < 0) { |
48848
81f3a5eaecb0
8197498: (se) kevent polling should ignore signal interrupt (EINTR)
bpb
parents:
47216
diff
changeset
|
159 |
JNU_ThrowIOExceptionWithLastError(env, |
81f3a5eaecb0
8197498: (se) kevent polling should ignore signal interrupt (EINTR)
bpb
parents:
47216
diff
changeset
|
160 |
"KQueueArrayWrapper: kevent poll failed"); |
12047 | 161 |
} |
162 |
||
163 |
return result; |
|
164 |
} |
|
165 |
||
166 |
||
167 |
JNIEXPORT void JNICALL |
|
168 |
Java_sun_nio_ch_KQueueArrayWrapper_interrupt(JNIEnv *env, jclass cls, jint fd) |
|
169 |
{ |
|
170 |
char c = 1; |
|
171 |
if (1 != write(fd, &c, 1)) { |
|
172 |
JNU_ThrowIOExceptionWithLastError(env, "KQueueArrayWrapper: interrupt failed"); |
|
173 |
} |
|
174 |
} |
|
175 |