author | alanb |
Tue, 26 Aug 2008 09:23:12 +0100 | |
changeset 1142 | e01e390f6551 |
parent 2 | 90ce3da70b43 |
child 1247 | b4c26443dee5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
#include "jni.h" |
|
27 |
#include "jni_util.h" |
|
28 |
#include "jvm.h" |
|
29 |
#include "jlong.h" |
|
30 |
||
31 |
#include "sun_nio_ch_EPollArrayWrapper.h" |
|
32 |
||
33 |
#include <dlfcn.h> |
|
34 |
#include <unistd.h> |
|
35 |
#include <sys/resource.h> |
|
36 |
#include <sys/time.h> |
|
37 |
||
38 |
#ifdef __cplusplus |
|
39 |
extern "C" { |
|
40 |
#endif |
|
41 |
||
42 |
/* epoll_wait(2) man page */ |
|
43 |
||
44 |
typedef union epoll_data { |
|
45 |
void *ptr; |
|
46 |
int fd; |
|
47 |
__uint32_t u32; |
|
48 |
__uint64_t u64; |
|
49 |
} epoll_data_t; |
|
50 |
||
1142
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
51 |
|
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
52 |
/* x86-64 has same alignment as 32-bit */ |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
53 |
#ifdef __x86_64__ |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
54 |
#define EPOLL_PACKED __attribute__((packed)) |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
55 |
#else |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
56 |
#define EPOLL_PACKED |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
57 |
#endif |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
58 |
|
2 | 59 |
struct epoll_event { |
60 |
__uint32_t events; /* Epoll events */ |
|
61 |
epoll_data_t data; /* User data variable */ |
|
1142
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
62 |
} EPOLL_PACKED; |
2 | 63 |
|
64 |
#ifdef __cplusplus |
|
65 |
} |
|
66 |
#endif |
|
67 |
||
68 |
#define RESTARTABLE(_cmd, _result) do { \ |
|
69 |
do { \ |
|
70 |
_result = _cmd; \ |
|
71 |
} while((_result == -1) && (errno == EINTR)); \ |
|
72 |
} while(0) |
|
73 |
||
74 |
/* |
|
75 |
* epoll event notification is new in 2.6 kernel. As the offical build |
|
76 |
* platform for the JDK is on a 2.4-based distribution then we must |
|
77 |
* obtain the addresses of the epoll functions dynamically. |
|
78 |
*/ |
|
79 |
typedef int (*epoll_create_t)(int size); |
|
80 |
typedef int (*epoll_ctl_t) (int epfd, int op, int fd, struct epoll_event *event); |
|
81 |
typedef int (*epoll_wait_t) (int epfd, struct epoll_event *events, int maxevents, int timeout); |
|
82 |
||
83 |
static epoll_create_t epoll_create_func; |
|
84 |
static epoll_ctl_t epoll_ctl_func; |
|
85 |
static epoll_wait_t epoll_wait_func; |
|
86 |
||
87 |
static int |
|
88 |
iepoll(int epfd, struct epoll_event *events, int numfds, jlong timeout) |
|
89 |
{ |
|
90 |
jlong start, now; |
|
91 |
int remaining = timeout; |
|
92 |
struct timeval t; |
|
93 |
int diff; |
|
94 |
||
95 |
gettimeofday(&t, NULL); |
|
96 |
start = t.tv_sec * 1000 + t.tv_usec / 1000; |
|
97 |
||
98 |
for (;;) { |
|
99 |
int res = (*epoll_wait_func)(epfd, events, numfds, timeout); |
|
100 |
if (res < 0 && errno == EINTR) { |
|
101 |
if (remaining >= 0) { |
|
102 |
gettimeofday(&t, NULL); |
|
103 |
now = t.tv_sec * 1000 + t.tv_usec / 1000; |
|
104 |
diff = now - start; |
|
105 |
remaining -= diff; |
|
106 |
if (diff < 0 || remaining <= 0) { |
|
107 |
return 0; |
|
108 |
} |
|
109 |
start = now; |
|
110 |
} |
|
111 |
} else { |
|
112 |
return res; |
|
113 |
} |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
JNIEXPORT void JNICALL |
|
118 |
Java_sun_nio_ch_EPollArrayWrapper_init(JNIEnv *env, jclass this) |
|
119 |
{ |
|
120 |
epoll_create_func = (epoll_create_t) dlsym(RTLD_DEFAULT, "epoll_create"); |
|
121 |
epoll_ctl_func = (epoll_ctl_t) dlsym(RTLD_DEFAULT, "epoll_ctl"); |
|
122 |
epoll_wait_func = (epoll_wait_t) dlsym(RTLD_DEFAULT, "epoll_wait"); |
|
123 |
||
124 |
if ((epoll_create_func == NULL) || (epoll_ctl_func == NULL) || |
|
125 |
(epoll_wait_func == NULL)) { |
|
126 |
JNU_ThrowInternalError(env, "unable to get address of epoll functions, pre-2.6 kernel?"); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
JNIEXPORT jint JNICALL |
|
131 |
Java_sun_nio_ch_EPollArrayWrapper_epollCreate(JNIEnv *env, jobject this) |
|
132 |
{ |
|
133 |
/* |
|
134 |
* epoll_create expects a size as a hint to the kernel about how to |
|
135 |
* dimension internal structures. We can't predict the size in advance. |
|
136 |
*/ |
|
137 |
int epfd = (*epoll_create_func)(256); |
|
138 |
if (epfd < 0) { |
|
139 |
JNU_ThrowIOExceptionWithLastError(env, "epoll_create failed"); |
|
140 |
} |
|
141 |
return epfd; |
|
142 |
} |
|
143 |
||
144 |
JNIEXPORT jint JNICALL |
|
145 |
Java_sun_nio_ch_EPollArrayWrapper_fdLimit(JNIEnv *env, jclass this) |
|
146 |
{ |
|
147 |
struct rlimit rlp; |
|
148 |
if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) { |
|
149 |
JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed"); |
|
150 |
} |
|
151 |
return (jint)rlp.rlim_max; |
|
152 |
} |
|
153 |
||
1142
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
154 |
JNIEXPORT jint JNICALL |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
155 |
Java_sun_nio_ch_EPollArrayWrapper_sizeofEPollEvent(JNIEnv* env, jclass this) |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
156 |
{ |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
157 |
return sizeof(struct epoll_event); |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
158 |
} |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
159 |
|
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
160 |
JNIEXPORT jint JNICALL |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
161 |
Java_sun_nio_ch_EPollArrayWrapper_offsetofData(JNIEnv* env, jclass this) |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
162 |
{ |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
163 |
return offsetof(struct epoll_event, data); |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
164 |
} |
e01e390f6551
6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents:
2
diff
changeset
|
165 |
|
2 | 166 |
JNIEXPORT void JNICALL |
167 |
Java_sun_nio_ch_EPollArrayWrapper_epollCtl(JNIEnv *env, jobject this, jint epfd, |
|
168 |
jint opcode, jint fd, jint events) |
|
169 |
{ |
|
170 |
struct epoll_event event; |
|
171 |
int res; |
|
172 |
||
173 |
event.events = events; |
|
174 |
event.data.fd = fd; |
|
175 |
||
176 |
RESTARTABLE((*epoll_ctl_func)(epfd, (int)opcode, (int)fd, &event), res); |
|
177 |
||
178 |
/* |
|
179 |
* A channel may be registered with several Selectors. When each Selector |
|
180 |
* is polled a EPOLL_CTL_DEL op will be inserted into its pending update |
|
181 |
* list to remove the file descriptor from epoll. The "last" Selector will |
|
182 |
* close the file descriptor which automatically unregisters it from each |
|
183 |
* epoll descriptor. To avoid costly synchronization between Selectors we |
|
184 |
* allow pending updates to be processed, ignoring errors. The errors are |
|
185 |
* harmless as the last update for the file descriptor is guaranteed to |
|
186 |
* be EPOLL_CTL_DEL. |
|
187 |
*/ |
|
188 |
if (res < 0 && errno != EBADF && errno != ENOENT && errno != EPERM) { |
|
189 |
JNU_ThrowIOExceptionWithLastError(env, "epoll_ctl failed"); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
JNIEXPORT jint JNICALL |
|
194 |
Java_sun_nio_ch_EPollArrayWrapper_epollWait(JNIEnv *env, jobject this, |
|
195 |
jlong address, jint numfds, |
|
196 |
jlong timeout, jint epfd) |
|
197 |
{ |
|
198 |
struct epoll_event *events = jlong_to_ptr(address); |
|
199 |
int res; |
|
200 |
||
201 |
if (timeout <= 0) { /* Indefinite or no wait */ |
|
202 |
RESTARTABLE((*epoll_wait_func)(epfd, events, numfds, timeout), res); |
|
203 |
} else { /* Bounded wait; bounded restarts */ |
|
204 |
res = iepoll(epfd, events, numfds, timeout); |
|
205 |
} |
|
206 |
||
207 |
if (res < 0) { |
|
208 |
JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed"); |
|
209 |
} |
|
210 |
return res; |
|
211 |
} |
|
212 |
||
213 |
JNIEXPORT void JNICALL |
|
214 |
Java_sun_nio_ch_EPollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd) |
|
215 |
{ |
|
216 |
int fakebuf[1]; |
|
217 |
fakebuf[0] = 1; |
|
218 |
if (write(fd, fakebuf, 1) < 0) { |
|
219 |
JNU_ThrowIOExceptionWithLastError(env,"write to interrupt fd failed"); |
|
220 |
} |
|
221 |
} |