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 |
|
|
51 |
struct epoll_event {
|
|
52 |
__uint32_t events; /* Epoll events */
|
|
53 |
epoll_data_t data; /* User data variable */
|
|
54 |
} __attribute__ ((__packed__));
|
|
55 |
|
|
56 |
#ifdef __cplusplus
|
|
57 |
}
|
|
58 |
#endif
|
|
59 |
|
|
60 |
#define RESTARTABLE(_cmd, _result) do { \
|
|
61 |
do { \
|
|
62 |
_result = _cmd; \
|
|
63 |
} while((_result == -1) && (errno == EINTR)); \
|
|
64 |
} while(0)
|
|
65 |
|
|
66 |
/*
|
|
67 |
* epoll event notification is new in 2.6 kernel. As the offical build
|
|
68 |
* platform for the JDK is on a 2.4-based distribution then we must
|
|
69 |
* obtain the addresses of the epoll functions dynamically.
|
|
70 |
*/
|
|
71 |
typedef int (*epoll_create_t)(int size);
|
|
72 |
typedef int (*epoll_ctl_t) (int epfd, int op, int fd, struct epoll_event *event);
|
|
73 |
typedef int (*epoll_wait_t) (int epfd, struct epoll_event *events, int maxevents, int timeout);
|
|
74 |
|
|
75 |
static epoll_create_t epoll_create_func;
|
|
76 |
static epoll_ctl_t epoll_ctl_func;
|
|
77 |
static epoll_wait_t epoll_wait_func;
|
|
78 |
|
|
79 |
static int
|
|
80 |
iepoll(int epfd, struct epoll_event *events, int numfds, jlong timeout)
|
|
81 |
{
|
|
82 |
jlong start, now;
|
|
83 |
int remaining = timeout;
|
|
84 |
struct timeval t;
|
|
85 |
int diff;
|
|
86 |
|
|
87 |
gettimeofday(&t, NULL);
|
|
88 |
start = t.tv_sec * 1000 + t.tv_usec / 1000;
|
|
89 |
|
|
90 |
for (;;) {
|
|
91 |
int res = (*epoll_wait_func)(epfd, events, numfds, timeout);
|
|
92 |
if (res < 0 && errno == EINTR) {
|
|
93 |
if (remaining >= 0) {
|
|
94 |
gettimeofday(&t, NULL);
|
|
95 |
now = t.tv_sec * 1000 + t.tv_usec / 1000;
|
|
96 |
diff = now - start;
|
|
97 |
remaining -= diff;
|
|
98 |
if (diff < 0 || remaining <= 0) {
|
|
99 |
return 0;
|
|
100 |
}
|
|
101 |
start = now;
|
|
102 |
}
|
|
103 |
} else {
|
|
104 |
return res;
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
JNIEXPORT void JNICALL
|
|
110 |
Java_sun_nio_ch_EPollArrayWrapper_init(JNIEnv *env, jclass this)
|
|
111 |
{
|
|
112 |
epoll_create_func = (epoll_create_t) dlsym(RTLD_DEFAULT, "epoll_create");
|
|
113 |
epoll_ctl_func = (epoll_ctl_t) dlsym(RTLD_DEFAULT, "epoll_ctl");
|
|
114 |
epoll_wait_func = (epoll_wait_t) dlsym(RTLD_DEFAULT, "epoll_wait");
|
|
115 |
|
|
116 |
if ((epoll_create_func == NULL) || (epoll_ctl_func == NULL) ||
|
|
117 |
(epoll_wait_func == NULL)) {
|
|
118 |
JNU_ThrowInternalError(env, "unable to get address of epoll functions, pre-2.6 kernel?");
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
JNIEXPORT jint JNICALL
|
|
123 |
Java_sun_nio_ch_EPollArrayWrapper_epollCreate(JNIEnv *env, jobject this)
|
|
124 |
{
|
|
125 |
/*
|
|
126 |
* epoll_create expects a size as a hint to the kernel about how to
|
|
127 |
* dimension internal structures. We can't predict the size in advance.
|
|
128 |
*/
|
|
129 |
int epfd = (*epoll_create_func)(256);
|
|
130 |
if (epfd < 0) {
|
|
131 |
JNU_ThrowIOExceptionWithLastError(env, "epoll_create failed");
|
|
132 |
}
|
|
133 |
return epfd;
|
|
134 |
}
|
|
135 |
|
|
136 |
JNIEXPORT jint JNICALL
|
|
137 |
Java_sun_nio_ch_EPollArrayWrapper_fdLimit(JNIEnv *env, jclass this)
|
|
138 |
{
|
|
139 |
struct rlimit rlp;
|
|
140 |
if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
|
|
141 |
JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
|
|
142 |
}
|
|
143 |
return (jint)rlp.rlim_max;
|
|
144 |
}
|
|
145 |
|
|
146 |
JNIEXPORT void JNICALL
|
|
147 |
Java_sun_nio_ch_EPollArrayWrapper_epollCtl(JNIEnv *env, jobject this, jint epfd,
|
|
148 |
jint opcode, jint fd, jint events)
|
|
149 |
{
|
|
150 |
struct epoll_event event;
|
|
151 |
int res;
|
|
152 |
|
|
153 |
event.events = events;
|
|
154 |
event.data.fd = fd;
|
|
155 |
|
|
156 |
RESTARTABLE((*epoll_ctl_func)(epfd, (int)opcode, (int)fd, &event), res);
|
|
157 |
|
|
158 |
/*
|
|
159 |
* A channel may be registered with several Selectors. When each Selector
|
|
160 |
* is polled a EPOLL_CTL_DEL op will be inserted into its pending update
|
|
161 |
* list to remove the file descriptor from epoll. The "last" Selector will
|
|
162 |
* close the file descriptor which automatically unregisters it from each
|
|
163 |
* epoll descriptor. To avoid costly synchronization between Selectors we
|
|
164 |
* allow pending updates to be processed, ignoring errors. The errors are
|
|
165 |
* harmless as the last update for the file descriptor is guaranteed to
|
|
166 |
* be EPOLL_CTL_DEL.
|
|
167 |
*/
|
|
168 |
if (res < 0 && errno != EBADF && errno != ENOENT && errno != EPERM) {
|
|
169 |
JNU_ThrowIOExceptionWithLastError(env, "epoll_ctl failed");
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
JNIEXPORT jint JNICALL
|
|
174 |
Java_sun_nio_ch_EPollArrayWrapper_epollWait(JNIEnv *env, jobject this,
|
|
175 |
jlong address, jint numfds,
|
|
176 |
jlong timeout, jint epfd)
|
|
177 |
{
|
|
178 |
struct epoll_event *events = jlong_to_ptr(address);
|
|
179 |
int res;
|
|
180 |
|
|
181 |
if (timeout <= 0) { /* Indefinite or no wait */
|
|
182 |
RESTARTABLE((*epoll_wait_func)(epfd, events, numfds, timeout), res);
|
|
183 |
} else { /* Bounded wait; bounded restarts */
|
|
184 |
res = iepoll(epfd, events, numfds, timeout);
|
|
185 |
}
|
|
186 |
|
|
187 |
if (res < 0) {
|
|
188 |
JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");
|
|
189 |
}
|
|
190 |
return res;
|
|
191 |
}
|
|
192 |
|
|
193 |
JNIEXPORT void JNICALL
|
|
194 |
Java_sun_nio_ch_EPollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
|
|
195 |
{
|
|
196 |
int fakebuf[1];
|
|
197 |
fakebuf[0] = 1;
|
|
198 |
if (write(fd, fakebuf, 1) < 0) {
|
|
199 |
JNU_ThrowIOExceptionWithLastError(env,"write to interrupt fd failed");
|
|
200 |
}
|
|
201 |
}
|