0N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "jlong.h"
0N/A
0N/A#include "sun_nio_ch_EPollArrayWrapper.h"
0N/A
0N/A#include <unistd.h>
0N/A#include <sys/time.h>
0N/A#include <sys/epoll.h>
0N/A
0N/A#define RESTARTABLE(_cmd, _result) do { \
0N/A do { \
0N/A _result = _cmd; \
0N/A } while((_result == -1) && (errno == EINTR)); \
0N/A} while(0)
0N/A
0N/A
0N/Astatic int
0N/Aiepoll(int epfd, struct epoll_event *events, int numfds, jlong timeout)
0N/A{
0N/A jlong start, now;
0N/A int remaining = timeout;
0N/A struct timeval t;
0N/A int diff;
0N/A
0N/A gettimeofday(&t, NULL);
0N/A start = t.tv_sec * 1000 + t.tv_usec / 1000;
0N/A
0N/A for (;;) {
0N/A int res = epoll_wait(epfd, events, numfds, timeout);
0N/A if (res < 0 && errno == EINTR) {
0N/A if (remaining >= 0) {
0N/A gettimeofday(&t, NULL);
0N/A now = t.tv_sec * 1000 + t.tv_usec / 1000;
0N/A diff = now - start;
0N/A remaining -= diff;
0N/A if (diff < 0 || remaining <= 0) {
0N/A return 0;
0N/A }
0N/A start = now;
0N/A }
0N/A } else {
0N/A return res;
0N/A }
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_EPollArrayWrapper_init(JNIEnv *env, jclass this)
0N/A{
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_EPollArrayWrapper_epollCreate(JNIEnv *env, jobject this)
0N/A{
0N/A /*
0N/A * epoll_create expects a size as a hint to the kernel about how to
0N/A * dimension internal structures. We can't predict the size in advance.
0N/A */
0N/A int epfd = epoll_create(256);
0N/A if (epfd < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "epoll_create failed");
0N/A }
0N/A return epfd;
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_EPollArrayWrapper_sizeofEPollEvent(JNIEnv* env, jclass this)
0N/A{
0N/A return sizeof(struct epoll_event);
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_EPollArrayWrapper_offsetofData(JNIEnv* env, jclass this)
0N/A{
0N/A return offsetof(struct epoll_event, data);
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_EPollArrayWrapper_epollCtl(JNIEnv *env, jobject this, jint epfd,
0N/A jint opcode, jint fd, jint events)
0N/A{
0N/A struct epoll_event event;
0N/A int res;
0N/A
0N/A event.events = events;
0N/A event.data.fd = fd;
0N/A
0N/A RESTARTABLE(epoll_ctl(epfd, (int)opcode, (int)fd, &event), res);
0N/A
0N/A /*
0N/A * A channel may be registered with several Selectors. When each Selector
0N/A * is polled a EPOLL_CTL_DEL op will be inserted into its pending update
0N/A * list to remove the file descriptor from epoll. The "last" Selector will
0N/A * close the file descriptor which automatically unregisters it from each
0N/A * epoll descriptor. To avoid costly synchronization between Selectors we
0N/A * allow pending updates to be processed, ignoring errors. The errors are
0N/A * harmless as the last update for the file descriptor is guaranteed to
0N/A * be EPOLL_CTL_DEL.
0N/A */
0N/A if (res < 0 && errno != EBADF && errno != ENOENT && errno != EPERM) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "epoll_ctl failed");
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_EPollArrayWrapper_epollWait(JNIEnv *env, jobject this,
0N/A jlong address, jint numfds,
0N/A jlong timeout, jint epfd)
0N/A{
0N/A struct epoll_event *events = jlong_to_ptr(address);
0N/A int res;
0N/A
0N/A if (timeout <= 0) { /* Indefinite or no wait */
0N/A RESTARTABLE(epoll_wait(epfd, events, numfds, timeout), res);
0N/A } else { /* Bounded wait; bounded restarts */
28N/A res = iepoll(epfd, events, numfds, timeout);
28N/A }
0N/A
0N/A if (res < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");
0N/A }
0N/A return res;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_EPollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
0N/A{
0N/A int fakebuf[1];
0N/A fakebuf[0] = 1;
0N/A if (write(fd, fakebuf, 1) < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env,"write to interrupt fd failed");
0N/A }
0N/A}
0N/A