Path: blob/master/src/java.base/linux/native/libnio/ch/EPoll.c
41133 views
/*1* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <dlfcn.h>26#include <unistd.h>27#include <sys/types.h>28#include <sys/epoll.h>2930#include "jni.h"31#include "jni_util.h"32#include "jvm.h"33#include "jlong.h"34#include "nio.h"35#include "nio_util.h"3637#include "sun_nio_ch_EPoll.h"3839JNIEXPORT jint JNICALL40Java_sun_nio_ch_EPoll_eventSize(JNIEnv* env, jclass clazz)41{42return sizeof(struct epoll_event);43}4445JNIEXPORT jint JNICALL46Java_sun_nio_ch_EPoll_eventsOffset(JNIEnv* env, jclass clazz)47{48return offsetof(struct epoll_event, events);49}5051JNIEXPORT jint JNICALL52Java_sun_nio_ch_EPoll_dataOffset(JNIEnv* env, jclass clazz)53{54return offsetof(struct epoll_event, data);55}5657JNIEXPORT jint JNICALL58Java_sun_nio_ch_EPoll_create(JNIEnv *env, jclass clazz) {59int epfd = epoll_create1(EPOLL_CLOEXEC);60if (epfd < 0) {61JNU_ThrowIOExceptionWithLastError(env, "epoll_create1 failed");62}63return epfd;64}6566JNIEXPORT jint JNICALL67Java_sun_nio_ch_EPoll_ctl(JNIEnv *env, jclass clazz, jint epfd,68jint opcode, jint fd, jint events)69{70struct epoll_event event;71int res;7273event.events = events;74event.data.fd = fd;7576res = epoll_ctl(epfd, (int)opcode, (int)fd, &event);77return (res == 0) ? 0 : errno;78}7980JNIEXPORT jint JNICALL81Java_sun_nio_ch_EPoll_wait(JNIEnv *env, jclass clazz, jint epfd,82jlong address, jint numfds, jint timeout)83{84struct epoll_event *events = jlong_to_ptr(address);85int res = epoll_wait(epfd, events, numfds, timeout);86if (res < 0) {87if (errno == EINTR) {88return IOS_INTERRUPTED;89} else {90JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");91return IOS_THROWN;92}93}94return res;95}969798