Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native/sun/nio/ch/KQueueArrayWrapper.c
38918 views
/*1* Copyright (c) 2011, 2012, 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/*26* KQueueArrayWrapper.c27* Implementation of Selector using FreeBSD / Mac OS X kqueues28* Derived from Sun's DevPollArrayWrapper29*/303132#include "jni.h"33#include "jni_util.h"34#include "jvm.h"35#include "jlong.h"3637#include <sys/types.h>38#include <sys/event.h>39#include <sys/time.h>4041JNIEXPORT void JNICALL42Java_sun_nio_ch_KQueueArrayWrapper_initStructSizes(JNIEnv *env, jclass clazz)43{44#define CHECK_EXCEPTION() { \45if ((*env)->ExceptionCheck(env)) { \46goto exceptionOccurred; \47} \48}4950#define CHECK_ERROR_AND_EXCEPTION(_field) { \51if (_field == NULL) { \52goto badField; \53} \54CHECK_EXCEPTION(); \55}565758jfieldID field;5960field = (*env)->GetStaticFieldID(env, clazz, "EVFILT_READ", "S");61CHECK_ERROR_AND_EXCEPTION(field);62(*env)->SetStaticShortField(env, clazz, field, EVFILT_READ);63CHECK_EXCEPTION();6465field = (*env)->GetStaticFieldID(env, clazz, "EVFILT_WRITE", "S");66CHECK_ERROR_AND_EXCEPTION(field);67(*env)->SetStaticShortField(env, clazz, field, EVFILT_WRITE);68CHECK_EXCEPTION();6970field = (*env)->GetStaticFieldID(env, clazz, "SIZEOF_KEVENT", "S");71CHECK_ERROR_AND_EXCEPTION(field);72(*env)->SetStaticShortField(env, clazz, field, (short) sizeof(struct kevent));73CHECK_EXCEPTION();7475field = (*env)->GetStaticFieldID(env, clazz, "FD_OFFSET", "S");76CHECK_ERROR_AND_EXCEPTION(field);77(*env)->SetStaticShortField(env, clazz, field, (short) offsetof(struct kevent, ident));78CHECK_EXCEPTION();7980field = (*env)->GetStaticFieldID(env, clazz, "FILTER_OFFSET", "S");81CHECK_ERROR_AND_EXCEPTION(field);82(*env)->SetStaticShortField(env, clazz, field, (short) offsetof(struct kevent, filter));83CHECK_EXCEPTION();84return;8586badField:87return;8889exceptionOccurred:90return;9192#undef CHECK_EXCEPTION93#undef CHECK_ERROR_AND_EXCEPTION94}9596JNIEXPORT jint JNICALL97Java_sun_nio_ch_KQueueArrayWrapper_init(JNIEnv *env, jobject this)98{99int kq = kqueue();100if (kq < 0) {101JNU_ThrowIOExceptionWithLastError(env, "KQueueArrayWrapper: kqueue() failed");102}103return kq;104}105106107JNIEXPORT void JNICALL108Java_sun_nio_ch_KQueueArrayWrapper_register0(JNIEnv *env, jobject this,109jint kq, jint fd, jint r, jint w)110{111struct kevent changes[2];112struct kevent errors[2];113struct timespec dontBlock = {0, 0};114115// if (r) then { register for read } else { unregister for read }116// if (w) then { register for write } else { unregister for write }117// Ignore errors - they're probably complaints about deleting non-118// added filters - but provide an error array anyway because119// kqueue behaves erratically if some of its registrations fail.120EV_SET(&changes[0], fd, EVFILT_READ, r ? EV_ADD : EV_DELETE, 0, 0, 0);121EV_SET(&changes[1], fd, EVFILT_WRITE, w ? EV_ADD : EV_DELETE, 0, 0, 0);122kevent(kq, changes, 2, errors, 2, &dontBlock);123}124125126JNIEXPORT jint JNICALL127Java_sun_nio_ch_KQueueArrayWrapper_kevent0(JNIEnv *env, jobject this, jint kq,128jlong kevAddr, jint kevCount,129jlong timeout)130{131struct kevent *kevs = (struct kevent *)jlong_to_ptr(kevAddr);132struct timespec ts;133struct timespec *tsp;134int result;135136// Java timeout is in milliseconds. Convert to struct timespec.137// Java timeout == -1 : wait forever : timespec timeout of NULL138// Java timeout == 0 : return immediately : timespec timeout of zero139if (timeout >= 0) {140ts.tv_sec = timeout / 1000;141ts.tv_nsec = (timeout % 1000) * 1000000; //nanosec = 1 million millisec142tsp = &ts;143} else {144tsp = NULL;145}146147result = kevent(kq, NULL, 0, kevs, kevCount, tsp);148149if (result < 0) {150if (errno == EINTR) {151// ignore EINTR, pretend nothing was selected152result = 0;153} else {154JNU_ThrowIOExceptionWithLastError(env, "KQueueArrayWrapper: kqueue failed");155}156}157158return result;159}160161162JNIEXPORT void JNICALL163Java_sun_nio_ch_KQueueArrayWrapper_interrupt(JNIEnv *env, jclass cls, jint fd)164{165char c = 1;166if (1 != write(fd, &c, 1)) {167JNU_ThrowIOExceptionWithLastError(env, "KQueueArrayWrapper: interrupt failed");168}169}170171172173