Path: blob/master/src/java.base/windows/native/libnio/ch/WEPollNatives.c
41134 views
/*1* Copyright (c) 2020, 2021, 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 "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "jlong.h"29#include "nio.h"30#include "nio_util.h"31#include "wepoll.h"32#include "sun_nio_ch_WEPoll.h"3334JNIEXPORT jint JNICALL35Java_sun_nio_ch_WEPoll_eventSize(JNIEnv* env, jclass clazz)36{37return sizeof(struct epoll_event);38}3940JNIEXPORT jint JNICALL41Java_sun_nio_ch_WEPoll_eventsOffset(JNIEnv* env, jclass clazz)42{43return offsetof(struct epoll_event, events);44}4546JNIEXPORT jint JNICALL47Java_sun_nio_ch_WEPoll_dataOffset(JNIEnv* env, jclass clazz)48{49return offsetof(struct epoll_event, data);50}5152JNIEXPORT jlong JNICALL53Java_sun_nio_ch_WEPoll_create(JNIEnv *env, jclass clazz) {54HANDLE h = epoll_create1(0);55if (h == NULL) {56JNU_ThrowIOExceptionWithLastError(env, "epoll_create1 failed");57}58return ptr_to_jlong(h);59}6061JNIEXPORT jint JNICALL62Java_sun_nio_ch_WEPoll_ctl(JNIEnv *env, jclass clazz, jlong h,63jint opcode, jlong s, jint events)64{65struct epoll_event event;66int res;67SOCKET socket = (SOCKET) jlong_to_ptr(s);6869event.events = (uint32_t) events;70event.data.sock = socket;7172res = epoll_ctl(jlong_to_ptr(h), opcode, socket, &event);73return (res == 0) ? 0 : errno;74}7576JNIEXPORT jint JNICALL77Java_sun_nio_ch_WEPoll_wait(JNIEnv *env, jclass clazz, jlong h,78jlong address, jint numfds, jint timeout)79{80struct epoll_event *events = jlong_to_ptr(address);81int res = epoll_wait(jlong_to_ptr(h), events, numfds, timeout);82if (res < 0) {83JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");84return IOS_THROWN;85}86return res;87}8889JNIEXPORT void JNICALL90Java_sun_nio_ch_WEPoll_close(JNIEnv *env, jclass clazz, jlong h) {91epoll_close(jlong_to_ptr(h));92}939495